What thread calls the BeginInvoke asynchronous delegate callback?

What thread should the Asynchronous Delegate BeginInvoke callback be called?
UI or thread thread thread.

eg

private void button1_Click(object sender, EventArgs e)
{
    Func<string> func1 = LoadingDada;
    func1.BeginInvoke(IsDone, func1);
}


string LoadingDada()
{
    Thread.Sleep(10000);  //simulated a long running
    x = Thread.CurrentThread.Name;
    return "str_100000";
}

string IsDone(IAsyncResult a) 
{
    var loadingDataReturn = (Func<string>)a.AsyncState;
    string rr = loadingDataReturn.EndInvoke(a);

    textBox1.Text = rr;
} 
+3
source share
1 answer

You are invoking BeginInvokein the delegate, so it will be a pool thread. If you called BeginInvokein the control, that would be the UI thread.

Unfortunately, it BeginInvokemeans an almost complete opposite in these two scenarios.

+4
source

Source: https://habr.com/ru/post/1745689/


All Articles