.NET SerialPort DataReceived event not firing

前端 未结 9 685
忘掉有多难
忘掉有多难 2020-12-09 22:52

I have a WPF test app for evaluating event-based serial port communication (vs. polling the serial port). The problem is that the DataReceived event doesn\'t seem to be fir

9条回答
  •  情歌与酒
    2020-12-09 23:30

    I ran into a similarly odd problem recently, but only on some machines. As Dave Swersky noted, this may have been a threading issue, especially if you were running under .NET 4.0 or later.

    In .NET 4.0 the event handler is triggered on a ThreadPool thread, and under certain circumstances there may be a substantial delay before that occurs. (In my code, which had been working perfectly under .NET 2.0, problems were observed as soon as we upgraded to .NET 4.5. The event handler would often be triggered much later than would be expected, and sometimes it would not get triggered at all!)

    Calling ThreadPool.SetMinThreads(...) with a bigger value for the completion threads made the issue disappear as quickly as it had arrived. In the context of our application ThreadPool.SetMinThreads(2, 4) was sufficient. On the machines where the problem was observed, the default values (as obtained by calling ThreadPool.SetMinThreads) were both 2.

提交回复
热议问题