gpt4 book ai didi

c# - FormClosing 后计时器滴答?

转载 作者:行者123 更新时间:2023-11-30 16:23:03 25 4
gpt4 key购买 nike

在 myForm_FormClosing 之后是否有任何机会调用 timer_Tick在下面的代码中。

如果有机会:在 myForm_FormClosing 中调用 timer.Stop() 是否足以避免在 myForm_FormClosing 之后调用 timer_Tick?

using System;
using System.Windows.Forms;
using System.ComponentModel;

namespace Test
{
static class Program
{
[STAThread]
static void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new MyForm());
}
}

class MyForm : Form
{
private IContainer components;
private Timer timer;

protected override void Dispose(bool disposing)
{
if (disposing && (components != null))
{
components.Dispose();
}
base.Dispose(disposing);
}

public MyForm()
{
components = new Container();
timer = new Timer(components);

timer.Interval = 50;
timer.Tick += timer_Tick;
timer.Enabled = true;

FormClosing += myForm_FormClosing;
}

private void timer_Tick(object sender, EventArgs e)
{
}

private void myForm_FormClosing(object sender, FormClosingEventArgs e)
{
}
}
}

更新:在收到一些提示(感谢帮助)后,我基本上选择了以下代码来实现我想要的。请注意,调用 myForm_FormClosing 后仍然可以调用 timer1_Tick!这个解决方案只是引入了一个标志(我称之为 doWork),它在调用 myForm_FormClosing 之后停止执行 timer1_Tick 中的代码。

using System;
using System.Windows.Forms;
using System.ComponentModel;

namespace Test
{
static class Program
{
[STAThread]
static void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new MyForm());
}
}

class MyForm : Form
{
private IContainer components;
private Timer timer;
private bool doWork = true;

protected override void Dispose(bool disposing)
{
if (disposing && (components != null))
{
components.Dispose();
}
base.Dispose(disposing);
}

public MyForm()
{
components = new Container();
timer = new Timer(components);

timer.Interval = 50;
timer.Tick += timer_Tick;
timer.Enabled = true;

FormClosing += myForm_FormClosing;
}

private void timer_Tick(object sender, EventArgs e)
{
if (doWork)
{
//do the work
}
}

private void myForm_FormClosing(object sender, FormClosingEventArgs e)
{
doWork = false;
}
}
}

最佳答案

是的,这是可能的。根据the docs ...

Occurs before the form is closed.

When a form is closed, it is disposed, releasing all resources associated with the form.

直到 FormClosing 事件之后,计时器才会被释放。这是一个非常人为的例子来说明它是如何发生的。在调用 FormClosing 之后,您将看到您在 Timer tick 上命中了调试器断点。

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Diagnostics;
using System.Threading.Tasks;
using System.Threading;

namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{
private System.Windows.Forms.Timer _time = new System.Windows.Forms.Timer();
private Task _t1 = null;
private Task _t2 = null;
private bool _closingFlag = false;
public Form1()
{
InitializeComponent();

_time.Interval = 50;
_time.Tick += (s, e) => {
textBox1.Text = DateTime.Now.ToString();
if (_closingFlag) Debugger.Break();
};

_time.Start();
}

private void Form1_FormClosing(object sender, FormClosingEventArgs e)
{
_closingFlag = true;

_t1 = Task.Factory.StartNew(() => { Thread.Sleep(1000); });
_t2 = Task.Factory.StartNew(() => { Thread.Sleep(1000); });

Task.WaitAll(_t1, _t2);
}
}
}

关于c# - FormClosing 后计时器滴答?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11957486/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com