unmvyd

博客园 首页 新随笔 联系 订阅 管理
private BackgroundWorker m_MessageConsumer = new BackgroundWorker();
private BlockingCollection<string> m_BlockingQueue = new BlockingCollection<string>();
构造函数
{
m_MessageConsumer.DoWork += M_MessageConsumer_DoWork;
    m_MessageConsumer.WorkerSupportsCancellation = true;
}
析构函数
{
m_MessageConsumer?.CancelAsync();
            m_MessageConsumer?.Dispose();
}
private void ClicentConnected(...)
{
    
    m_MessageConsumer.RunWorkerAsync();

}

private void M_MessageConsumer_DoWork(object sender, DoWorkEventArgs e)
        {
            try
            {
                while (m_U3DRunning)
                {
                    if (!m_BlockingQueue.IsCompleted)
                    {
                        string send = m_BlockingQueue.Take();
                        LogHelper.Debug(send);
                        m_PipeServer.PushMessage(send);
                    }

                }

            }
            catch (Exception ex)
            {
                LogHelper.Error(ex.Message, ex.StackTrace);
            }

        }
public void PushMessage(string send)
        {
            m_BlockingQueue.Add(send);
        }
posted on 2024-08-16 09:36  准爵  阅读(2)  评论(0编辑  收藏  举报