I want to call asynchronous methods, but I don't want use a timer. How can I substitute the timer? I need the invoker that substitute the timer.
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) ??
Take a look at the following link. I think it has just the answer to your question.
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.
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.