我的代码在关闭winform后保持运行

  • 本文关键字:运行 winform 代码 c# dialog
  • 更新时间 :
  • 英文 :


我有很多表单,我在主表单上管理它们。我打开其他窗体与ShowDialog()和一些我的窗体涉及一个操作发生定时器保持运行后关闭对话框,我使用

this.Close ();

用于关闭showdialog,但它仍然在后面工作,然后给我消息框。在我的主窗体上显示。我的意思是,如果它仍然处于活动状态,它是否可以工作,但是我关闭了对话框?

下面的代码只是其中之一,它与任务等待一起工作,我的其他形式与计时器一起运行,我也得到了同样的问题。

private async void ıconButton4_Click(object sender, EventArgs e)
{
saniye = 0;

if (hız <= 29)
{
MessageBox.Show("Başlamadan önce hızı da belirlemeliyiz", "Hız belirtilmedi", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
else
{
buttondurdur.Enabled = true;
button1.Enabled = false;
button2.Enabled = false;
menuStrip1.Enabled = false;
richTextBox1.Text = string.Empty;

TextFormatFlags flags = TextFormatFlags.Top | TextFormatFlags.Left |
TextFormatFlags.WordBreak | TextFormatFlags.NoPadding |
TextFormatFlags.TextBoxControl;
timer1.Enabled = true;

foreach (var word in GetWords())
{
richTextBox1.Text += (word + ' ');
kelimesayısı += 1;
do
{
await Task.Delay(hız);
} while (_isPaused);

Size textSize = TextRenderer.MeasureText(richTextBox1.Text, richTextBox1.Font, richTextBox1.Size, flags);

if (textSize.Height >= (richTextBox1.Height - 40))
{
richTextBox1.Clear();
}

}


if (Saying.Length >= 150)
{
timer1.Stop();
MessageBox.Show("Okuma hızınız dakikada " + (kelimesayısı * 60) / saniye + " kelime ");
timer1.Enabled = false;
richTextBox1.Clear();
button1.Enabled = false;
button2.Enabled = true;
buttondurdur.Enabled = false;
Saying = string.Empty;
kelimesayısı = 0;
menuStrip1.Enabled = true;
saniye = 0;
}
else
{
MessageBox.Show("Seçilen metin çok kısa ", "Uyarı", MessageBoxButtons.OK, MessageBoxIcon.Error);
timer1.Enabled = false;
richTextBox1.Clear();                
buttondurdur.Enabled = false;
Saying = string.Empty;
saniye = 0;
kelimesayısı = 0;
button1.Enabled = true;
button2.Enabled = true;
menuStrip1.Enabled = true;
}
}


}

您可能想尝试this.Dispose();,而不是this.Close();。虽然这是一个更糟糕的解决方案,但在这种情况下它可能会更好。

我的意思是:

我创建了一个简单的表单应用程序,在主表单中添加了一个按钮,并将其作为按钮处理程序运行:

private void button1_Click(object sender, EventArgs e)
{
using (var dlg = new TheDialog())
{
if (dlg.ShowDialog(this) == DialogResult.OK)
{
MessageBox.Show("OK");
}
}
}

然后我写了TheDialog表单类(与ShowDialog非模态运行的意图构建)。

我添加了三个按钮,一个标记为"Start timer",另外两个是非模态表单的标准OK和Cancel按钮(将它们设置为表单的Accept和Cancel按钮)。我还在对话框的表单中添加了一个计时器,将其时间设置为15秒(15000ms)。下面是按钮处理程序:

private void StartTimerBtn_Click(object sender, EventArgs e)
{
if (timer1.Enabled)
{
timer1.Stop();
}
timer1.Start();
}
private void CancelBtn_Click(object sender, EventArgs e)
{
DialogResult = DialogResult.Cancel;
Close();
}
private void OkBtn_Click(object sender, EventArgs e)
{
DialogResult = DialogResult.OK;
Close();
}

我将定时器的Tick事件连接到:

private void timer1_Tick(object sender, EventArgs e)
{
MessageBox.Show("Timer Expired");
timer1.Stop();
}

如果你打开表单,按下"Start Timer"数到15,会弹出提示框,提示"定时器已过期">

最后,我连接了对话框的FormClosing事件。这将在窗体关闭之前被调用-无论是因为有人按了右上角的[x]还是那个人按了OKCancel按钮(或按了[ESC]或[Enter])。
private void TheDialog_FormClosing(object sender, FormClosingEventArgs e)
{
if (timer1.Enabled)
{
timer1.Stop();
}
}

这样,无论发生什么,当对话框关闭时,计时器将停止。

不管它的价值是什么,仅仅调用Dispose是一个真正的坏主意。访问已处置的对象从来都不是一个好主意——您需要先停止它。处置窗体时,您在设计器中添加到窗体中的控件也将被处置。

同样值得注意的是,这就是最小可复制示例的真正含义

最新更新