tags:

views:

46

answers:

1

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;
} 
+3  A: 

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.

Marc Gravell
@Mark, I can update UI Control in IsDone(). so it is in UI thread?
northTiger