我可以安全地在两个线程之间共享此“状态”对象吗?线程和UI之间的共享变量C#
private bool status = false;
private void uiNewThread_bootloaderStartIdSetupAuto()
{
while (status)
;
}
以上是新线程将从UI推出如下:
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;
}
}
在大多数CPU架构和.NET Framework,挥发性或多或少是多余的。乔达菲在这里讨论。 http://www.bluebytesoftware.com/blog/2010/12/04/SayonaraVolatile.aspx – stevethethread 2012-07-17 09:53:04
@stevethethread:在我的系统中,我无数次地重新创建了这个场景,并在.NET中获得了一个死锁。这里的问题是非常真实的。 – Tudor 2012-07-17 09:55:39
不幸的是,即使通常通过使用'volatile'来正常工作,一个'volatile write'也不能保证在宇宙热死之前完成写操作:-) – xanatos 2012-07-17 10:05:59