473,467 Members | 1,604 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Raise event inside a thread

Hi,
I hope u can help me

I asked this question before but I didn't explain it very well.

Let say I hvae a thread 'A', creates object 'O', and start thread 'B' to do
some work on 'O', OK?
I want thread 'B' to fire event of 'O'
BUT MUST THAT EVENT DONE BY THREAD 'A' not 'B'!!

I want the formal way, not a tricky one!

How can I do this?
[ any help, samples, links, keyword to serach will be appricated ]

plesase , i have a projct depends on this!

Waleed
Nov 17 '05 #1
5 3193
Waleed AlRashoud <Wa*************@discussions.microsoft.com> wrote:
I hope u can help me

I asked this question before but I didn't explain it very well.

Let say I hvae a thread 'A', creates object 'O', and start thread 'B' to do
some work on 'O', OK?
I want thread 'B' to fire event of 'O'
BUT MUST THAT EVENT DONE BY THREAD 'A' not 'B'!!

I want the formal way, not a tricky one!

How can I do this?
[ any help, samples, links, keyword to serach will be appricated ]

plesase , i have a projct depends on this!


Well, thread A will have to be waiting for work to do, which you can do
with a producer/consumer queue as shown half way down
http://www.pobox.com/~skeet/csharp/t...eadlocks.shtml

Then just make the event handler add the appropriate work item to the
queue, and make thread A the consumer of the queue.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 17 '05 #2
Thanks Jon,

it's full answer for my qustion , but I still couldn't explain it very well!

Again, let us make a new situation:
Thread 'A' is the main thread , will create Thread 'B' to do some work on
Object o, then thread 'A' should continu create threads 'C','D','...',.., and
so on, with max concurrent 10 for example, each thread do the smae job [ for
seprate object : oB,oC,oD,oE,..];
each thread finish its job,will raise event to be catched from thread 'A'..
to create a new object 'oJ' and create new thread 'J' to do some work. and so
on
What I'm trying to avoid is :
while(true){}
to check for each thread is't done or not, I don't think while(true) is a
good way!
...
I have seen something called ISynchronize.invoke, But i think it's working
only with UI-thread . is there any similar interface or something in non-UI
threads?
I hope so!

Thanks again


"Jon Skeet [C# MVP]" wrote:
Well, thread A will have to be waiting for work to do, which you can do
with a producer/consumer queue as shown half way down
http://www.pobox.com/~skeet/csharp/t...eadlocks.shtml

Then just make the event handler add the appropriate work item to the
queue, and make thread A the consumer of the queue.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too

Nov 17 '05 #3
Waleed AlRashoud <Wa*************@discussions.microsoft.com> wrote:
it's full answer for my qustion , but I still couldn't explain it very well!

Again, let us make a new situation:
Thread 'A' is the main thread , will create Thread 'B' to do some work on
Object o, then thread 'A' should continu create threads 'C','D','...',.., and
so on, with max concurrent 10 for example, each thread do the smae job [ for
seprate object : oB,oC,oD,oE,..];
each thread finish its job,will raise event to be catched from thread 'A'..
to create a new object 'oJ' and create new thread 'J' to do some work. and so
on
What I'm trying to avoid is :
while(true){}
to check for each thread is't done or not, I don't think while(true) is a
good way!
..
I have seen something called ISynchronize.invoke, But i think it's working
only with UI-thread . is there any similar interface or something in non-UI
threads?
I hope so!


If that's the only reason you need to effectively raise the event in
thread A, there's a much better approach - thread pooling. I've got a
sample custom thread pool in my miscutil library:
http://www.pobox.com/~skeet/csharp/miscutil

Otherwise you could make thread A wait on a monitor, and notify that
monitor each time a thread completes. You might potentially want to
have a counter which each thread increments (safely) to say how many
threads have finished, and which thread A decrements when it creates a
new thread. Effectively at that stage you've got a counting semaphore.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 17 '05 #4

Thanks Jon, good work with ur lib..!

I tested ur work with this addition, to chcek if event is cathed by main
thread [ as 'A' ] or in other new thread [ as 'X' ].
in [Test.cs] , I added these lines inside [static void TestThreadPool() ]
/// Start
pool.AfterWorkItem += new AfterWorkItmeHandler(Pool_After);
Console.WriteLine("Main Thread ID " +
Thread.currentThread.ManagedThreadId.ToString();
/// End

Then I created new static void method [Pool_After(... ,...) ] and wrote:
///Start
Console.WriteLine("Event Catched By " +
Thread.currentThread.ManagedThreadId.ToString());
///End

This is Output:
/// Start Output
Main Thread ID 9
Event Catched By 11
/// End output

So! event not catched by main Thread!
Is it a mistake I did using CustomThreadPool , Or threads not raiseing
events inside main thread?

Nov 17 '05 #5
Waleed AlRashoud <Wa*************@discussions.microsoft.com> wrote:
I tested ur work with this addition, to chcek if event is cathed by main
thread [ as 'A' ] or in other new thread [ as 'X' ].
in [Test.cs] , I added these lines inside [static void TestThreadPool() ]
/// Start
pool.AfterWorkItem += new AfterWorkItmeHandler(Pool_After);
Console.WriteLine("Main Thread ID " +
Thread.currentThread.ManagedThreadId.ToString();
/// End

Then I created new static void method [Pool_After(... ,...) ] and wrote:
///Start
Console.WriteLine("Event Catched By " +
Thread.currentThread.ManagedThreadId.ToString());
///End

This is Output:
/// Start Output
Main Thread ID 9
Event Catched By 11
/// End output

So! event not catched by main Thread!
No.
Is it a mistake I did using CustomThreadPool , Or threads not raiseing
events inside main thread?


No, they can't be. The point was that you can signal to the main thread
within the event, using monitors or a ManualResetEvent.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 17 '05 #6

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

Similar topics

8
by: Mark | last post by:
Hi, I'm looking for some ideas on how to build a very simple Event processing framework in my C++ app. Here is a quick background ... I'm building a multithreaded app in C++ (on Linux) that...
1
by: Dan Cimpoiesu | last post by:
I have a remoting object, derived from MarshalByRefComponent, that I instantiate on the client side, with Activator.GetObject. Can I receive events fired on the server, on the client? How?
4
by: Charles Law | last post by:
Suppose a worker thread needs to signal to the main thread that an event has occurred. Ordinarily, any event raised by the worker thread will be on its own thread. How can the worker thread...
2
by: Pietro | last post by:
Hello, somebody know how to raise an event from a nested class? I have two classes, the class1 with 1 events, and a nested class (class2) inside the class1. So... How can I raise class1 events...
12
by: Benny Raymond | last post by:
I understand that you would normally want to raise an event on the same thread so that your code blocks until the event has been dealt with... however i've run into a situation where I have a...
4
by: Pon | last post by:
Hi everybody, Simple question : Inside a class that has a shared method, how to raise an event on a given instance of this class from the shared method ?
3
by: wanwan | last post by:
I made a game with a window form that needs to record the mouse position to an array at 100 samples per second, so I use the mouse move event to do the job. The problem is the mouse move event...
2
by: Sin Jeong-hun | last post by:
Suppose class Engine do something in another thread and raise events. class Engine { Thread Worker; public event ... EngineMessage; public void Start() { Worker=new Thread(new...
11
by: nadeem_far | last post by:
Hello, I am working on a c# project using framework 1.1. Since its a console application,no windows forms are being used. I am trying to implement event driven classes that just want to raise...
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
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
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,...
0
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...
1
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
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,...
0
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 ...

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.