473,387 Members | 1,549 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,387 software developers and data experts.

Events between threads?

Hi All,
I hvae worker class to manage the application
inside worker : worker creates (n) number of threads to do IO operations.

The Problem Is:
to handle all threads : [create new] , [process finished] , [clean all]
I have to keep worker process inside while statement:
while (true)
...
and it takes 50% of cpu all time!
I'm wondering how can I use events to keep worker sleep until one of the
opetations threads finish?
NOTE: event must wake worker thread with event args.
AND NOTE : worker thread must be diffrent thread than IO threads.

Thank u
[ i'm using : C# 2005 beta 2 ]
Nov 17 '05 #1
7 1872
> I have to keep worker process inside while statement:
while (true)
..
and it takes 50% of cpu all time!


just call Thread.Sleep(...) inside the loop. this should help.
Wiktor Zychla
Nov 17 '05 #2
is this the way e.g. windows waits for process to finish?
or events is the way?

What about ThreadPool, does it do the trick?

"Wiktor Zychla" wrote:
I have to keep worker process inside while statement:
while (true)
..
and it takes 50% of cpu all time!


just call Thread.Sleep(...) inside the loop. this should help.
Wiktor Zychla

Nov 17 '05 #3
> is this the way e.g. windows waits for process to finish?

no, Thread.Sleep is not the way to wait for the worker thread to finish.

if the only purpose of the while () {...} loop is to wait for worker threads
then use the System.Threading.WaitHandle.WaitAll() method.
read the docs for further info on this method.

Wiktor Zychla
Nov 17 '05 #4
Thanks,

I have unlimited number of processes [broadcast]
so i need to create very dynamic ThreadPool,
When ever any thread finish a job , Main Thread has to take the result, then
inserts new thread , and so on,
But as i see in this example:
http://msdn2.microsoft.com/library/9...us,vs.80).aspx

1- array of WaitHandle must be decalred. Which very bad for my problem
2- I want to wait for each [ NOT FOR ALL, NOT FOR ANY] thread to finish to
process its result and create new thread.

I Hope u can help me further.
Thanks agian.

"Wiktor Zychla" wrote:
is this the way e.g. windows waits for process to finish?


no, Thread.Sleep is not the way to wait for the worker thread to finish.

if the only purpose of the while () {...} loop is to wait for worker threads
then use the System.Threading.WaitHandle.WaitAll() method.
read the docs for further info on this method.

Wiktor Zychla

Nov 17 '05 #5
> I have unlimited number of processes [broadcast]
so i need to create very dynamic ThreadPool,
When ever any thread finish a job , Main Thread has to take the result,
then
inserts new thread , and so on,
But as i see in this example:
http://msdn2.microsoft.com/library/9...us,vs.80).aspx

1- array of WaitHandle must be decalred. Which very bad for my problem
2- I want to wait for each [ NOT FOR ALL, NOT FOR ANY] thread to finish
to
process its result and create new thread.

I Hope u can help me further.
Thanks agian.


I see two possible routes you could try:
a) use one of thread synchronization objects, a mutex or an event to sync
the threads
b) use async delegates with async notifications (delegate.BeginInvoke( new
AsyncCallback( notification_routine ), ... ).

The latter solution could be an option when async operations should return
values for further processing. By using async delegates, you avoid dealing
with threads at all.

Wiktor Zychla
Nov 17 '05 #6
Thanks Wiktor

But what i know , using asynch and beginInvoke raise event in different
(new) thread, not the main thread, is that right?
How to overcome this problem?
thanks

"Wiktor Zychla" wrote:
I have unlimited number of processes [broadcast]
so i need to create very dynamic ThreadPool,
When ever any thread finish a job , Main Thread has to take the result,
then
inserts new thread , and so on,
But as i see in this example:
http://msdn2.microsoft.com/library/9...us,vs.80).aspx

1- array of WaitHandle must be decalred. Which very bad for my problem
2- I want to wait for each [ NOT FOR ALL, NOT FOR ANY] thread to finish
to
process its result and create new thread.

I Hope u can help me further.
Thanks agian.


I see two possible routes you could try:
a) use one of thread synchronization objects, a mutex or an event to sync
the threads
b) use async delegates with async notifications (delegate.BeginInvoke( new
AsyncCallback( notification_routine ), ... ).

The latter solution could be an option when async operations should return
values for further processing. By using async delegates, you avoid dealing
with threads at all.

Wiktor Zychla

Nov 17 '05 #7
Thanks Wiktor

But what i know , using asynch and beginInvoke raise event in different
(new) thread, not the main thread, is that right?
How to overcome this problem?
thanks
Nov 17 '05 #8

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: LP | last post by:
Hello! I am still transitioning from VB.NET to C#. I undertand the basic concepts of Delegates, more so of Events and somewhat understand AsyncCallback methods. But I need some clarification on...
3
by: Stampede | last post by:
Hi, I want to use the FileSystemWatcher in a Windows Service. I read an article, where the author created the FileSystemWatcher object in a seperate thread and when the event is fired, he started...
11
by: Nicky Smith | last post by:
Hello, I'm studying a book on VB.net Win apps, and I'm reading a section on events and delegates and raising events. Is it just me, or is this not just subs dressed up as something else? I...
15
by: Bryce K. Nielsen | last post by:
I have an object that starts a thread to do a "process". One of the steps inside this thread launches 12 other threads via a Delegate.BeginInvoke to process. After these 12 threads are launched,...
3
by: daan | last post by:
Hello, I have a problem and I can't get the solution for it :( I have a com dll, which i imported as a reference. The com object is part of a class which is multithreaded and will create...
14
by: Gotch | last post by:
Hi all, I've recently digged into C# and the whole .Net stuff. Particularly I found the idea of adding Events and Delegates to the C# language very interesting and I'm trying to use them in...
4
by: jehugaleahsa | last post by:
Hello: Is there a way to prevent one event from firing while another event is already being fired? I have a tool that extracts media from web pages and it has multiple events firing when the...
9
by: thiago777 | last post by:
Question details: VB .NET / threads / events / GUI Imagine the following situation: A method from object "A" creates "n" threads. Variables from these threads contains values that should...
0
by: thiago777 | last post by:
Hi! Im still trying to make my application work in an event-driven way. It had worked so far with my threads, but the problem Im having is only when modifying a GUI component from the event handler...
3
by: JohnM | last post by:
Hi there, Are there any specific rules or best-practices I should be aware regarding events and the threads they're fired on. Object 1 can be created on thread 1 for instance and an event then...
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: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
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: 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
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,...
0
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...
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,...

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.