UncleRonin
Well-known member
Can someone help explain to me the way Services and Threading works in this situation...
I have a service which OnStart() creates background threads. It uses a global variable Active As Boolean to track whether or not threads can execute. OnStart() Active = True and OnStop() Active = False. In the threads there are the following execution patterns...
In Pattern A if the service is stopped (Active = False) all the code executes normally and the thread loop exits like you'd expect. But if the service is stopped then Pattern B will execute DoStuff() but halts. Nothing else happens after Thread.Sleep() so DoOtherStuff() is never called. Is this the way it's supposed to happen or is something funky going on?
Just because the service is stopping surely the thread won't as well, at least not until it's duties are performed. No errors are thrown so it's really quite confusing. I was wondering if maybe the ThreadState WaitSleepJoin had something to do with this behaviour but I'm not knowledgable enough on the subject to comment. What do you think?
I have a service which OnStart() creates background threads. It uses a global variable Active As Boolean to track whether or not threads can execute. OnStart() Active = True and OnStop() Active = False. In the threads there are the following execution patterns...
VB.NET:
'Pattern A
While Me.Active
Thread.Sleep(1000)
DoStuff()
DoOtherStuff()
End While
VB.NET:
'Pattern B
While Me.Active
DoStuff()
Thread.Sleep(1000)
DoOtherStuff()
End While
In Pattern A if the service is stopped (Active = False) all the code executes normally and the thread loop exits like you'd expect. But if the service is stopped then Pattern B will execute DoStuff() but halts. Nothing else happens after Thread.Sleep() so DoOtherStuff() is never called. Is this the way it's supposed to happen or is something funky going on?
Just because the service is stopping surely the thread won't as well, at least not until it's duties are performed. No errors are thrown so it's really quite confusing. I was wondering if maybe the ThreadState WaitSleepJoin had something to do with this behaviour but I'm not knowledgable enough on the subject to comment. What do you think?