查看线程不正确结束 [英] See thread not correctly ended

查看:161
本文介绍了查看线程不正确结束的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发使用C#和WPF上的Visual Studio 2015年项目。有时候,我辞掉了正在运行的项目与我亲密的命令,有时停止调试按钮。问题是,一些测试后,我的电脑开始温暖和球迷制造噪音。我不得不退出Visual Studio来平息机

I'm developing a project on Visual Studio 2015 using C# and WPF. Sometimes I quit my running project with my close command, and sometimes with the stop debug button. The problem is that after a few tests, my PC starts to warm and the fans make noise. I have to quit Visual Studio to calm the machine.

所以,我有问题:


  • 如何看到测试后,线程没有结束?

  • 当我知道他们,如何正确地结束呢? (其实我的Dispose 某些线程时的windowClosing

  • 如何确保当我使用停止调试按钮线程将正常结束?

  • How to see the threads not ended after a test ?
  • When I know them, how to properly end them ? (actually I Dispose some threads when WindowClosing)
  • How make sure that thread will properly ends when I use the stop debug button ?

感谢您

编辑:

有是任务管理器的屏幕截图。当我开始应用CPU上涨5%至15%(或事件25%)。 RAM上升从4GO至4.5。
当我停止应用程序,CPU进入45%几秒钟,并返回到5%,但RAM去4.70GO和不回去了。

There is the screenshot of task manager. When I start application the CPU rise from 5% to 15% (or event 25%). RAM rise from 4GO to 4.5. When I stop application, CPU goes to 45% for a few seconds and go back to 5% but RAM goes to 4.70GO and doesn't go back down.

任务管理器

EDIT2:

我创办这种线程在我的应用程序:

I founded this kind of thread on my application:

private bool isClosing = false;
public void Start()
{
    isClosing = false;
    ThreadPool.QueueUserWorkItem(new WaitCallback(doWorkThread));
}

public void Stop()
{
    isClosing = true;
}

private AutoResetEvent endPoolStateButton = new AutoResetEvent(false);
private void doWorkThread(object sender)
{
    Action action = new Action(() => doWork());
    while (!isClosing)
    {
        Thread.Sleep(100);
        this.Dispatcher.BeginInvoke(action, System.Windows.Threading.DispatcherPriority.Background);
    }
    endPoolStateButton.Set();
}

private void doWork()
{
    /* Job performed */
}

我不知道是否有使用线程的一个非常好的方法吗?如果应用程序关闭时没有设定 isClosing = TRUE 的同时,从来没有停止。和线程从来没有真正放弃?你是否认为,这种线程可能会导致我所有的烦恼

I wonder if there is a really good way to use thread ? If application close without setting isClosing = true the while never stop. And the thread is never really aborted ? Do you think that this kind of thread can cause all the troubles I have ?

推荐答案

下面是的我的解决方案的如何以优雅的方式停止线程。希望该代码是明确的。我使用的CancellationToken 以取消操作螺纹和 ManualResetEvent的等待线程取消:

Here is my solution how to stop thread in elegant way. Hope the code is clear. I use CancellationToken to cancel operations in thread and ManualResetEvent to wait for thread cancellation:

namespace ElegantThreadWork
{
    using System;
    using System.Threading;
    using System.Diagnostics;

    class ThreadObject
    {
        public CancellationToken CancellationToken { get; private set; }
        public ManualResetEvent WaitHandle { get; private set; }

        public ThreadObject(CancellationToken ct, ManualResetEvent wh)
        {
            CancellationToken = ct;
            WaitHandle = wh;
        }
    }
    public class Program
    {
        static void DoWork(CancellationToken ct)
        {
            Console.WriteLine("Thread[{0}] started", Thread.CurrentThread.ManagedThreadId);
            int i = 0;
            // Check for cancellation on each iteration
            while (!ct.IsCancellationRequested)
            {
                // Do something
                Console.WriteLine("Thread[{0}]: {1}", Thread.CurrentThread.ManagedThreadId, i);
                // Wait on CancellationToken. If cancel be called, WaitOne() will immediatly return control!
                // You can see it by elapsed time
                ct.WaitHandle.WaitOne(TimeSpan.FromSeconds(1));
                i++;
            }
            Console.WriteLine("Thread[{0}] has been cancelled", Thread.CurrentThread.ManagedThreadId);
        }
        static void ThreadProc(object state)
        {
            ThreadObject to = (ThreadObject)state;
            try
            {
                DoWork(to.CancellationToken);
            }
            finally
            {
                to.WaitHandle.Set();
            }
        }
        public static void Main(string[] args)
        {
            TimeSpan MAX_THREAD_EXITING_TIMEOUT = TimeSpan.FromSeconds(5);

            // Use for elegant thread exiting
            ManualResetEvent isThreadExitedEvent = new ManualResetEvent(false);
            CancellationTokenSource cts = new CancellationTokenSource();
            ThreadObject threadObj = new ThreadObject(cts.Token, isThreadExitedEvent);

            // Create thread
            Thread thread = new Thread(ThreadProc, 0);
            thread.Start(threadObj);

            Console.WriteLine("Just do something in main thread");

            Console.WriteLine("Bla.");
            Thread.Sleep(1000);

            Console.WriteLine("Bla..");
            Thread.Sleep(1000);

            Console.WriteLine("Bla...");
            Thread.Sleep(1000);

            Console.WriteLine("Thread cancelattion...");
            Stopwatch sw = Stopwatch.StartNew();
            // Cancel thread
            cts.Cancel();

            // Wait for thread exiting
            var isOk = isThreadExitedEvent.WaitOne(MAX_THREAD_EXITING_TIMEOUT);
            sw.Stop();
            Console.WriteLine("Waiting {0} for thread exiting. Wait result: {1}. Cancelled in {2}", MAX_THREAD_EXITING_TIMEOUT, isOk, sw.Elapsed);

            // If we couldn't stop thread in elegant way, just abort it
            if (!isOk)
                thread.Abort();
        }
    }
}

这篇关于查看线程不正确结束的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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