简体   繁体   English

winform 中的线程 - Compact .NET Framework 3.5

[英]Threading in winform - Compact .NET Framework 3.5

I have a thread in a function which start the live monitoring, it basically, opens the serial port and continously reading data from serial port.我在 function 中有一个线程启动实时监控,它基本上打开串口并不断从串口读取数据。 However, If I need to terminate this thread, how should I do it?但是,如果我需要终止这个线程,我应该怎么做呢? Because If I don't terminate the running thread which opened the specific serial port and reading data.因为如果我不终止打开特定串口和读取数据的运行线程。 When I close it and call the function again.当我关闭它并再次拨打 function 时。 The same serial port cannot opened.同一个串口打不开。 I suspect that the serial port is not closed properly and is still running in a seperate thread.我怀疑串行端口没有正确关闭并且仍在单独的线程中运行。 So I think i have to terminate that thread in order to open the same serial port again next time.所以我想我必须终止那个线程,以便下次再次打开同一个串行端口。 Does anyone have any idea of how to achieve this?有谁知道如何实现这一目标?

I have seen some forum said that Thread.Abort() is dangerous to use.我看到一些论坛说 Thread.Abort() 使用起来很危险。 It should only be used in last resort.它应该只在最后的手段中使用。

Thanks for any helps.感谢您的帮助。

Charles查尔斯

Generally, you design the method that is run in the background thread to listen for cancellation requests.通常,您设计在后台线程中运行的方法来侦听取消请求。 This can be as simple as a boolean value:这可以像 boolean 值一样简单:

//this simply provides a synchronized reference wrapper for the Boolean,
//and prevents trying to "un-cancel"
public class ThreadStatus
{
   private bool cancelled;

   private object syncObj = new Object();

   public void Cancel() {lock(syncObj){cancelled = true;}}

   public bool IsCancelPending{get{lock(syncObj){return cancelled;}}}
}

public void RunListener(object status)
{
   var threadStatus = (ThreadStatus)status;

   var listener = new SerialPort("COM1");

   listener.Open();

   //this will loop until we cancel it, the port closes, 
   //or DoSomethingWithData indicates we should get out
   while(!status.IsCancelPending 
         && listener.IsOpen 
         && DoSomethingWithData(listener.ReadExisting())
      Thread.Yield(); //avoid burning the CPU when there isn't anything for this thread

   listener.Dispose();
}

...

Thread backgroundThread = new Thread(RunListener);
ThreadStatus status = new ThreadStatus();
backgroundThread.Start(status);

...

//when you need to get out...
//signal the thread to stop looping
status.Cancel();
//and block this thread until the background thread ends normally.
backgroundThread.Join()

First think you have thread in it and to close all your threads you should set all of them to background threads before you start them then they will be closed automatically when the application exits.首先认为你有线程,要关闭所有线程,你应该在启动它们之前将它们全部设置为后台线程,然后它们将在应用程序退出时自动关闭。

And then try this way:然后尝试这种方式:

Thread somethread = new Thread(...);
someThread.IsBackground = true;
someThread.Start(...); 

Refer http://msdn.microsoft.com/en-us/library/aa457093.aspx参考http://msdn.microsoft.com/en-us/library/aa457093.aspx

Use a boolean flag you set to false initially and when you want your thread to quit you set it to true.使用最初设置为 false 的 boolean 标志,当您希望线程退出时,将其设置为 true。 Apparently your main thread loop needs to monitor that flag.显然,您的主线程循环需要监视该标志。 When it sees that it changes to true then quite your polling, close the port and exit the main thread loop.当它看到它变为 true 时,就停止轮询,关闭端口并退出主线程循环。

Your main loop could look like this:您的主循环可能如下所示:

OpenPort();
while (!_Quit)
{
    ... check if some data arrived
    if (!_Quit)
    {
        ... process data
    }
}
ClosePort();

Depending on how you wait for new data you might want to make use of events ( ManualResetEvent or AutoResetEvent ) in order to wake up your thread when you want it to quit.根据您等待新数据的方式,您可能希望使用事件( ManualResetEventAutoResetEvent )以便在您希望它退出时唤醒您的线程。

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

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