473,480 Members | 1,587 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Wait For Multiple Events

Hello, Newsgroupians:

Again, I'm still migrating from Win32 to C#. I've a question regarding some
event handling in C#.

I have created an EventWaitHandle object and pass that to my worker thread.
When the EventWaitHandle is signaled, I would like my secondary thread to
exit. However, in the thread, I also have another event that I would like to
create, which would be much like a CreateWaitableTimer() in Win32.

In Win32, I could use WaitForMultipleEvents() in my while loop...

bool bDone = FALSE;
while (!bDone)
{
int nIndex = WaitForMultipleEvents(...);

if (nIndex == "Terminate Event") // Pseudocode here
{
bDone = true;
}
else if (nIndex == "Timer Event") // Pseudocode here
{
...
}
}

This would work for me, again, in Win32. Now, I'm trying to create the same
code in C#.

I have come up with a solution I believe is similar to my situation.

// This is my thread delegate
public static void Start(object obj)
{
if (obj.GetType() == typeof(EventWaitHandle))
{
EventWaitHandle ewhTerminateEvent = (EventWaitHandle)obj;
Timer t = new System.Threading.Timer(timerCallBack, null, 0, 1000);

ewhTerminateEvent.WaitOne();
}
}

// This is the timer delegate
public static void timerCallBack(object obj)
{
...
}

This, I believe would work; however, is it possible to create a timer that
will not have a callback function? Therefore, I could emulate the Win32
structure that I have described... (IE: I could use WaitHandle.WaitAny())?

Thank you all for your continued support; your help is always appreciated.
Trecius
Nov 21 '07 #1
3 7851
On 2007-11-21 09:49:02 -0800, Trecius <Tr*****@discussions.microsoft.comsaid:
[...]
This, I believe would work; however, is it possible to create a timer that
will not have a callback function? Therefore, I could emulate the Win32
structure that I have described... (IE: I could use WaitHandle.WaitAny())?
Yes, WaitHandle.WaitAny() is pretty much the equivalent of
WaitForMultipleEvents().

That said, I don't really understand the implementation you've
suggested in either case. If you've got some sort of processing that
can be done on the tick of a timer, then why do you need the extra
thread at all? You can set up the timer, let it run until you would
have it stop, and then stop it. All of that can be done in the main
thread, without the need to explicitly start any additional thread and
without using waitable events at all.

Pete

Nov 21 '07 #2
Thank you, Mr. Duniho. I'll probably do your suggestion and use the lock
keyword.

Also, although I will be doing the System.Timers.Timer to implement my
project as you have suggested, I still have that question: is it possible to
create a timer that will not call a callback function upon ticking? Instead,
it will just set it's event, so I can use it in WaitAny()?

Trecius

"Peter Duniho" wrote:
On 2007-11-21 09:49:02 -0800, Trecius <Tr*****@discussions.microsoft.comsaid:
[...]
This, I believe would work; however, is it possible to create a timer that
will not have a callback function? Therefore, I could emulate the Win32
structure that I have described... (IE: I could use WaitHandle.WaitAny())?

Yes, WaitHandle.WaitAny() is pretty much the equivalent of
WaitForMultipleEvents().

That said, I don't really understand the implementation you've
suggested in either case. If you've got some sort of processing that
can be done on the tick of a timer, then why do you need the extra
thread at all? You can set up the timer, let it run until you would
have it stop, and then stop it. All of that can be done in the main
thread, without the need to explicitly start any additional thread and
without using waitable events at all.

Pete

Nov 21 '07 #3
On 2007-11-21 11:33:01 -0800, Trecius <Tr*****@discussions.microsoft.comsaid:
Thank you, Mr. Duniho. I'll probably do your suggestion and use the lock
keyword.

Also, although I will be doing the System.Timers.Timer to implement my
project as you have suggested, I still have that question: is it possible to
create a timer that will not call a callback function upon ticking? Instead,
it will just set it's event, so I can use it in WaitAny()?
All the timer classes I'm aware of (all three of them) use an event to
notify the client that the timer period has elapsed.

If you want a sort of timed event, a common technique is to just use
one event, and wait with a timeout value. Then when the thread returns
from the wait, it can check to see if the event is actually set or not
and behave appropriately.

Pete

Nov 22 '07 #4

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

Similar topics

29
4179
by: Paul L. Du Bois | last post by:
Has anyone written a Queue.Queue replacement that avoids busy-waiting? It doesn't matter if it uses os-specific APIs (eg WaitForMultipleObjects). I did some googling around and haven't found...
2
10909
by: news.hp.com | last post by:
I have situation where I need to copy multiple records (only certain fields) from a Rules table to an Events table based on a selection identified in a combo box. When the selection is made in a...
9
1702
by: Roger Down | last post by:
Lets say I have a method UpdateCache() called from a single thread. I also have a method GetCache() called from multiple threads. When UpdateCache() is called, the cache updating is being...
6
2424
by: SP | last post by:
Hi, I want to add wait cursor code whenever page is post back. Page may be post back on my user control's or on change of dropdown or on click of any button on page. so is there any common...
15
6600
by: Iced Crow | last post by:
In C# I know that you can use delegates to assing multiple addresses of sub and functions to a delegate and have it fire multiple procedures... How do I do this in VB? I only know of assigning...
16
30016
by: Thirsty Traveler | last post by:
I would like to create a test harness that simulates multiple concurrent users executing an individual thread. I would like this to be determined at runtime when the user specifies the number of...
6
2952
by: Michael D. Ober | last post by:
I have an application that needs to limit the number of simultaneous threads that are executing at any given time. The threadpool appears to be ideal for this. However, I need to have my Sub Main...
9
3162
by: Gummy | last post by:
Hello, I created a user control that has a ListBox and a RadioButtonList (and other stuff). The idea is that I put the user control on the ASPX page multiple times and each user control will...
14
21700
by: stephen.durkin | last post by:
I've gathered that javascript doesn't have a handy wait() function, so I made the following function makeDelay() to make use of PHP's sleep(), which then does the waiting, and responds with...
0
7044
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
6908
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
7087
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
6741
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
5341
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,...
1
4782
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...
0
4483
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...
1
563
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
182
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

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.