By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
455,467 Members | 1,448 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 455,467 IT Pros & Developers. It's quick & easy.

WaitAny( ), Timer and Event fires up problem

P: n/a
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.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using System.Timers;
using System.Threading;

namespace WindowsApplication1
{
/// <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.ComponentModel.Container components = null;

public ManualResetEvent evtButtonClick = new ManualResetEvent(false);
public ManualResetEvent evtPollTimer;
private System.Windows.Forms.StatusBar statusBar1;
public System.Timers.Timer hPollTimer;

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

//
// TODO: Add any constructor code after InitializeComponent call
//
}

/// <summary>
/// Clean up any resources being used.
/// </summary>
protected override void Dispose( bool disposing )
{
if( disposing )
{
if (components != null)
{
components.Dispose();
}
}
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 InitializeComponent()
{
this.button1 = new System.Windows.Forms.Button();
this.statusBar1 = new System.Windows.Forms.StatusBar();
this.SuspendLayout();
//
// button1
//
this.button1.Location = new System.Drawing.Point(104, 136);
this.button1.Name = "button1";
this.button1.TabIndex = 0;
this.button1.Text = "button1";
this.button1.Click += new System.EventHandler(this.button1_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.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
this.ClientSize = new System.Drawing.Size(292, 266);
this.Controls.Add(this.statusBar1);
this.Controls.Add(this.button1);
this.Name = "Form1";
this.Text = "Form1";
this.ResumeLayout(false);

}
#endregion

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

private void button1_Click(object sender, System.EventArgs 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.Copy(DummyWaitEvents, 0, WaitEvents, 0, iEventCount);

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

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

if (evtPollTimer == null)
{
hPollTimer = new System.Timers.Timer();
evtPollTimer = new ManualResetEvent(false);
}
if (flag)
{
SetWaitableTimer(ref hPollTimer, "Poll", 2, 0, null, null,
false);
}

continue;
}

if (evtPollTimer.WaitOne(0, false))
{
if (statusBar1.Text == "12345")
statusBar1.Text = "67890";
else
statusBar1.Text = "12345";
evtPollTimer.Reset();
}

}
}

void SetWaitableTimer(ref System.Timers.Timer hTimer, string
strTimerName, long lDueTime, long lPeriod,
string strCompletionRoutine,
string strArgToCompletionRoutine,
bool bResume)
{
if (lDueTime > 0)
{
hTimer.AutoReset = true;
}
else
{
hTimer.AutoReset = false;
}
hTimer.Interval = lDueTime*1000;
if (strTimerName.Equals("Poll"))
{
hTimer.Elapsed += new ElapsedEventHandler(this.OnPollTimedEvent);
evtPollTimer.Reset();
}
hTimer.Enabled = true;
hTimer.Start();
}

void OnPollTimedEvent(object source, System.Timers.ElapsedEventArgs
e)
{
//MessageBox.Show("Poll");
evtPollTimer.Set();
}
}
}

Apr 5 '06 #1
Share this question for a faster answer!
Share on Google+

This discussion thread is closed

Replies have been disabled for this discussion.