Azure / DotNetty

DotNetty project – a port of netty, event-driven asynchronous network application framework
Other
4.09k stars 977 forks source link

Task new thread use ManualResetEvent why dotnetty stop WaitOne(), I dot not user Dotnetty thread #466

Open chenderong opened 5 years ago

chenderong commented 5 years ago

`
var bootstrap = new Bootstrap(); bootstrap .Group(group) .Channel() .Option(ChannelOption.TcpNodelay, true) .Handler(new ActionChannelInitializer(channel => { IChannelPipeline pipeline = channel.Pipeline; pipeline.AddLast(new NLogHandler("BDDataClientService")); pipeline.AddLast("IdleStateHandler", new IdleStateHandler(300, 60, 300)); pipeline.AddLast("DBTerIdelStateMyHandler", new DBDataIdelStateMyHandler()); pipeline.AddLast("BeiDouFrameDecoder", new BeiDouFrameDecoder(BDFrameConst.FRAME_FLAG, BDFrameConst.FRAME_MAX_LEN, BDFrameConst.FRAME_Min_LEN)); pipeline.AddLast("BDClientBeiDouContentDecoderHandler", new BeiDouContentDecoderHandler(this._optionsMonitor)); pipeline.AddLast("BeiDouContentEncoderHandler", new BeiDouContentEncoderHandler()); pipeline.AddLast("BDClientHandler", new BDDataClientHandler()); }));

BDDataClientHandler:

public class BDDataClientHandler : ChannelHandlerAdapter
{
    private readonly NLog.Logger _loggerInfo;
    private readonly NLog.Logger _loggerError;
    private const string jobKey = "BDDataClientHandler";
    private const string triggerKey = "BDDataClientHandler";

    private PlatSendMessageToTerProcessing _platSendMessageToTerProcessing;
    public BDDataClientHandler()
    {
        _loggerInfo = NLog.LogManager.GetLogger("Info");
        _loggerError = NLog.LogManager.GetLogger("");
        _platSendMessageToTerProcessing = new PlatSendMessageToTerProcessing();
       `  _platSendMessageToTerProcessing.Start();`
    }

    public override void ChannelRead(IChannelHandlerContext context, object message)
    {
        try
        {
            ChannelMessage channelMessage = message as ChannelMessage;
            if (channelMessage != null)
                _platSendMessageToTerProcessing.PushMessage(channelMessage);
        }
        catch (Exception ex)
        {

        }
    }

}

PlatSendMessageToTerProcessing

private ManualResetEvent _pmqMRE = new ManualResetEvent(false);

   private void DoingWork()
    {
        Task.Factory.StartNew(() =>
        {
            while (_isWork)
            {
                try
                {
                    _pmqMRE.WaitOne();
                    while (_platMessageQueue.Count > 0)
                    {
                        ChannelMessage channelMessage;
                        if (_platMessageQueue.TryDequeue(out channelMessage))
                        {
                            SendMessageToTer(channelMessage);
                        }
                    }
                    _pmqMRE.Reset();
                }
                catch (Exception ex)
                {

                }
            }
        });
    }

`

_pmqMRE.WaitOne(); I dot not user Dotnetty thread,dotnetty stop there why? thanks a lot

chenderong commented 5 years ago

pipeline.AddLast("BDClientHandler", new BDDataClientHandler()); PlatSendMessageToTerProcessing Task.Factory.StartNew _pmqMRE.WaitOne();

dotnetty stop WaitOne why?

chenderong commented 5 years ago

use Task.Run replace Task.Factory.StartNew
it is ok