473,799 Members | 2,772 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

WaitAny( ), Timer and Event fires up problem

Hi, Pals,

I am using WaitAny( ) to wait for the events triggered by a timer. But
it seems like that when the timer's interval elapsed, and the event was
set, but WaitAny is still waiting there.

Basically, I want the statusBar's text change every 2 seconds. But
since the WaitAny( ) doesn't stop, the statusBar's text changes every
10 seconds which is not what I want.

Thanks in advance.

Willie

The whole code is here:

using System;
using System.Drawing;
using System.Collecti ons;
using System.Componen tModel;
using System.Windows. Forms;
using System.Data;
using System.Timers;
using System.Threadin g;

namespace WindowsApplicat ion1
{
/// <summary>
/// Summary description for Form1.
/// </summary>
public class Form1 : System.Windows. Forms.Form
{
private System.Windows. Forms.Button button1;
/// <summary>
/// Required designer variable.
/// </summary>
private System.Componen tModel.Containe r components = null;

public ManualResetEven t evtButtonClick = new ManualResetEven t(false);
public ManualResetEven t evtPollTimer;
private System.Windows. Forms.StatusBar statusBar1;
public System.Timers.T imer hPollTimer;

public Form1()
{
//
// Required for Windows Form Designer support
//
InitializeCompo nent();

//
// TODO: Add any constructor code after InitializeCompo nent call
//
}

/// <summary>
/// Clean up any resources being used.
/// </summary>
protected override void Dispose( bool disposing )
{
if( disposing )
{
if (components != null)
{
components.Disp ose();
}
}
base.Dispose( disposing );
}

#region Windows Form Designer generated code
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeCompo nent()
{
this.button1 = new System.Windows. Forms.Button();
this.statusBar1 = new System.Windows. Forms.StatusBar ();
this.SuspendLay out();
//
// button1
//
this.button1.Lo cation = new System.Drawing. Point(104, 136);
this.button1.Na me = "button1";
this.button1.Ta bIndex = 0;
this.button1.Te xt = "button1";
this.button1.Cl ick += new System.EventHan dler(this.butto n1_Click);
//
// statusBar1
//
this.statusBar1 .Location = new System.Drawing. Point(0, 244);
this.statusBar1 .Name = "statusBar1 ";
this.statusBar1 .Size = new System.Drawing. Size(292, 22);
this.statusBar1 .TabIndex = 3;
this.statusBar1 .Text = "12345";
//
// Form1
//
this.AutoScaleB aseSize = new System.Drawing. Size(5, 13);
this.ClientSize = new System.Drawing. Size(292, 266);
this.Controls.A dd(this.statusB ar1);
this.Controls.A dd(this.button1 );
this.Name = "Form1";
this.Text = "Form1";
this.ResumeLayo ut(false);

}
#endregion

/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main()
{
Application.Run (new Form1());
}

private void button1_Click(o bject sender, System.EventArg s e)
{
evtButtonClick. Set();
bool flag = false;

int iEventCount = 0;

evtPollTimer = null;
while (true)
{
WaitHandle[] DummyWaitEvents = new WaitHandle[2];
iEventCount = 0;
DummyWaitEvents[iEventCount] = evtButtonClick;
if (flag)
DummyWaitEvents [++iEventCount] = evtPollTimer;

WaitHandle[] WaitEvents = new WaitHandle[iEventCount];
System.Array.Co py(DummyWaitEve nts, 0, WaitEvents, 0, iEventCount);

int iIndex = WaitHandle.Wait Any(WaitEvents, 10000, true);

if (evtButtonClick .WaitOne(0, false))
{
evtButtonClick. Reset();
flag = true;

if (evtPollTimer == null)
{
hPollTimer = new System.Timers.T imer();
evtPollTimer = new ManualResetEven t(false);
}
if (flag)
{
SetWaitableTime r(ref hPollTimer, "Poll", 2, 0, null, null,
false);
}

continue;
}

if (evtPollTimer.W aitOne(0, false))
{
if (statusBar1.Tex t == "12345")
statusBar1.Text = "67890";
else
statusBar1.Text = "12345";
evtPollTimer.Re set();
}

}
}

void SetWaitableTime r(ref System.Timers.T imer hTimer, string
strTimerName, long lDueTime, long lPeriod,
string strCompletionRo utine,
string strArgToComplet ionRoutine,
bool bResume)
{
if (lDueTime > 0)
{
hTimer.AutoRese t = true;
}
else
{
hTimer.AutoRese t = false;
}
hTimer.Interval = lDueTime*1000;
if (strTimerName.E quals("Poll"))
{
hTimer.Elapsed += new ElapsedEventHan dler(this.OnPol lTimedEvent);
evtPollTimer.Re set();
}
hTimer.Enabled = true;
hTimer.Start();
}

void OnPollTimedEven t(object source, System.Timers.E lapsedEventArgs
e)
{
//MessageBox.Show ("Poll");
evtPollTimer.Se t();
}
}
}

