Tag: 重入

Gui重新管理等待

我在使用NotifyIcons时发现了一个重入问题。 重现起来非常简单,只需在表单上删除NotiftIcon,click事件应如下所示: private bool reentrancyDetected; private void notifyIcon1_MouseClick(object sender, MouseEventArgs e) { if (reentrancyDetected) MessageBox.Show(“Reentrancy”); reentrancyDetected = true; lock (thisLock) { //do nothing } reentrancyDetected = false; } 同时启动后台线程会导致一些争用: private readonly object thisLock = new object(); private readonly Thread bgThread; public Form1() { InitializeComponent(); bgThread = new Thread(BackgroundOp) { IsBackground = true }; bgThread.Start(); } […]

在其回调方法中停止计时器

我有一个System.Threading.Timer,它每10毫秒调用一次适当的事件处理程序(回调)。 该方法本身不可重入 ,有时可能超过10毫秒 。 因此,我想在方法执行期间停止计时器。 码: private Timer _creatorTimer; // BackgroundWorker’s work private void CreatorWork(object sender, DoWorkEventArgs e) { _creatorTimer = new Timer(CreatorLoop, null, 0, 10); // some other code that worker is doing while the timer is active // … // … } private void CreatorLoop(object state) { // Stop timer (prevent reentering) _creatorTimer.Change(Timeout.Infinite, […]