By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
457,714 Members | 1,318 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 457,714 IT Pros & Developers. It's quick & easy.

Howto? Create a multithreaded class library

P: n/a
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);
}
}
Jul 21 '05 #1
Share this Question
Share on Google+
2 Replies


P: n/a
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
"Rudi" <no****@nomail.com> wrote in message
news:%2****************@TK2MSFTNGP09.phx.gbl...
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);
}
}

Jul 21 '05 #2

P: n/a
Rudi <no****@nomail.com> wrote:
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.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Jul 21 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.