473,725 Members | 1,980 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 2008
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
3846
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
5321
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
4886
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
1402
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
1934
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
1557
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
5352
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
1663
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
8888
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
8752
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
9257
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
9174
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
9111
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
8096
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...
0
6011
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();...
0
4517
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4782
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?

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.