Solved

Wake me up in 500ms

Posted on 2006-07-09
6
248 Views
Last Modified: 2010-04-16
Hi,
I have a requirement that when a button is clicked, I should wait 500ms and than open a message box.
However, when i use the Thread.Sleep(500) method, the GUI get freezed.

So what I need is some sort of mechanism that I supply a callback function to be called after a certain amount of milliseconds.

I know I can use timers, but I don't want to add a timer to my class and activate it, just for that delay.

So is there a function like this: InvokeAfterDelay(Delegate callback, int milliseconds) ?
0
Comment
Question by:PHILIPS_MED
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
6 Comments
 
LVL 11

Accepted Solution

by:
vo1d earned 300 total points
ID: 17067980
if you use the sleep method, the app running thread is paused.

if you dont want the control timer object, use the Thread.Timer object.
it is a timer object, which comes from the threadpool.
implement that timer object in your app, which interval is set to 500.
in the click eventhandler of your button, start the timer.
in the delegate of your timer, stop the timer first and show the messagebox.
this timer is a lightweight timer and needs nearly no resources.

0
 
LVL 11

Expert Comment

by:vo1d
ID: 17067994
sorry, the namespace for that timer object is:
System.Threading.Timer .
0
 

Author Comment

by:PHILIPS_MED
ID: 17068323
Hi vo1d,
If I understood you correctly, this is what I should put on the button.Click event handler:

new System.Threading.Timer(new TimerCallback(ShowMessageBox), null, 500, Timeout.Infinite);

And the timer will automatically stop and dispose itself?
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 86

Expert Comment

by:Mike Tomlinson
ID: 17068552
For longer delays, you can use code like below (VB.Net example):

    Private Sub Delay(ByVal DelayInSeconds As Integer)
        Dim ts As TimeSpan
        Dim targetTime As DateTime = DateTime.Now.AddSeconds(DelayInSeconds)
        Do
            ts = targetTime.Subtract(DateTime.Now)
            Application.DoEvents() ' keep app responsive
            System.Threading.Thread.Sleep(50) ' reduce CPU usage (keep value small to avoid lockups)
        Loop While ts.TotalSeconds > 0
    End Sub
0
 
LVL 11

Expert Comment

by:vo1d
ID: 17068578
correct.
0
 
LVL 11

Expert Comment

by:vo1d
ID: 17068583
your ShowMessageBox should look like this:
public void ShowMessageBox(Object stateInfo)
        {            
            MessageBox.Show("whatever");
        }
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

This article describes a simple method to resize a control at runtime.  It includes ready-to-use source code and a complete sample demonstration application.  We'll also talk about C# Extension Methods. Introduction In one of my applications…
This article aims to explain the working of CircularLogArchiver. This tool was designed to solve the buildup of log file in cases where systems do not support circular logging or where circular logging is not enabled
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…

734 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question