Solved

Wake me up in 500ms

Posted on 2006-07-09
6
249 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
Salesforce Made Easy to Use

On-screen guidance at the moment of need enables you & your employees to focus on the core, you can now boost your adoption rates swiftly and simply with one easy tool.

 
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

MS Dynamics Made Instantly Simpler

Make Your Microsoft Dynamics Investment Count  & Drastically Decrease Training Time by Providing Intuitive Step-By-Step WalkThru Tutorials.

Question has a verified solution.

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

Introduction Although it is an old technology, serial ports are still being used by many hardware manufacturers. If you develop applications in C#, Microsoft .NET framework has SerialPort class to communicate with the serial ports.  I needed to…
Performance in games development is paramount: every microsecond counts to be able to do everything in less than 33ms (aiming at 16ms). C# foreach statement is one of the worst performance killers, and here I explain why.
In this video we outline the Physical Segments view of NetCrunch network monitor. By following this brief how-to video, you will be able to learn how NetCrunch visualizes your network, how granular is the information collected, as well as where to f…
In this video you will find out how to export Office 365 mailboxes using the built in eDiscovery tool. Bear in mind that although this method might be useful in some cases, using PST files as Office 365 backup is troublesome in a long run (more on t…
Suggested Courses

627 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