Link to home
Start Free TrialLog in
Avatar of Benji_
Benji_Flag for United Kingdom of Great Britain and Northern Ireland

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.


Avatar of Carl Tawn
Carl Tawn
Flag of United Kingdom of Great Britain and Northern Ireland image

You seem to have forgotten the "attached code" step of your question :)
Avatar of Benji_

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

Open in new window

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.
>>This was previously working perfectly, and now it has completely stopped.

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?
Avatar of Benji_

ASKER

The boolean variables are true, even if I put a message box in that subroutine with nothing else, it still does not execute.
Avatar of Benji_

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.
Avatar of Benji_

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?
Avatar of Benji_

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?
Avatar of Benji_

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?
Avatar of Benji_

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?
Avatar of Benji_

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.
Avatar of Benji_

ASKER

It is working. Thanks for that information. Ok, so is there a way to interface with the UI via proxy?
ASKER CERTIFIED SOLUTION
Avatar of Nasir Razzaq
Nasir Razzaq
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Benji_

ASKER

Solution provided worked.