This is because once Write starts, it calls the m_write_func() which then checks m_stop_write to see if the port has been disabled. If it has, it cancels the write and assumes that the port has closed. Unfortunately, if the write has just fired, it will be sitting waiting for m_written_ev, and once m_write_func() has completed, it will never set m_written_ev, and no future calls to Write will get through because the original is still waiting.
In m_write_func() I added a call to m_written.ev.Set() for when m_stop_write is true. This releases any Writes currently waiting. Note it's also important to reset m_written_ev in Write before calling m_write_func().
So...
Code: Select all
if(m_stop_write) { // Thread ended (port closed) run_thread = false; m_written_ev.Set(); // ADD THE SET HERE }
Code: Select all
// Only one thread might be inside this function at a time. Monitor.Enter(m_write_lock); _threadEntries++; int bytes_written = 0; m_written_ev.Reset(); // ADD THE RESET HERE m_check_port_enabled(true);