0

I'm making an application that is reading the serial port and updating data in a WindowsForms application screen.

Sometimes when you try to close the program is locked. I'm using delegate.

What should I be doing wrong?

void sp1_LineReceived(object sender, LineReceivedEventArgs Args)
{
  Invoke(new Action(() =>
  {
    // execute code
  }));
}

Init

public FormPrincipal()
{
  InitializeComponent();
  spSistema.LineReceived += new LineReceivedEventHandler(sp1_LineReceived);
  // next codes
}

Outher codes

public partial class FormPrincipal : Form
{       
  SerialPort spSimulador = new SerialPort();        
  public static PortaSerial spSistema = new PortaSerial();
Tiedt Tech
  • 719
  • 15
  • 46

1 Answers1

2

In general, BeginInvoke is preferable to Invoke, as it won't block. See here. You may well have a deadlock here.

void sp1_LineReceived(object sender, LineReceivedEventArgs Args)
{
  BeginInvoke(new Action(() =>
  {
    // execute code
  }));
}
Community
  • 1
  • 1
Jon B
  • 51,025
  • 31
  • 133
  • 161