简体   繁体   English

停止不在while循环中的C#线程

[英]Stop a C# thread that is not in a while loop

I've searched around to find the best way to terminate a non UI thread. 我四处搜寻以找到终止非UI线程的最佳方法。 What I found is that, in short, the thread is in a while loop and you simply set the while loop condition to false to make the thread exit the loop: 简而言之,我发现线程处于while循环中,您只需将while循环条件设置为false即可使线程退出循环:

https://msdn.microsoft.com/en-us/library/7a2f3ay4(v=vs.80).aspx

Fine but my thread is not in a while loop. 很好,但是我的线程不在while循环中。 The thread is started and then its dispatcher is used. 启动线程,然后使用其调度程序。 It's actually a thread in a C# dll. 它实际上是C#dll中的一个线程。 The dll itself is using a second C++ dll through a SWIG interface. 该dll本身正在通过SWIG接口使用第二个C ++ dll。 Long story short, I need a dispatcher. 长话短说,我需要一个调度员。

Now before going further, I would like to mention that everything works fine. 现在,在继续之前,我想提一下,一切正常。 But the only way I found to stop the dll thread is by calling Thread.Abort(). 但是,我发现停止dll线程的唯一方法是调用Thread.Abort()。 I would like to this in a more graceful way. 我想以更优雅的方式做到这一点。

From the UI thread, that uses the C# dll, an instance of the dll class is created: 从使用C#dll的UI线程中,创建dll类的实例:

    public CSomethingDll()
    {
        // This is the dll class constructor.
        using (m_startEvent = new ManualResetEvent(false))
        {
            m_mainSdkThread = new Thread(DllThreadEntryPoint);
            m_mainSdkThread.Name = "DLL THREAD";
            m_mainSdkThread.Start(m_startEvent);

            m_startEvent.WaitOne();
        }
    }

    [STAThreadAttribute]
    public void DllThreadEntryPoint (object o)
    {
        m_dispatcher = System.Windows.Threading.Dispatcher.CurrentDispatcher;

        (o as ManualResetEvent).Set();

        System.Windows.Threading.Dispatcher.Run();

        // The dispatcher is now running and ready for calls to BeginInvoke().
    }

The dispatcher of the dll thread is now running. dll线程的调度程序现在正在运行。 The UI thread can now initialize the dll by calling the InitializeA method of the dll: UI线程现在可以通过调用dll的InitializeA方法来初始化dll:

    public void InitializeA()
    {
        m_dispatcher.BeginInvoke(EvInitialize);
    }

Now, when the UI thread is terminating, when the application is closing, the UI thread calls the FinalizeA method: 现在,当UI线程终止时,当应用程序关闭时,UI线程调用FinalizeA方法:

    public void FinalizeA()
    {
        m_dispatcher.BeginInvoke(EvFinalize);
    }

Inside the dll thread, stuff is done to terminate everything and release the resources. 在dll线程内部,已完成了一些工作以终止所有操作并释放资源。 Everything works fine. 一切正常。 But the only way I found to stop the dll thread is by calling Thread.Abort(): 但是我发现停止dll线程的唯一方法是调用Thread.Abort():

m_dispatcher.Thread.Abort();

Now, is there some trick to stop the dll thread through the Dispatcher? 现在,是否有一些技巧可以阻止通过Dispatcher的dll线程? Some way to stop the dispatcher that will then terminate the associated thread? 有什么方法可以停止调度程序,该调度程序随后将终止关联的线程?

Thanks! 谢谢!

How do you terminate your UI thread, do you properly exit your dispatcher? 如何终止UI线程,是否正确退出调度程序?

You really need to call Dispatcher.ExitAllFrames so that the Run method exits. 您确实需要调用Dispatcher.ExitAllFrames以便退出Run方法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM