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);
}
}
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);
}
}