473,395 Members | 1,460 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

Howto? Create a multithreaded class library

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
2 1971
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: Logan | last post by:
Several people asked me for the following HOWTO, so I decided to post it here (though it is still very 'alpha' and might contain many (?) mistakes; didn't test what I wrote, but wrote it - more or...
7
by: Sidd | last post by:
Hi, I tried finding and example of multithreaded client-serve program in python. Can any one please tell me how to write a multithreaded client-server programn in python such that 1.It can handle...
4
by: Digital Fart | last post by:
howto make a connection to database available in my classes. What is the best practice when i want to write classes that need a connection to the database? Do i make a conn variable in my...
1
by: Chris | last post by:
As I understand it, I could create a class library, and then have some of my ASPX pages inherit from classes in the class library (the web application would have a reference to the class library...
2
by: tinman | last post by:
Hi... I would like to achive the following object model in VB.NET.....not too sure how to achieve this but in VB6 I can declare the Project as PublicNotCreatable so as to prevent external...
2
by: Rudi | last post by:
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...
0
by: Benjamin | last post by:
I am attempting to create multiple itterators for a custom class that I have created. Basically I have a class that I am populating using XML de-serialization. I want to be able to loop through the...
4
by: Bernd Muent | last post by:
Hi together, I made a DLL in the following way (simplified example just to explain what is the problem) using devcpp and gcc compiler on Windows XP: dll.h: #ifndef _DLL_H_ #define _DLL_H_ #if...
3
by: Guillaume Hanique | last post by:
Hi, I feel very stupid. I simply want to derive a control from system.web.ui.webcontrols.button and use that on my webform, but I just can't get it done. Can anyone tell me how to do that? I...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.