Link to home
Start Free TrialLog in
Avatar of peter1950
peter1950

asked on

Convert C# in VB 2005

Developing apps for WM6, frequently I have problems with serial port that hangs when I close it. I've found on a forum this code but I don't be able to convert in VB 2005.
Can somebody help me ?
private void Form1_FormClosing(object sender, FormClosingEventArgs e)
 
{
 
if (serialPort1.IsOpen)
 
{
 
e.Cancel = true; //cancel the fom closing
 
Thread CloseDown = new Thread(new ThreadStart(CloseSerialOnExit)); //close port in new thread to avoid hang
 
CloseDown.Start(); //close port in new thread to avoid hang
 
}
 
}
 
private void CloseSerialOnExit()
 
{
 
try
 
{
 
serialPort1.Close(); //close the serial port
 
}
 
catch (Exception ex)
 
{
 
MessageBox.Show(ex.Message); //catch any serial port closing error messages
 
}
 
this.Invoke(new EventHandler(NowClose)); //now close back in the main thread
 
}
 
private void NowClose(object sender, EventArgs e)
 
{
 
this.Close(); //now close the form
 
}

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of QPR
QPR
Flag of New Zealand image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial