我能安全地在这两个线程之间共享这个“状态”对象吗?
以下是将从下面的用户界面启动的新线程:
private bool status = false;
private void uiNewThread_bootloaderStartIdSetupAuto()
{
while (status)
;
}
以下是将从下面的用户界面启动的新线程:
private void uiBtnBootloaderStartIdSetupAuto_Click(object sender, EventArgs e)
{
if (MessageBox.Show("ID will be setup starting from 1 to 16. \n\nAfter pressing 'YES', press the orange button one-by-one on the nodes.\nThe first pressed node will have number 1, the next number 2, and so on... \n\nWhen done, hit DONE button.", "ID setup", MessageBoxButtons.YesNo) == DialogResult.Yes)
{
status = true;
Thread transmitConfig = new Thread(new ThreadStart(uiNewThread_bootloaderStartIdSetupAuto)); //close port in new thread to avoid
transmitConfig.Start();
}
else
{
Log(LogMsgType.Normal, "User cancelled");
status = false;
}
}
volatile
一切都正常,但volatile write
并不能保证写操作在宇宙热寂之前完成 :-) - xanatos