Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Wake me up in 500ms

Posted on 2006-07-09
6
Medium Priority
?
251 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 1200 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
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
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

Important Lessons on Recovering from Petya

In their most recent webinar, Skyport Systems explores ways to isolate and protect critical databases to keep the core of your company safe from harm.

Question has a verified solution.

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

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…
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
Add bar graphs to Access queries using Unicode block characters. Graphs appear on every record in the color you want. Give life to numbers. Hopes this gives you ideas on visualizing your data in new ways ~ Create a calculated field in a query: …
Sometimes it takes a new vantage point, apart from our everyday security practices, to truly see our Active Directory (AD) vulnerabilities. We get used to implementing the same techniques and checking the same areas for a breach. This pattern can re…
Suggested Courses

715 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