非同期のOnMsgRecieved呼び出しで、値をコントロールに直接割り当てると、コントロールに機能しません。 その後、スレッドが安全でないことが判明し、問題を解決するためにコードを取得しました。 これは機能しています。しかし、私はそれが実際に何をしているのか分かりません。誰も私にそれを完全に理解させることができますか? コードは: -Windowsフォームスレッドではどういうことが起こっていますか?
public void listener_OnMsgRecieved(string aResponse)
{
ShowResponseMessage(aResponse);
}
public void ShowResponseMessage(string aResponse)
{
// InvokeRequired required compares the thread ID of the
// calling thread to the thread ID of the creating thread.
// If these threads are different, it returns true.
if (this.listBox.InvokeRequired)
{
SetTextCallback d = new SetTextCallback(ShowResponseMessage);
this.Invoke(d, new object[] { aResponse });
}
else
{
this.listBox.Items.Add(aResponse);
label.Text = "Response received from Server :";
}
}
同期コンテキストに関連するものはありますか? – userGS
ありがとう、実用的な説明! –