Question Threading issues using the threadpool

skythinker

New member
Joined
Dec 12, 2010
Messages
3
Programming Experience
10+
Hello everyone!
I'm working with vb.net 3.5SP1 /VS2008.

I'm familiar with windows threads from C++ but I didn't do it since the 90s.
I have 5 threadpool calls (ThreadPool.QueueUserWorkItem) that do some complex work (they read info from different web pages, regular expressions, etc) and each updates different columns in a data-grid view. They pause for 1 second between each web read with thread.sleep (not to overload the server).

My issue is that some of the threads do not complete. I get for example 3 results when I should have 8. Sometimes it works fine. This is an older 3.0Ghz HT machine, I want to test it on the lowest common denominator(and that is all I have :(). I tried using
VB.NET:
Monitor.Enter(ListView1)
, it seems to help but it is hard to tell. I tried using the thread debug window but it is not really helpful, it seems do disappear during execution.

If I remove the threads I always get my data. I'm wondering if something is being blocked? Is there a way to debug this?

Also, does thread.sleep(x) stop all threads or just the thread that the statement is executed in?
 
My issue is that some of the threads do not complete
Is this before or after you added synchronization?

If you only need Monitor.Enter/Exit you should use SyncLock statement instead, this is a simplification and also ensures the lock is released, which it sounds you're not doing properly. (excerpt from Monitor Class (System.Threading))

If these threads doesn't 'step on each toes', ie using same object asynchronous, you don't need synchronization (apart from invoking to UI thread).
Also, does thread.sleep(x) stop all threads or just the thread that the statement is executed in?
Thread.Sleep Method (Int32) (System.Threading)

Is there a way to debug this?
You can set breakpoints and output debugging information (Debug.Write for example) also from threaded calls.
 
Is this before or after you added synchronization?

If you only need Monitor.Enter/Exit you should use SyncLock statement instead, this is a simplification and also ensures the lock is released, which it sounds you're not doing properly. (excerpt from Monitor Class (System.Threading))

If these threads doesn't 'step on each toes', ie using same object asynchronous, you don't need synchronization (apart from invoking to UI thread).

Thread.Sleep Method (Int32) (System.Threading)


You can set breakpoints and output debugging information (Debug.Write for example) also from threaded calls.

Thanks for the response.

My problems were mostly from before I added the monitor (sync) code. It seems to work most of the time, but very occasionally it seems to crap out. Except for calling my HTML load proc, the threads shouldn't hamper each other. I did find this:

VB.NET:
Private Shared outputBlock As String
 Private Shared proutputblock As String

Which are class level vars AND also being passed as parameters. I don't know if that matters.

I am using code like this:

VB.NET:
Monitor.Enter(ListView1)
                ListView1.Items(i).SubItems(3).Text = blc
Monitor.Exit(ListView1)

I also have an invoke statement in this sub.

I could probably use synclock too, but I thought it would degrade performance.

Also, what would be the best way to know when all my 5 threads complete? or interrupting all of them at once? I'm not sure if threadpool threads can raise events. I was thinking of wrapping them in a Thread class or backgroundworker.
 
As explained SyncLock is a simplification for standard Try-Finally code using Monitor.Enter/Exit, it is just an alias. You're missing the Try-Finally by the way.

An event can be raised from any method, it is raised in same thread context as it is called, you could invoke to UI thread and raise the event from there also.

To know when all workers are completed you could count (add worker, subtract worker). It is also possible to BeginInvoke delegates yourself and use the WaitHandle.WaitForAll method, of course this wait must be done on a secondary thread to not block UI thread.

The best way to stop background work is to use a Boolean flag and check this in worker periodically. To abruptly stop a thread you have to create the Thread instance yourself and use the Abort method, be sure clean up properly from the Finally block if it has open resources. It is possible to get the Thread reference from a threadpool thread, but it is not recommended to meddle with it. Thread class doesn't expose a WaitHandle, so if you need this you can for example use the AutoResetEvent/ManualResetEvent classes for such synchronization.

System.Threading Namespace () This namespace contains most of the .Net threading tools (Delegate class which is relevant belongs to System namespace), there are some good information to be found from these links. General multi-threading tutorials should also be useful for you.
 
Back
Top