Howto? Create a multithreaded class library

R

Rudi

Is there anyone who can point me to a good tutorial on how to create a
multithreaded class library?

Class A is a controller that starts up a number of threads of code in Class
B. Class B raises events back to Class A.
I want the code in the eventhandlers of Class A to run on the thread that
created the object.

In a Windows.Forms control this is easy by calling Control.BeginInvoke, but
how do I do this in a normal Class?

<simplified code sample>

using System;
using System.Threading;

public class A
{
private Thread[] tList = new Thread[10];
private B[] bList = new B[10];

public void StartWork()
{
for (int i = 1; i < 10; i++)
{
B b = new B();
Thread t = new Thread(new ThreadStart(b.DoWork));
b.Progress += new EventHandler(b_Progress);

bList[i-1] = b;
tList[i-1] = t;
t.Start();
}
}

private void b_Progress(object sender, EventArgs e)
{
// this method is called on the thread from B

// HOW TO: switch to the thread that called A.StartWork ?

}
}

public class B
{

public bool Canceled = false;
public event EventHandler Progress;

public void DoWork()
{
do
{
//< do some work>
Progress(this, null);
}
while (!Canceled);
}
}
 
J

Jay B. Harlow [MVP - Outlook]

Rudi,
The project I am working where I plan on doing something similar to this I
was going to have my ClassA implement
System.ComponentModel.ISynchronizeInvoke.

From a high level (as I have not started coding yet):

My ClassA would have (at least) 4 member fields, the "owning" Thread (your
thread it was created on), a Queue, a "padlock", and an ResetEvent. The
Queue would be used to hold Method/Args pairs that needed to be invoked on
the main thread. Where Method is the delegate passed to
ISynchronizeInvoke.Invoke & Args is the array of objects passed to
ISynchronizeInvoke.Invoke. Secondary threads would put the Delegate & Object
array pair into the Queue, the main thread would read these pairs out &
invoke the Delegate via Delegate.DynamicInvoke...

Unfortunately I have not started coding this yet, so I have not worked out
all the details (the IAsyncResult object needed for BeginInvoke & EndInvoke,
as Invoke itself can be implemented by calling these two... More then likely
the Queue would hold instances of my class that implemented IAsyncResult...

The event handler could simply check to see if InvokeRequired, if it is it
would Invoke itself to "teleport" itself to the initial thread...

The trick is going to be does the owning thread then go to sleep waiting on
the event for requests to show up on the queue or does it occasionally call
a method on ClassA to see if there are requests to be made... My project the
owning thread would actually wait on the event for new requests.

Hope this helps
Jay
 
J

Jon Skeet [C# MVP]

Rudi said:
Is there anyone who can point me to a good tutorial on how to create a
multithreaded class library?

Have a look at
http://www.pobox.com/~skeet/csharp/multithreading.html

If you want to be able to effectively invoke a method in another
thread, that other thread is going to have to be polling a queue of
some kind, or something like that - you can't just tell an otherwise
occupied thread to execute some different code.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top