我有一些示例源代码,下面有2个锁定语句.在我的测试运行期间,我点击'r1RefreshButton'将showReport1方法放入一个永久循环中.然后我点击'r2RefereshButton'来检查锁是否按预期进行.
我发现showReport2上的锁并没有真正锁定.我错过了什么?
public partial class TestReportsForm : Form
{
private static readonly Object thisLock = new Object();
public TestReportsForm() { InitializeComponent(); }
private void showReport1()
{
lock (thisLock)
{
r1RefreshButton.Enabled = false;
try
{
// while loop is used to simulate long process
while (true)
{
Application.DoEvents();
}
}
catch (Exception ex)
{
String errorMessage = "Error encountered\n\n" +
"Error details: \n" +
ex.Message;
MessageBox.Show(this, errorMessage, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
finally
{
r1RefreshButton.Enabled = true;
}
}
}
private void …Run Code Online (Sandbox Code Playgroud)