Solved

Wake me up in 500ms

Posted on 2006-07-09
6
244 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
  • 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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 85

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Introduction This article series is supposed to shed some light on the use of IDisposable and objects that inherit from it. In essence, a more apt title for this article would be: using (IDisposable) {}. I’m just not sure how many people would ge…
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
This tutorial demonstrates a quick way of adding group price to multiple Magento products.
This video explains how to create simple products associated to Magento configurable product and offers fast way of their generation with Store Manager for Magento tool.

914 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

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now