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);
}