Apr 5 '06 #1
0 1662

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

Similar topics

1
19139
by: pokémon | last post by:
Can someone please post a working example of how to use the WaitAny() method. Thanks.
3
3512
by: Pietje de kort | last post by:
Hello all, I want to implement Timeout behaviour in a class of mine. When a method is called it should timeout after a few seconds. To do this I've built a System.Threading.Timer that calls a delegate method after x milli's, the called delegate throws an exception indicating timeout. The big problem is my code will not catch the thrown exception because it
6
4127
by: roger beniot | last post by:
I have a program that launches multiple threads with a ThreadStart method like the following (using System.Net.Sockets.Socket for UDP packet transfers to a server): ThreadStart pseudo code: Connect Receive response Send Connect ACK
1
4513
by: Dinesh Eswaran | last post by:
hi all, I want to create a waitable timer in C#. How do I do it? There is Timer class and there is the WaitHandle class with WaitAny... methods. But they dont seem to help. Basically iam looking for the C# version of platform SDK's SetWaitableTimer Thanks Dinesh
3
2339
by: Kenny | last post by:
I am running a windows service that takes actions based on a couple System.Threading.Timers. The intervals are usually short... based on the time of day, anywhere between 1 and 5 minutes. Recently however, the event that was firing based on the timer started firing rapidly; it fired about 9000 times in a minute and a half. After the minute and a half was up, it returned to normal behavior! .... just wondering if anyone has experienced...
6
2296
by: Daniel | last post by:
i have an array that i want all threads to be able to READ from concurrently, however, at times i want to UPDATE the array. at which point i want all threads that use it to block when they try to read from the array. currently i do this by wrapping ALL READ AND WRITE access to the array in a lock("foobar"){} this prevernts threads from reading from the array while it is being written to however it ALSO keeps more then one thread from...
6
1388
by: Jon | last post by:
I have a service program that creates a crystal report and prints the report to a named printer driver. The printer driver raises an event when it is finished. I am supposed to trap for the following event to determine when I can continue with my code flow. Event: %printername%.mfx.complete Can anyone help me out with some sample code to loop and wait for this event? I found one example in C#, but I'm not having much luck getting to...
3
7902
by: =?Utf-8?B?VHJlY2l1cw==?= | last post by:
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.
1
2325
by: subhalalitha | last post by:
There is a requirement in our system. There is one Transaction Monitoring thread which wakes up after every 30 sec and checks for the requests that didnot get responses for "2 min" and sends Timeout Responses to the caller. There is a need to wake up this Thread upon an "event" to send Timeout responses for all the Requests. I tried samples and this combination does not seem to work for me. In short , The thread should wake up...
0
10495
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10032
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
9085
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
6811
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
5469
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
5597
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4148
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 we have to send another system
2
3764
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2942
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 can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.