Im currently trying to code very simple thing.
what it supposed to do:
- enter google
- wait 5 seconds
- search something.
now, the part I cant do is wait. There is thread sleep etc. but they stop GUI and makes my program unusable. I can also do it with timers开发者_C百科 but it isnt very effective way to do it, since in actual app there will be many waits... Are there anyway to do with it like 2-3 lines of code only, and without stopping GUI?
A timer is exactly what you want here - you want to say, "In 5 seconds, do X" (potentially executing in the UI thread) which is exactly what a timer does for you. If you want to encapsulate that in a single method which you can pass in an Action
and a TimeSpan
or whatever, that's fine - but a timer is definitely the way to go.
(The type of timer you want to use will depend on what thread you want the timer to fire on, etc.)
You need to do the work on a seprate thread so it dosent halt the GUI thread.
Thread worker = new Thread(dowork);
worker.Start();
精彩评论