Click here to Skip to main content
Rate this: bad
Please Sign up or sign in to vote.
See more: Threading VB.NET
I am learning threading and I was wondering if there was a way to check if a thread was working or not, something like this.

If thread1.working then
    thread2 = new thread(address of somemethod)
    thread1 = new thread(address of somemethod)
end if

I am not looking for code, just resources on how this would work.
Posted 17-Dec-12 3:57am
Sergey Alexandrovich Kryukov at 17-Dec-12 10:04am
Why, exactly?
Rate this: bad
Please Sign up or sign in to vote.

Solution 1

That depends on what you mean by "working". You can only get the status of a thread (IsRunning), but you cannot get the status of the code it's executing.

The only way to do that would be to rewrite the code your background thread is running to provide some sort of "heartbeat" or some other status variable or events that your parent code can check or subscribe to.
Daniel Verbatim Hallam at 17-Dec-12 9:22am
Would I be right to assume that once the method the thread is executing has completed the thread would no longer be running?
Dave Kreskowiak at 17-Dec-12 9:33am
Once the the code is done the thread dies. It will no longer be running.
Rate this: bad
Please Sign up or sign in to vote.

Solution 2

Daniel Verbatim Hallam at 17-Dec-12 9:24am
Thats a useful link too, I was looking for something like that, thank you.
CPallini at 17-Dec-12 9:33am
you are welcome.
Sergey Alexandrovich Kryukov at 17-Dec-12 9:57am
This is useful, good to know, my 5, but...

It's much better not to poll the thread at all and not depend on return. It's much better to design a thread to notify require set on completion; another approach is to wait for thread completion by Join, which is a blocking code, without wasteful polling; is some situations, this is exactly what should be done.

This way, the real solution of the problem depends on the purpose OP did not share with us.
Espen Harlinn at 17-Dec-12 12:33pm
Rate this: bad
Please Sign up or sign in to vote.

Solution 3

CPallini answered you, but the polling of the thread for status is a sign of really wrong design.

In some situations, you need a blocking call System.Threading.Thread.Join. The calling thread will be put in wait state, sleeping without wasting any CPU cycles, until awakened by completion. Please see:[^].

I see you already trying "if (working) thread.Join()", but this is pointless, really. Explain what you are trying to achieve, the purpose of it.

In other cases, it's the best for a thread to notify other threads on completion. How? It depends. Please see my comments to Solution 2.


This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
0 Sergey Alexandrovich Kryukov 383
1 Sascha Lefévre 215
2 Maciej Los 185
3 Abhinav S 170
4 Richard Deeming 155
0 Sergey Alexandrovich Kryukov 7,141
1 OriginalGriff 6,410
2 Maciej Los 2,832
3 Abhinav S 2,732
4 Peter Leow 2,694

Advertise | Privacy | Mobile
Web03 | 2.8.150414.1 | Last Updated 17 Dec 2012
Copyright © CodeProject, 1999-2015
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100