简体   繁体   English

一个While循环线程

[英]A While Loop Thread

So I've been trying to create a bit of code that sends data on a while loop, specifically an alive packet to a server through a UdpClient. 所以我一直在尝试创建一些代码,这些代码在while循环上发送数据,特别是通过UdpClient向服务器发送活动数据包。

 static void doSend(string ip, int port)
    {
        while (isSending)
        {
            _sockMain = new UdpClient(ip, port);
            // Code for datagram here, took it out
            _sockMain.Send(arr_bData, arr_bData.Length);
        }
    }

But when I call the "Stop" method, it gets stuck in a constant loop and doesn't come out. 但是当我调用“停止”方法时,它会陷入一个恒定的循环并且不会出现。 How can I put the while loop into a Thread? 如何将while循环放入线程? So I can abort the thread on stop, cancelling the loop? 所以我可以在停止时中止线程,取消循环?

It hangs because your doSend method works on UI thread. 它会挂起,因为你的doSend方法适用于UI线程。 You can use something like the below class to make it run on a seperate thread or you can use BackgroundWorkerClass 您可以使用类似下面的类来使其在单独的线程上运行,或者您可以使用BackgroundWorkerClass

public class DataSender
    {
        public DataSender(string ip, int port)
        {
            IP = ip;
            Port = port;
        }

        private string IP;
        private int Port;
        System.Threading.Thread sender;
        private bool issending = false;

        public void StartSending()
        {
            if (issending)
            {
                // it is already started sending. throw an exception or do something.
            }
            issending = true;
            sender = new System.Threading.Thread(SendData);
            sender.IsBackground = true;
            sender.Start();
        }

        public void StopSending()
        {
            issending = false;
            if (sender.Join(200) == false)
            {
                sender.Abort();
            }
            sender = null;
        }

        private void SendData()
        {
            System.Net.Sockets.UdpClient _sockMain = new System.Net.Sockets.UdpClient(IP, Port);
            while (issending)
            {
                // Define and assign arr_bData somewhere in class
                _sockMain.Send(arr_bData, arr_bData.Length);
            }
        }
    }

You can use the backgroundworker thread http://www.dotnetperls.com/backgroundworker and inside dowork() put your while loop. 你可以使用backgroundworker线程http://www.dotnetperls.com/backgroundworker和dowork()里面的你的while循环。 You can stop the code by using CancelAsync() and set backgroundWorker1.WorkerSupportsCancellation == true 您可以使用CancelAsync()并设置backgroundWorker1.WorkerSupportsCancellation == true来停止代码

BackgroundWorker bw = new BackgroundWorker();
          if (bw.IsBusy != true)
          {
              bw.RunWorkerAsync();

          }

          private void bw_DoWork(object sender, DoWorkEventArgs e)
          {
              // Run your while loop here and return result.
              result = // your time consuming function (while loop)
          }

          // when you click on some cancel button  
           bw.CancelAsync();
static bool _isSending;

static void doSend(string ip, int port)
{
    _isSending = true;

    while (_isSending)
    {
        _sockMain = new UdpClient(ip, port);
        // ...
        _sockMain.Send(arr_bData, arr_bData.Length);
    }
}

static void Stop()
{
    // set flag for exiting loop here
    _isSending = false;    
}

Also consider to name your methods in PascalCase, ie DoSend (even StartSending will be better), StopSending . 还要考虑在PascalCase中命名你的方法,即DoSend (甚至StartSending会更好), StopSending

如何使用BREAK声明?

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

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