Benji_
asked on
Thread not starting sub VB.NET
Hello everyone. I am having yet another problem with VB.NET
I have a system that constantly pings a database for commands to process, this needs to be running constantly, until it is stopped by the user. The interface of the application needs to remain responsive when this is running though, so I decided to enclose the checking process in a thread. I am using the attached code. It seems the thread starts, as if I call .IsAlive from the startthread sub, it shows it as true, but nothing in the sub is being called.
This was previously working perfectly, and now it has completely stopped.
Any advice would be greatly appreciated, thanks.
I have a system that constantly pings a database for commands to process, this needs to be running constantly, until it is stopped by the user. The interface of the application needs to remain responsive when this is running though, so I decided to enclose the checking process in a thread. I am using the attached code. It seems the thread starts, as if I call .IsAlive from the startthread sub, it shows it as true, but nothing in the sub is being called.
This was previously working perfectly, and now it has completely stopped.
Any advice would be greatly appreciated, thanks.
You seem to have forgotten the "attached code" step of your question :)
ASKER
I attached it, oh well. Here it is:
Sub startthread()
threadJobPing = New Thread(New ThreadStart(AddressOf JobPing))
threadJobPing.Start()
Log("CORE: Loaded Checker thread. Testing...")
End Sub
Sub JobPing()
'log jobs pings to file
If threadJobPing.IsAlive = True Then
Log("Checker Thread Test Complete: Online")
End If
Do While smcCheckJobs = True
If smcIsConfigured = True Then
Log("Checking for available jobs...")
AvailableJobs()
CommandCheck()
Thread.Sleep(180)
Else
Log("SMC is not configured.")
End If
Loop
End Sub
You can simplify this
New Thread(New ThreadStart(AddressOf JobPing))
to
New Thread(AddressOf JobPing)
Is it not executing any code in the sub? May be the boolean in while loop is not true.
New Thread(New ThreadStart(AddressOf JobPing))
to
New Thread(AddressOf JobPing)
Is it not executing any code in the sub? May be the boolean in while loop is not true.
>>This was previously working perfectly, and now it has completely stopped.
What has changed since it was last working?
What has changed since it was last working?
Where are the boolean variables in your sub routine defined?
Are these variables existing outside your thread? If so, they must be global to be seen. What other peices of code touch these variables? How have you made them threadsafe and avoided race conditions?
Where is the Log method calling? Is it outside your thread? If so, how have you made it threadsafe?
Are these variables existing outside your thread? If so, they must be global to be seen. What other peices of code touch these variables? How have you made them threadsafe and avoided race conditions?
Where is the Log method calling? Is it outside your thread? If so, how have you made it threadsafe?
ASKER
The boolean variables are true, even if I put a message box in that subroutine with nothing else, it still does not execute.
ASKER
I tell a lie actually, If i put a message box in the sub, it does actually run. But even though the variables contain the correct values, they are not running the contents of the loop.
ASKER
To further extend the information, when I call the sub manually it works, other than the fact the interface is non-responsive (Which is to be expected). It seems that anything other than a messagebox does not work in that sub when it is called by a thread.
I think the reason could be that you are calling other subs from that sub. What happens if you copy the code from one of the subs to this sub?
ASKER
That is interesting, that code runs. Why is this problem occurring then? It has never happened before and the permissions on the other subs have not changed.
I think its clear that the problem is in calling another sub from a thread. Would it be possible to keep all the code in 1 sub?
ASKER
This is not possible no, because this thread calls upon subs which are needed outside of the thread. I am just wondering as to why this is happening now, when it wasn't previously? Is there any more information I can provide to shed some light on the situation?
Set breakpoints and see whether then other subs are being called or not at all. If it was working previously then it stopped, what change had you made?
ASKER
I changed it from a console application to a GUI application. No other subs are running at the thread execution time.
But the main thread is obviously running. Do you interact with the UI in any of the threads?
ASKER
Yes, there is one UI interaction which is posting to a listbox.
Comment that out and see what happens. You can not interact (directly) with UI from a thread.
ASKER
It is working. Thanks for that information. Ok, so is there a way to interface with the UI via proxy?
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Solution provided worked.