之后的FormClosing计时器滴答? [英] Timer Tick after FormClosing?

查看:143
本文介绍了之后的FormClosing计时器滴答?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

任何的机会,timer_Tick可以在下面的代码myForm_FormClosing
之后被调用。



如果有机会的话:是不是足以叫内myForm_FormClosing timer.Stop(),以避免timer_Tick被myForm_FormClosing后调用

 使用系统? ;使用System.Windows.Forms的
;
使用System.ComponentModel;

命名空间测试
{
静态类节目
{
[STAThread]
静态无效的主要()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(假);
Application.Run(新的MyForm());
}
}

类MyForm的:表格
{
私人的IContainer组件;
私人定时器定时器;

保护覆盖无效的Dispose(BOOL处置)
{
如果(处置和放大器;及(成分= NULL)!)
{
组分。的Dispose();
}
base.Dispose(处置);
}

公共MyForm的()
{
组分=新的Container();
定时器=新定时器(组件);

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

的FormClosing + = myForm_FormClosing;
}

私人无效timer_Tick(对象发件人,EventArgs五)
{
}

私人无效myForm_FormClosing(对象发件人,FormClosingEventArgsê )
{
}
}
}

更新
receving一些提示(感谢帮助)我基本上都选择了下面的代码才达到我想要的东西之后。
请不是timer1_Tick仍然可以被称为myForm_FormClosing被称为后!
该溶液只引入了一个标志(ⅰ称它为的doWork),其停止myForm_FormClosing被称为后要执行内timer1_Tick代码



 使用系统;使用System.Windows.Forms的
;
使用System.ComponentModel;

命名空间测试
{
静态类节目
{
[STAThread]
静态无效的主要()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(假);
Application.Run(新的MyForm());
}
}

类MyForm的:表格
{
私人的IContainer组件;
私人定时器定时器;
私人布尔的doWork = TRUE;

保护覆盖无效的Dispose(BOOL处置)
{
如果(处置和放大器;及(成分= NULL)!)
{
组分。的Dispose();
}
base.Dispose(处置);
}

公共MyForm的()
{
组分=新的Container();
定时器=新定时器(组件);

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

的FormClosing + = myForm_FormClosing;
}

私人无效timer_Tick(对象发件人,EventArgs五)
{
如果(的doWork)
{
//做的工作
}
}

私人无效myForm_FormClosing(对象发件人,FormClosingEventArgs E)
{
的doWork = FALSE;
}
}
}


解决方案

是的,它是可能的。据的文档 ...




窗体关闭之前发生。



在一个窗体关闭,它配置,释放与表单关联的所有资源




定时器将不会被处理,直到的FormClosing事件之后。这里是一个很做作它是如何发生的例子。你会看到你打的断点调试器的计时器滴答的FormClosing被称为后。

 使用系统; 
使用System.Collections.Generic;
使用System.ComponentModel;
使用System.Data这;
使用System.Drawing中;
使用System.Linq的;
使用System.Text;使用System.Windows.Forms的
;使用System.Diagnostics程序
;使用System.Threading.Tasks
;
使用的System.Threading;

命名空间WindowsFormsApplication1
{
公共部分Form1类:表格
{
私人System.Windows.Forms.Timer _time =新System.Windows.Forms的.Timer();
私人任务_t1 = NULL;
私人任务_t2 = NULL;
私人布尔_closingFlag = FALSE;
公共Form1中()
{
的InitializeComponent();

_time.Interval = 50;
_time.Tick + =(S,E)=> {
textBox1.Text = DateTime.Now.ToString();
如果(_closingFlag)Debugger.Break();
};

_time.Start();
}

私人无效Form1_FormClosing(对象发件人,FormClosingEventArgs E)
{
_closingFlag = TRUE;

_t1 = Task.Factory.StartNew(()=> {Thread.sleep代码(1000);});
_t2 = Task.Factory.StartNew(()=> {Thread.sleep代码(1000);});

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


Is there any chance that timer_Tick could be called after myForm_FormClosing in the code below.

If there is a chance: Is it sufficient to call timer.Stop() within myForm_FormClosing in order to avoid that timer_Tick gets called after myForm_FormClosing?

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)
        {
        }
    }
}

Update: After receving a few hints (thanks for helping) I basically have chosen the following code to achive what I want. Please not that timer1_Tick could still be called after myForm_FormClosing was called! This solution just introduces a flag (i called it doWork) which stops the code within timer1_Tick to be executed after myForm_FormClosing was called.

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;
        }
    }
}

解决方案

Yes it is possible. According to the docs ...

Occurs before the form is closed.

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

The timer will not be disposed until after the FormClosing event. Here is a very contrived example of how it can happen. You will see that you hit the debugger breakpoint on the Timer tick after FormClosing has been called.

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);
        }
    }
}

这篇关于之后的FormClosing计时器滴答?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