Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations biv343 on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Using timer to control an on and off state

Status
Not open for further replies.

robUK2

Programmer
Mar 10, 2008
57
TH
Hello,

VS 2008

I have the following code in my timer control

Code:
private void tmrVibrate_Tick(object sender, EventArgs e)
        {
            vibrateAlert = new Led();
            vibrateAlert.SetLedStatus(1, Led.LedState.On);
            Debug.WriteLine("Vibrating");
            Thread.Sleep(1000);
            vibrateAlert.SetLedStatus(1, Led.LedState.Off);
        }
I have 2 buttons start and stop. Start will enable the timer and stop will disable the timer.

The problem was that the PDA would continuely vibrate. So I put it in the timer event and set the interval for 2 seconds. Then in the timer event I sleep for 1 second. This will vibrate it for 1 second then switch it off and do this every 2 seconds.

The problem now is, that the UI will freeze during the pause of 1 second.

Is there a better way to do this?

Many thanks for any advice,
 
I'm not too familiar with the compact framework, but you might be able to put this workload onto another thread instead of executing it in the main UI thread.

Hope this helps,

Alex

[small]----signature below----[/small]
Majority rule don't work in mental institutions

My Crummy Web Page
 
Hello,

I solved the problem using the code below and setting the boolean flag in the timer tick event. However, as the timer is enabled in a thread that is not the UI thread, when I set the timer to enabled to true, the timer doesn't start. I was thinking of invoking the timer. However, the timer doesn't have that property.

Code:
private int OnIncomingCall(int _callID, string _caller)
        {
            callID = _callID;
            caller = _caller;       

            if (this.btnAnswer.InvokeRequired)
            {
                this.btnAnswer.Invoke(new EventHandler(UpdateAnswerButton));
            }
            
            if (this.lblCallStatus.InvokeRequired)
            {
                this.lblCallStatus.Invoke(new EventHandler(DisplayCaller));
            }
            this.tmrVibrate.Enabled = true; //Start the timer to enable vibration.
          
            drvAnswerCall(_callID, 180); // send: RINGING
            
            return 1;
        }


Code:
//Timer to oscillate the vibration in the PDA for an incoming call alert.
        //Timer is disabled when the user answers the call.
        static Boolean ledStatus = true; //Status of when to turn on and off the vibration
        private void tmrVibrate_Tick(object sender, EventArgs e)
        {
            if (ledStatus == true)
            {
                vibrateAlert.SetLedStatus(1, Led.LedState.On);
                ledStatus = false;
            }
            else
            {
                vibrateAlert.SetLedStatus(1, Led.LedState.Off);
                ledStatus = true;
            }
        }

Many thanks for any more suggestions,
 
Do you need to explicitly Start() the timer?

Its' late but I'll think some more on this tomorrow.

Can you show a little more code so we can see how your threads are working?

Alex

[small]----signature below----[/small]
Majority rule don't work in mental institutions

My Crummy Web Page
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top