繁体   English   中英

winform 中的线程 - Compact .NET Framework 3.5

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

我在 function 中有一个线程启动实时监控,它基本上打开串口并不断从串口读取数据。 但是,如果我需要终止这个线程,我应该怎么做呢? 因为如果我不终止打开特定串口和读取数据的运行线程。 当我关闭它并再次拨打 function 时。 同一个串口打不开。 我怀疑串行端口没有正确关闭并且仍在单独的线程中运行。 所以我想我必须终止那个线程,以便下次再次打开同一个串行端口。 有谁知道如何实现这一目标?

我看到一些论坛说 Thread.Abort() 使用起来很危险。 它应该只在最后的手段中使用。

感谢您的帮助。

查尔斯

通常,您设计在后台线程中运行的方法来侦听取消请求。 这可以像 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()

首先认为你有线程,要关闭所有线程,你应该在启动它们之前将它们全部设置为后台线程,然后它们将在应用程序退出时自动关闭。

然后尝试这种方式:

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

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

使用最初设置为 false 的 boolean 标志,当您希望线程退出时,将其设置为 true。 显然,您的主线程循环需要监视该标志。 当它看到它变为 true 时,就停止轮询,关闭端口并退出主线程循环。

您的主循环可能如下所示:

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

根据您等待新数据的方式,您可能希望使用事件( ManualResetEventAutoResetEvent )以便在您希望它退出时唤醒您的线程。

暂无
暂无

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

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