简体   繁体   English

哪个线程是一个BeginInvoke的异步委托的回调?

[英]which thread a BeginInvoke's callback of a asynchronous delegate is in?

Which thread a BeginInvoke's callback of a asynchronous delegate is supposed to be in? BeginInvoke对异步委托的回调应该在哪个线程?
UI thread or a Thread Pool thread. UI线程或线程池线程。

for example 例如

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;
} 

You are calling BeginInvoke on the delegate , so it will be a pool thread. 您在BeginInvoke 调用BeginInvoke ,因此它将是一个池线程。 If you called BeginInvoke on a control it would be the UI thread. 如果在控件上调用BeginInvoke 则它将是UI线程。

It is unfortunate that BeginInvoke means almost the exact opposite in these two scenarios. 不幸的是, BeginInvoke在这两种情况下几乎完全相反。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM