Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/multithreading/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 为什么可以';我不能在用户控件构造函数中启动线程吗?_C#_Multithreading_Winforms_User Controls - Fatal编程技术网

C# 为什么可以';我不能在用户控件构造函数中启动线程吗?

C# 为什么可以';我不能在用户控件构造函数中启动线程吗?,c#,multithreading,winforms,user-controls,C#,Multithreading,Winforms,User Controls,首先,我知道这是个坏习惯。。。在这一点上,这已经变成了一个“需要知道”的练习,然后是一个最佳实践练习 我有一个usercontrol,它是从主winform的构造函数初始化的。在该USerControl中,我试图启动一个保持活动的线程 public TestControl() { InitializeComponent(); this.Disposed += Dispose; // Start the keep alive Thread

首先,我知道这是个坏习惯。。。在这一点上,这已经变成了一个“需要知道”的练习,然后是一个最佳实践练习

我有一个usercontrol,它是从主winform的构造函数初始化的。在该USerControl中,我试图启动一个保持活动的线程

public TestControl()
    {
        InitializeComponent();

        this.Disposed += Dispose;

        // Start the keep alive Thread
        _keepAliveThread = new Thread(
            () =>
            {
                while (true)
                {
                    Thread.Sleep(60000);
                    try
                    {
                        _service.Ping();
                        Trace.WriteLine("Ping called on the Service");
                    }
                    catch
                    {
                        Trace.WriteLine("Ping failed");
                    }
                }
            });
        _keepAliveThread.Start();
    }
每当我这样做时,dispose不会在设计器中激发,也不会获得事件

只要不启动线程,dispose就会启动。再一次。。。我知道这是一种不好的做法,但我正试图弄清楚为什么这样做行不通。

以下是我的代码:

public partial class SillyControl : UserControl
{
    Thread backgroundThread;
    Service service = new Service();

    public SillyControl()
    {
        InitializeComponent();

        this.Disposed += delegate { Trace.WriteLine("I been disposed!"); };

        backgroundThread = new Thread(argument =>
        {
            Trace.WriteLine("Background ping thread has started.");

            while (true)
            {
                Thread.Sleep(5000);
                try
                {
                    service.Ping();
                    Trace.WriteLine("Ping!");
                }
                catch (Exception ex)
                {
                    Trace.WriteLine(string.Format("Ping failed: {0}", ex.Message)); 
                }
            }
        });

        backgroundThread.IsBackground = true; // <- Important! You don't want this thread to keep the application open.
        backgroundThread.Start();
    }
}
public部分类SillyControl:UserControl
{
线程背景线程;
服务=新服务();
公共愚蠢控制
{
初始化组件();
this.Disposed+=委托{Trace.WriteLine(“我被处置了!”;};
backgroundThread=新线程(参数=>
{
WriteLine(“后台ping线程已启动”);
while(true)
{
睡眠(5000);
尝试
{
service.Ping();
Trace.WriteLine(“Ping!”);
}
捕获(例外情况除外)
{
Trace.WriteLine(string.Format(“Ping失败:{0}”,ex.Message));
}
}
});

backgroundThread.IsBackground=true;//这似乎也不起作用。事实上,我相信语法也不正确。\ u服务期望线程构造函数为int。@AndySousa我已经编辑了语法,现在我有了VS open,进一步研究了语法。我只是像您一样更新了它…现在正在测试。dispose仍然没有ot get call,应用程序不会退出。还有其他想法吗?我完全改变了我的答案,因为我认为我看到了问题所在。将
线程设置为
true
非常重要。如果不这样做,应用程序将保持打开/运行,直到所有非后台线程都被杀死。在大多数情况下,UI(主线程)应该是唯一的非后台线程。这样做了…谢谢。我认为IsBackground=true将是默认操作。您能澄清一下“dispose不会在设计器中触发”的意思吗?在没有运行它的情况下,您如何检查它?我在
输出
窗口中没有看到任何提示它将从desi中进行处理的内容格内尔。