c# - C# 中的 MessageBox、锁和 System.Windows.Forms.Timer 问题
问题描述
lock
我相信我对 a 的工作方式或System.Windows.Forms.Timer
C# 中的工作方式有误解。
因此,我制作了一个简单的 Windows 窗体应用程序(.NET Framework),并在工具箱中添加了一个和Timer
一个。单击时开始,然后在虚拟对象上输入 a并在事件中阻止它。对于's事件,我有以下方法:Button
Form
Button
Timer
Timer
lock
Tick
Button
Click
private void button1_Click(object sender, EventArgs e)
{
timer1.Enabled = true;
}
对于Timer
'sTick
事件,我有这个方法:
readonly object lockObj = new object();
private void timer1_Tick(object sender, EventArgs e)
{
lock (lockObj)
{
MessageBox.Show("Entered the lock!");
MessageBox.Show("Exiting the lock...");
}
}
其他一切都保持默认,没有额外的代码。
我希望这个程序显示一个MessageBox
带有 text的单曲"Entered the lock!"
,然后在我关闭它之后,还有下一个带有"Exiting the lock..."
我认为锁将被释放的消息和排队的 Tick 事件(如果有任何获得锁),该过程重复。相反,它"Entered the lock!"
MessageBox
会不断打开多次而不必关闭它,就好像每个Tick
事件调用都会进入锁,即使没有人释放它。
我试图在控制台应用程序中复制它,但没有运气。我很感激有关导致此问题的原因的提示,因此我知道在哪里进行调查。
您可以在 Windows 窗体应用程序中测试的替代代码:
using System;
using System.Drawing;
using System.Windows.Forms;
namespace Lock_Test_2
{
public partial class Form1 : Form
{
Timer timer1;
readonly object lockObj = new object();
public Form1()
{
InitializeComponent();
Button button1 = new Button();
button1.Location = new Point(100, 100);
button1.Size = new Size(187, 67);
button1.Text = "button1";
button1.Click += button1_Click;
Controls.Add(button1);
timer1 = new Timer();
timer1.Tick += timer1_Tick;
}
private void button1_Click(object sender, EventArgs e)
{
timer1.Enabled = true;
}
private void timer1_Tick(object sender, EventArgs e)
{
lock (lockObj)
{
MessageBox.Show("Entered the lock!");
MessageBox.Show("Exiting the lock...");
}
}
}
}
解决方案
System.Windows.Forms.Timer
通过 Windows 消息循环调度其事件。
MessageBox.Show
显示一个消息框,然后将 windows 消息循环作为嵌套循环泵入。这可以包括为计时器分派更多事件。
由于只涉及一个线程(UI 线程)并且lock
是可重入的,这就是您显示多个消息框的原因。
推荐阅读
- sharepoint-online - 从移动应用程序访问共享点在线列表
- alfresco - org.alfresco.trashcan.TrashcanCleanerTest 中的 java.lang.ExceptionInInitializerError。
(TrashcanCleanerTest.java:67) - maven - Maven - 使用从 src/test 到 src/main 的静态变量
- spring-boot - Spring Boot WAR部署到tomcat
- windows - 将文件从 Jenkins 服务器 (Linux) 复制到 Windows 2003 共享位置
- rest - Cisco Spark:有没有办法使用 bot API 进行@all 提及?
- excel - 将 Excel 工作表打印成单个 PDF
- c# - 来自 ASP.NET SmtpClient 的偶尔“SMTP 服务器需要安全连接”
- regex - 和 REGEX 中的运算符
- python - 如何在样条插值的所有数据点处设置导数零约束?