private void addToTextBox(object o) { int ThreadId = Thread.CurrentThread.ManagedThreadId; Debug.WriteLine(" ThreadId=" + ThreadId + ", addTotextBox.CheckAccess=" + textBox1.Dispatcher.CheckAccess()); if (textBox1.Dispatcher.CheckAccess()) { textBox1.Text += o.ToString() + Thread.CurrentThread.ManagedThreadId + " "; } else { Delegate del = new TextBox1Delegate(addToTextBox); this.Dispatcher.BeginInvoke(del, o); } }
public void CallBack(IAsyncResult asRes) { AsyncResult result = (AsyncResult)asRes; CalcSampleDelegate calcSampleDel = (CalcSampleDelegate)result.AsyncDelegate; TextBox1Delegate textDel = new TextBox1Delegate(DisplayResults); try { double factor = calcSampleDel.EndInvoke(asRes); string sampleName = (string)result.AsyncState; textBox1.Dispatcher.Invoke(textDel, "\n" + sampleName.Remove(sampleName.LastIndexOf('.')) + "\t" + factor); } catch (Exception ex) { MessageBox.Show(ex.ToString()); } }