Solved

Thread not starting sub VB.NET

Posted on 2010-11-24
20
495 Views
Last Modified: 2012-05-10
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.


0
Comment
Question by:Benji_
20 Comments
 
LVL 52

Expert Comment

by:Carl Tawn
ID: 34205236
You seem to have forgotten the "attached code" step of your question :)
0
 
LVL 2

Author Comment

by:Benji_
ID: 34205484
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

0
 
LVL 83

Expert Comment

by:CodeCruiser
ID: 34205594
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.
0
 
LVL 32

Expert Comment

by:Erick37
ID: 34205739
>>This was previously working perfectly, and now it has completely stopped.

What has changed since it was last working?
0
 
LVL 9

Expert Comment

by:Orcbighter
ID: 34211981
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?
0
 
LVL 2

Author Comment

by:Benji_
ID: 34212350
The boolean variables are true, even if I put a message box in that subroutine with nothing else, it still does not execute.
0
 
LVL 2

Author Comment

by:Benji_
ID: 34212387
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.
0
 
LVL 2

Author Comment

by:Benji_
ID: 34212485
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.
0
 
LVL 83

Expert Comment

by:CodeCruiser
ID: 34212555
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?
0
 
LVL 2

Author Comment

by:Benji_
ID: 34212621
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.
0
Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

 
LVL 83

Expert Comment

by:CodeCruiser
ID: 34218311
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?
0
 
LVL 2

Author Comment

by:Benji_
ID: 34218350
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?
0
 
LVL 83

Expert Comment

by:CodeCruiser
ID: 34218422
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?
0
 
LVL 2

Author Comment

by:Benji_
ID: 34218847
I changed it from a console application to a GUI application. No other subs are running at the thread execution time.
0
 
LVL 83

Expert Comment

by:CodeCruiser
ID: 34219252
But the main thread is obviously running. Do you interact with the UI in any of the threads?
0
 
LVL 2

Author Comment

by:Benji_
ID: 34229191
Yes, there is one UI interaction which is posting to a listbox.
0
 
LVL 83

Expert Comment

by:CodeCruiser
ID: 34229196
Comment that out and see what happens. You can not interact (directly) with UI from a thread.
0
 
LVL 2

Author Comment

by:Benji_
ID: 34229240
It is working. Thanks for that information. Ok, so is there a way to interface with the UI via proxy?
0
 
LVL 83

Accepted Solution

by:
CodeCruiser earned 500 total points
ID: 34229307
0
 
LVL 2

Author Closing Comment

by:Benji_
ID: 34231235
Solution provided worked.
0

Featured Post

What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

Join & Write a Comment

Microsoft Reports are based on a report definition, which is an XML file that describes data and layout for the report, with a different extension. You can create a client-side report definition language (*.rdlc) file with Visual Studio, and build g…
What my article will show is if you ever had to do processing to a listbox without being able to just select all the items in it. My software Visual Studio 2008 crystal report v11 My issue was I wanted to add crystal report to a form and show…
This is Part 3 in a 3-part series on Experts Exchange to discuss error handling in VBA code written for Excel. Part 1 of this series discussed basic error handling code using VBA. http://www.experts-exchange.com/videos/1478/Excel-Error-Handlin…
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…

747 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now