Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Halting and Debugging thread? 1

Status
Not open for further replies.

meinhunna

Programmer
Jul 31, 2004
118
AU
I have three data processing routines which I want to run in threads so that they don’t clog up the UI. How can I halt execution of thread until prior thread completes successfully.

Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click

'Link tables.
'Spin off a new thread.
t1 = New Thread(New ThreadStart(AddressOf CreateLinks))
t1.IsBackground = True
t1.Start()

Select Case True

Case optRawFigures.Checked
'Spin off a new thread.
t2 = New Thread(New ThreadStart(AddressOf GetFigures))
t2.IsBackground = True
t2.Start()

Case optAverage.Checked
'Spin off a new thread.
t3 = New Thread(New ThreadStart(AddressOf CalculateFigures))
t3.IsBackground = True
t3.Start()

End Select

End Sub

Apparently thread t1 is executed on button click along with one of the threads from Select Case. This leads to error because tables need to be linked before any data processing job can be done.

I want t1 to execute and when it is completed successfully it should execute Select Case.

Also, how can I debug the routine which runs off a thread.

Thanks
 
To signal from one thread to another, you'd use an instance of the Monitor class from System.Threading.

Chip H.


____________________________________________________________________
Click here to learn Ways to help with Tsunami Relief
If you want to get the best response to a question, please read FAQ222-2244 first
 
I am pretty new to threading, but another programmer demonstrated setting priorities on threads in a staff meeting yesterday. If you give the first thread the highest priority then it will execute all the way until the end. Give the other two lower priorities and they will begin after the other one ends. Not sure if this answers your question, but at least I tried. If you need the code then I will get it, just let me know.
 
I beleive thread.abort is what you are looking for. Thread.abort will raise a "ThreadAbortException" in the thread specified. If the error is unhandled, the thread will just end. If the you have any advanced error handling (logging, retries, etc), make sure you catch the ThreadAbortException differently then the catch all Exception.

I'm not sure on what the monitor class chiph mentioned is, but you can use delegates and the .invoke/.beinginvoke methods to make calls from one thread to another (very helpful when trying to update a GUI from a non-primary thread).

-Rick

----------------------
 
To stop the current thread until another thread finishes, use the Thread.Join method

t1.Start
....... other stuff

Dim HasThreadEnded as boolean
HasThreadEnded = t1.Join(timeout)

think about Thread.Abort only if you timeout.


Compare Code
 
Giving a thread the highest priority will more than likely lock up the PC.
 
doh, I miss-read. I thought he was looking for a way to cancel a thread.

You could use JY's suggestion, which would require a control thread which then calls the individual process threads then then waits for join inbetween them. This way, the UI thread would be uneffected, and the threads would run sychronously (one after another)

Another possibility would be to use delegates. With delegates you can set up a CallBack procedure which will fire when a thread completes. You could then have the callback method call the next delegate to be processed.

-Rick

----------------------
 
Thanks everyone. I'll try Thread.Join method.

chiph what is Monitor Class for System.Threading though?
 

What about debug in thread. When I place breakpoint in the procedure which is called by a thread it does not break into debug mode ???
 
Perhaps I am being obtuse here but it appears to me he only needs one new thread - since the 2nd two are reliant on the previous thread finishing. Why would he not just spin on a thread to call one sub which calls the other sub - which calls the other sub? No need to communicate between threads at all.
 
sodakotahusker u have a point here, i can do it in one thread. thanks

learned something new though Thread.Join

JohnYingling Build/Config/Active Sol Config is Debug
 
Just common sense and use of the KISS principal. I just started working with threads two days ago.

I was curious how you could debug two threads of code at the same time.

Can one of you gurus help me with this one? I am trying to not only use a secondary thread calling a class - but potentially multiple secondary threads calling the same class.

 
To debug separate threads, you would use the thread pane under the Debug | Windows menu.

Chip H.


____________________________________________________________________
Click here to learn Ways to help with Tsunami Relief
If you want to get the best response to a question, please read FAQ222-2244 first
 
No - difference instance of the class. I wanted to reuse the same object variable to fire off the thread to the class - but I think I have to use a new variable for each instance.

I don't see anything besides BreakPoints and Immediate under the Debug/Windows menu! Is there some parameter I need to set somewhere to allow debugging threads?

In playing with this I discovered the attach menu which apparently will let me attach to any running process and debug it? Would this only be meaning if the symbolic debugging code was included in the compiling - or does Dot Net have a different way of doing this type of thing?
 
Attaching to your running process may help.

You'll get symbols for your code via the .pdb files that are created when you do a debug configuration build. If you want symbols for system dlls, Microsoft has a symbol server that allows you to download symbols to your local machine (makes debugging a little slower as it has to check if you're up to date first).

Chip H.


____________________________________________________________________
Click here to learn Ways to help with Tsunami Relief
If you want to get the best response to a question, please read FAQ222-2244 first
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top