473,756 Members | 3,499 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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.BeginIn voke, but
how do I do this in a normal Class?

<simplified code sample>

using System;
using System.Threadin g;

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.D oWork));
b.Progress += new EventHandler(b_ Progress);

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

private void b_Progress(obje ct 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 2012
Rudi,
The project I am working where I plan on doing something similar to this I
was going to have my ClassA implement
System.Componen tModel.ISynchro nizeInvoke.

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
ISynchronizeInv oke.Invoke & Args is the array of objects passed to
ISynchronizeInv oke.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.Dynami cInvoke...

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******** ********@TK2MSF TNGP09.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.BeginIn voke, but how do I do this in a normal Class?

<simplified code sample>

using System;
using System.Threadin g;

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.D oWork));
b.Progress += new EventHandler(b_ Progress);

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

private void b_Progress(obje ct 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.co m>
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
3848
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 less - during my own installation of Python 2.3 on Fedora Core 1 Linux for a friend of mine). Anyway, HTH, L.
7
5322
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 multiple connections 2.It uses actual threads and not select() or some other function
4
4888
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 main() and give it as a parameter to every object i make that needs access to the database ex.
1
1404
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 DLL). But is it possible to make this work nicely in VS.NET, the way the default code behind pages work? I'm guessing not. I would like to be able to use the designer, drag a new button onto
2
1939
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 instancing of the Project class.... Example: Employee
2
341
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 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?
0
1558
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 class using a foreach loop. I realize that in order to do this I need to create my own GetEnumerator that implements the movenext and current methods. The problem that I have is that I have been able to implement this for only one of the classes...
4
5353
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 BUILDING_DLL # define DLLIMPORT __declspec (dllexport) #else /* Not BUILDING_DLL */
3
1664
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 can create my own usercontrol (Menu: Website Add new item... Web User Control). This creates an aspx and an aspx.vb file in the root folder, which inherits nicely from system.web.ui.usercontrol. But I want to inherit...
0
9456
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9273
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10032
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
9841
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9711
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8712
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7244
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6534
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
1
3805
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system

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.