northTiger
northTiger

Reputation: 253

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?
UI thread or a Thread Pool thread.

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

Upvotes: 3

Views: 461

Answers (1)

Marc Gravell
Marc Gravell

Reputation: 1064204

You are calling BeginInvoke on the delegate, so it will be a pool thread. If you called BeginInvoke on a control it would be the UI thread.

It is unfortunate that BeginInvoke means almost the exact opposite in these two scenarios.

Upvotes: 4

Related Questions