1

我有一个使用称为 Start/Stop 的方法启动/终止线程的类。Stop 方法可以正确清理线程资源,但如果线程本身自然终止或出现异常,我需要能够调用 Stop 或其他变体(如果需要)来正确清理。

由于我采用了锁定机制,因此不能从线程方法中调用 Stop。

有没有办法可以在原始上下文中调用 Stop 方法?

private bool Terminate { get; set; }
private object _SyncRoot = new object();
private System.Threading.Thread Thread { get; set; }
private System.Threading.CancellationTokenSource CancellationTokenSource { get; set; }

public bool Start (ProcessorOptions options)
{
    bool result = false;

    lock (this._SyncRoot)
    {
        if (this.State == EnumState.Ready)
        {
            this.Options = options;

            if (this.CancellationTokenSource != null)
            {
                this.CancellationTokenSource.Dispose();
            }

            this.CancellationTokenSource = new System.Threading.CancellationTokenSource();
            //this.CancellationTokenSource.Token.Register(?, null, true);

            this.Terminate = false;

            this.Thread = new System.Threading.Thread(new System.Threading.ThreadStart(this.Process));
            this.Thread.Start();

            result = true;
        }
    }

    return (result);
}

public void Stop ()
{
    lock (this._SyncRoot)
    {
        if (this.State == EnumState.Processing)
        {
            try
            {
                this.Terminate = true;
                this.CancellationTokenSource.Cancel(false);

                if (!this.Thread.Join(System.TimeSpan.FromSeconds(1.0D)))
                {
                    this.Thread.Abort();
                }
            }
            finally
            {
                this.Thread = null;
            }
        }
    }
}

private void Process ()
{
    lock (this._SyncRoot)
    {
        if (this.State != EnumState.Ready)
        {
            throw (new System.InvalidOperationException("The processor instance is not in a ready state."));
        }
    }

    while (!this.Terminate)
    {
        lock (this._SyncRoot)
        {
            if (this.QueuedDocuments.Count == 0)
            {
                if (this.StopAutomaticallyOnQueueEmpty)
                {
                    // Invoke this.Stop() before breaking.
                    break;
                }
            }
        }

        // Parallel.For uses CancellationTokenSource here.

        System.Threading.Thread.Sleep(System.TimeSpan.FromSeconds(0.2D));
    }
}
4

2 回答 2

2

如果您不想按照 Mike 的建议在单独的“维护”线程中跟踪 ThreadStates,您可以试试这个:

在 Process 方法中保留 break 语句以退出 while 循环(并锁定上下文)。在方法末尾添加以下代码:

ThreadPool.QueueUserWorkItem(o => Stop());

一旦线程池有可用线程(通常是立即),这将使用线程池在您的实例中调用 Stop 方法。

编辑:您还可以向您的 Stop 方法添加一个参数,表明您只想清理资源,而不进行任何线程同步(因为线程无论如何都将退出)。或者更好的是,将现有方法一分为二:清理和停止。这允许您在 Process 方法结束时调用 Stop (Cleanup),而无需使用线程池。

于 2013-10-12T19:59:03.073 回答
0

在你的主类中设置一个计时器,检查ThreadState它管理的每个线程的时间。如果状态是ThreadState.Stopped,请继续清理它(并将其从要观察的线程集合中删除)。

于 2013-10-12T18:46:48.813 回答