Solved

Wake me up in 500ms

Posted on 2006-07-09
6
243 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
Comment Utility
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
Comment Utility
sorry, the namespace for that timer object is:
System.Threading.Timer .
0
 

Author Comment

by:PHILIPS_MED
Comment Utility
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
6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

 
LVL 85

Expert Comment

by:Mike Tomlinson
Comment Utility
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
Comment Utility
correct.
0
 
LVL 11

Expert Comment

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

Featured Post

Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

Join & Write a Comment

This article is for Object-Oriented Programming (OOP) beginners. An Interface contains declarations of events, indexers, methods and/or properties. Any class which implements the Interface should provide the concrete implementation for each Inter…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
It is a freely distributed piece of software for such tasks as photo retouching, image composition and image authoring. It works on many operating systems, in many languages.
You have products, that come in variants and want to set different prices for them? Watch this micro tutorial that describes how to configure prices for Magento super attributes. Assigning simple products to configurable: We assigned simple products…

763 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

10 Experts available now in Live!

Get 1:1 Help Now