当winform应用程序关闭时,如何停止线程

时间:2021-01-07 07:13:01

I have a singleton that has a running thread for obtaining records from a server. But when I stop my winform application the thread keeps running. I have tried to create a destructor in my singleton to abort the thread if it running, but it does not have any effect on the thread - I know that the destructor is being evoked.

我有一个单例,它有一个运行线程,用于从服务器获取记录。但是,当我停止我的winform应用程序时,线程继续运行。我试图在我的单例中创建一个析构函数,以便在线程运行时中止该线程,但它对线程没有任何影响 - 我知道析构函数是被引发的。

I am looking for suggestions on how I should shut down a thread when my application closes. thanks

我正在寻找关于如何在我的应用程序关闭时关闭线程的建议。谢谢

C#, .net2

2 个解决方案

#1


9  

The best option, if possible in your application, is cooperative cancellation.

如果可能,在您的申请中,最佳选择是合作取消。

A thread automatically stops when it has no more code to execute. So, when the user closes your application, you set a flag indicating that your thread should stop. The thread needs to check from time to time if the flag is set and, if so, stop obtaining records from the server and return.

当没有更多代码要执行时,线程会自动停止。因此,当用户关闭您的应用程序时,您设置一个标志,指示您的线程应该停止。线程需要不时检查是否设置了标志,如果是,则停止从服务器获取记录并返回。

  • As @Hans Passant noted, BackgroundWorker has built-in support for this.
  • 正如@Hans Passant所说,BackgroundWorker已经为此提供了内置支持。

  • If you can upgrade, the .NET Framework 4.0 introduces a whole set of new classes that support cooperative cancellation of asynchronous operations.
  • 如果可以升级,.NET Framework 4.0引入了一整套支持异步操作协同取消的新类。

Otherwise, you can roll your own solution, for example

否则,您可以滚动自己的解决方案,例如

static bool isCancellationRequested = false;
static object gate = new object();

// request cancellation
lock (gate)
{
    isCancellationRequested = true;
}

// thread
for (int i = 0; i < 100000; i++)
{
    // simulating work
    Thread.SpinWait(5000000);

    lock (gate)
    {
        if (isCancellationRequested)
        {
            // perform cleanup if necessary
            //...
            // terminate the operation
            break;
        }
    }
}

#2


55  

A thread can be:

一个线程可以是:

  • Foreground : will keep alive your programs until it is finish.
  • 前景:将一直保持你的程序,直到它完成。

  • Background : will be terminated when you close your application.
  • 背景:关闭您的申请时将终止。

When you create a thread, it is by default a foreground thread.

创建线程时,默认情况下它是前台线程。

You can change that like this:

你可以改变这样:

Thread t = new Thread(myAction);
t.IsBackground = true;
t.Start();

#1


9  

The best option, if possible in your application, is cooperative cancellation.

如果可能,在您的申请中,最佳选择是合作取消。

A thread automatically stops when it has no more code to execute. So, when the user closes your application, you set a flag indicating that your thread should stop. The thread needs to check from time to time if the flag is set and, if so, stop obtaining records from the server and return.

当没有更多代码要执行时,线程会自动停止。因此,当用户关闭您的应用程序时,您设置一个标志,指示您的线程应该停止。线程需要不时检查是否设置了标志,如果是,则停止从服务器获取记录并返回。

  • As @Hans Passant noted, BackgroundWorker has built-in support for this.
  • 正如@Hans Passant所说,BackgroundWorker已经为此提供了内置支持。

  • If you can upgrade, the .NET Framework 4.0 introduces a whole set of new classes that support cooperative cancellation of asynchronous operations.
  • 如果可以升级,.NET Framework 4.0引入了一整套支持异步操作协同取消的新类。

Otherwise, you can roll your own solution, for example

否则,您可以滚动自己的解决方案,例如

static bool isCancellationRequested = false;
static object gate = new object();

// request cancellation
lock (gate)
{
    isCancellationRequested = true;
}

// thread
for (int i = 0; i < 100000; i++)
{
    // simulating work
    Thread.SpinWait(5000000);

    lock (gate)
    {
        if (isCancellationRequested)
        {
            // perform cleanup if necessary
            //...
            // terminate the operation
            break;
        }
    }
}

#2


55  

A thread can be:

一个线程可以是:

  • Foreground : will keep alive your programs until it is finish.
  • 前景:将一直保持你的程序,直到它完成。

  • Background : will be terminated when you close your application.
  • 背景:关闭您的申请时将终止。

When you create a thread, it is by default a foreground thread.

创建线程时,默认情况下它是前台线程。

You can change that like this:

你可以改变这样:

Thread t = new Thread(myAction);
t.IsBackground = true;
t.Start();