MultiThread Programing
-
I have to create a multithread program. It must control a pool of thread. How do I do this in C#? The idea is: Reading a properties file, the program will know what kind of thead It will start. Each thread will take care about one subject. Each thread will receive a parameter indicating how many thread will have in its pool. The MainThread will control the pool of thread, when it needs to process some UDP message will request a free thread in the pool. Will pass all of the information (data) to the thread and order to run. When the thread finishs its job will be in suspend mode waiting for next job. I had develop this kind of program in Delphi and in Java with success! But in C# I did not find any way to do this. Thanks. +------+ +----------+ | | |Thread-UDP|-------> +------+ | .EXE |-----> |LISTENER | | T2 | | | +----+-----+ +------+ +------+ | \ | \ \/ \ +-------+ +--------+ | T1 | | T3 + +-------+ +--------+
-
I have to create a multithread program. It must control a pool of thread. How do I do this in C#? The idea is: Reading a properties file, the program will know what kind of thead It will start. Each thread will take care about one subject. Each thread will receive a parameter indicating how many thread will have in its pool. The MainThread will control the pool of thread, when it needs to process some UDP message will request a free thread in the pool. Will pass all of the information (data) to the thread and order to run. When the thread finishs its job will be in suspend mode waiting for next job. I had develop this kind of program in Delphi and in Java with success! But in C# I did not find any way to do this. Thanks. +------+ +----------+ | | |Thread-UDP|-------> +------+ | .EXE |-----> |LISTENER | | T2 | | | +----+-----+ +------+ +------+ | \ | \ \/ \ +-------+ +--------+ | T1 | | T3 + +-------+ +--------+
-
I have to create a multithread program. It must control a pool of thread. How do I do this in C#? The idea is: Reading a properties file, the program will know what kind of thead It will start. Each thread will take care about one subject. Each thread will receive a parameter indicating how many thread will have in its pool. The MainThread will control the pool of thread, when it needs to process some UDP message will request a free thread in the pool. Will pass all of the information (data) to the thread and order to run. When the thread finishs its job will be in suspend mode waiting for next job. I had develop this kind of program in Delphi and in Java with success! But in C# I did not find any way to do this. Thanks. +------+ +----------+ | | |Thread-UDP|-------> +------+ | .EXE |-----> |LISTENER | | T2 | | | +----+-----+ +------+ +------+ | \ | \ \/ \ +-------+ +--------+ | T1 | | T3 + +-------+ +--------+
-
I have to create a multithread program. It must control a pool of thread. How do I do this in C#? The idea is: Reading a properties file, the program will know what kind of thead It will start. Each thread will take care about one subject. Each thread will receive a parameter indicating how many thread will have in its pool. The MainThread will control the pool of thread, when it needs to process some UDP message will request a free thread in the pool. Will pass all of the information (data) to the thread and order to run. When the thread finishs its job will be in suspend mode waiting for next job. I had develop this kind of program in Delphi and in Java with success! But in C# I did not find any way to do this. Thanks. +------+ +----------+ | | |Thread-UDP|-------> +------+ | .EXE |-----> |LISTENER | | T2 | | | +----+-----+ +------+ +------+ | \ | \ \/ \ +-------+ +--------+ | T1 | | T3 + +-------+ +--------+
-
I have to create a multithread program. It must control a pool of thread. How do I do this in C#? The idea is: Reading a properties file, the program will know what kind of thead It will start. Each thread will take care about one subject. Each thread will receive a parameter indicating how many thread will have in its pool. The MainThread will control the pool of thread, when it needs to process some UDP message will request a free thread in the pool. Will pass all of the information (data) to the thread and order to run. When the thread finishs its job will be in suspend mode waiting for next job. I had develop this kind of program in Delphi and in Java with success! But in C# I did not find any way to do this. Thanks. +------+ +----------+ | | |Thread-UDP|-------> +------+ | .EXE |-----> |LISTENER | | T2 | | | +----+-----+ +------+ +------+ | \ | \ \/ \ +-------+ +--------+ | T1 | | T3 + +-------+ +--------+
A while back I wrote a class named SynchronisedQueue[^] for parallel processing of Mandelbrot graphics. The main thread could use it to send the jobs to the threads by simply adding it to the queue. The threads would periodically try to get something from the queue while they are idle. The class for a thread could be something like:
public class WorkerThread {
public volatile bool Contine;
private SynchronisedQueue<Job> _jobs;public WorkerThread(SynchronisedQueue<Job> jobs) {
Contine = true;
_jobs = jobs;
}public void Run() {
while (Continue) {
Job job;
if (jobs.TryDequeue(out job)) {
// handle the job
} else {
Thread.Sleep(100);
}
}
}}
The main thread creates a queue, then creates the objects for the worker threads and sends the queue along to the constructor. If you need to send a result back from the worker thread to the main thread, you add another queue for the results. When the thread is done it just adds a result to the queue, and you have a timer in the main thread to look for results in the queue. The code for dispatching the jobs gets really simple. There is no requesting of threads and handling the situation of waiting for a free thread, just throw the jobs in the queue, and the first thread that gets free grabs it. :)
Despite everything, the person most likely to be fooling you next is yourself.
-
I have to create a multithread program. It must control a pool of thread. How do I do this in C#? The idea is: Reading a properties file, the program will know what kind of thead It will start. Each thread will take care about one subject. Each thread will receive a parameter indicating how many thread will have in its pool. The MainThread will control the pool of thread, when it needs to process some UDP message will request a free thread in the pool. Will pass all of the information (data) to the thread and order to run. When the thread finishs its job will be in suspend mode waiting for next job. I had develop this kind of program in Delphi and in Java with success! But in C# I did not find any way to do this. Thanks. +------+ +----------+ | | |Thread-UDP|-------> +------+ | .EXE |-----> |LISTENER | | T2 | | | +----+-----+ +------+ +------+ | \ | \ \/ \ +-------+ +--------+ | T1 | | T3 + +-------+ +--------+
Programming the Thread Pool in the .NET Framework[^]
Anyone who thinks he has a better idea of what's good for people than people do is a swine. - P.J. O'Rourke