开发者

Substitute the timer

开发者 https://www.devze.com 2022-12-09 23:25 出处:网络
I want to call asynch开发者_开发技巧ronous methods, but I don\'t want use a timer. How can I substitute the timer? I need the invoker that substitute the timer.You should look into the .NET Asynchrono

I want to call asynch开发者_开发技巧ronous methods, but I don't want use a timer. How can I substitute the timer? I need the invoker that substitute the timer.


You should look into the .NET Asynchronous Programming Model. It's much simpler then it sounds. You simply create a delegate to a method that you want to run asynchronously. Then call BeginInvoke on the delegate and your method is automatically run on a background thread.

In the asynchronous method, if you need to update the UI, you must use Control.Invoke/BeginInvoke on the relevant form or control.

Also, if the asynchronous method needs to return some result to the UI thread, you will need to utilise the IAsyncResult (returned from BeginInvoke) and the EndInvoke method.

See the article Calling Synchronous Methods Asynchronously for more details.

On the other hand, if you're just looking for an equivalent to the javascript method window.settimeout() in Windows.Forms the following avoids threads entirely by wrapping the standard Timer in a re-usable method:

public void SetTimeout(EventHandler doWork, int delayMS)
{

    System.Windows.Forms.Timer timer = new System.Windows.Forms.Timer();

    timer.Interval = delayMS; 

    timer.Tick += delegate(object s, EventArgs args) { timer.Stop(); };

    timer.Tick += doWork;

    timer.Start();

}

then to call it:

    SetTimeout(delegate { YourMethod(); }, 100);

Just like the Timer you drag onto a form, YourMethod() is run on the UI thread by the form message loop. Therefore you do not need to worry about BeginInvoke/Invoke at all.


You don't need a timer to invoke asynchronous methods... There are numerous other mechanisms availble to do that. A timer is used to invoke something at a specific time, or recurrently on some specific interval. Which is your requirement, to call something asynchronously, or to call something at specific time(s) ??


Control.BeginInvoke() is your friend if your app has a GUI. You give it a delegate to your function and .NET will call after the current event handler finished executing.

NB: As opposed to Delegate.BeginInvoke(), Control.BeginInvoke() executes in the main thread (actually the thread where the control was created).

In most cases, I personally prefer a deffered execution in the same thread to an async execution in a worker thread: Much less possible side effects. Your mileage may vary.

void f(string s)
{
  Debug.WriteLine(s + " -> Thread = " + 
    System.Threading.Thread.CurrentThread.Name);
}

private void button_Click(object sender, EventArgs e)
{
  Debug.WriteLine("Start button_Click");
  f("straight call in button_Click");
  BeginInvoke(new Action<string>(f), "async call through BeginInvoke()");
  Debug.WriteLine("End button_Click");
}

Output:

Start button_Click
straight call in button_Click -> Thread = Main Thread
End button_Click
async call through BeginInvoke() -> Thread = Main Thread

Note: the name "Main Thread" is assigned to the thread in the Main() of the program.


While the other answers are sufficient it may be worthwhile having a look at the article below. It demonstrates in c# a generic polling component that runs at a specified interval and uses a background thread to perform the user action specified.

Sample usage:

IPoller poller = new UrlPoller(args[0], TimeSpan.FromSeconds(7));
IPolling pollingComponent = new Polling.Core.Polling(poller);
pollingComponent.SubscribeForPollingUpdates(PollingAction);
pollingComponent.Start();

For the code and complete sample see:

http://www.avantprime.com/blog/24/an-example-of-repeating-code-using-a-worker-thread-without-using-timers-c

0

精彩评论

暂无评论...
验证码 换一张
取 消