Custom Search

วันศุกร์ที่ 18 พฤษภาคม พ.ศ. 2555

การ stop port serial และหยุด Thread ไม่ให้โปรแกรมค้าง

การ stop port serial และหยุด Thread ไม่ให้โปรแกรมค้าง ไปลอกเขามาอีกทีแต่จำเวปไม่ได้แล้ว

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();
    }

}

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

}

2 ความคิดเห็น:

ไม่ระบุชื่อ กล่าวว่า...

My family every time say that I am wasting my time here at web, except I know I am getting experience every day by reading thes fastidious articles or reviews.
my web page - simply click the up coming website page

ไม่ระบุชื่อ กล่าวว่า...

I really like your blog.. very nice colors &
theme. Did you design this website yourself or did you hire someone to do it for you?

Plz respond as I'm looking to create my own blog and would like to know where u got this from. cheers

Review my blog post - Miami diet centers
My page > Miami diet centers