由于线程结束或应用程序请求,I / O被中止

时间:2018-01-10 13:23:46

标签: c# multithreading serial-port ioexception

我看了很多关于此的话题,但没有结果。我正在建立一个程序,它自动从测量设备接收数据。我尝试打开/关闭ComPort,有时它可以工作,但有时它会在我关闭端口时抛出IOException。无论我做什么,这个IoException仍然会出现。

  

I / O操作因线程退出或中断而中止   申请请求。

我认为,当我关闭Comport时,程序仍在接收并处理来自测量设备的数据,并导致异常。或者也许是因为线程?

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.IO.Ports;
using System.Windows.Forms;
  

form1.cs中的代码

    private void Form1_Load(object sender, EventArgs e)
    {

        string[] ArrayComPortsNames;
        ArrayComPortsNames = SerialPort.GetPortNames();
        if (ArrayComPortsNames.Length == 0) return;
        Array.Sort(ArrayComPortsNames);
        foreach (var v in ArrayComPortsNames)
            cBoxCom.Items.Add(v);
        cBoxCom.SelectedIndex = 0;
    }
    private void cBoxCom_SelectedIndexChanged(object sender, EventArgs e)
    {
        cBoxCom.Text = cBoxCom.SelectedText;
    }
    private void btnConnect_Click(object sender, EventArgs e)
    {
        if (String.IsNullOrEmpty(cBoxCom.Text)) { MessageBox.Show("Please chose one comport."); return; }
        if (btnConnect.Text == "Connect")
        {
            pt340.ComPort.PortName = cBoxCom.Text;
            pt340.Open();
            pt340.ComPort.DataReceived += new SerialDataReceivedEventHandler(_Comport_DataReceived);
            btnConnect.Text = "Close";
        }
        else if(btnConnect.Text=="Close")
        {
            pt340.CLose();
            btnConnect.Text = "Connect";
        }
    }
    private void _Comport_DataReceived(object sender, SerialDataReceivedEventArgs e)
    {

        pt340.Translate();
        SetText(pt340.GetValue() + "\r\n"
                + pt340.GetMeasurement() + "\r\n" );

    }
    bool isClose = false;
    // show the output
    private void SetText(string text)
    {
        if (rBox.InvokeRequired && isClose==false)
        {
            SetTextCallback d = new SetTextCallback(SetText);
            Invoke(d, new object[] { text });
        }
        else
        {
            rBox.Text += text;
        }
    }

    private void Form1_FormClosing(object sender, FormClosingEventArgs e)
    {
        pt340.ComPort.DataReceived -= new SerialDataReceivedEventHandler(_Comport_DataReceived);
        pt340.CLose();
        isClose = true;
    }
    private void rBox_TextChanged(object sender, EventArgs e)
    {
        rBox.SelectionStart = rBox.Text.Length;
        rBox.ScrollToCaret();
    }
}

命名空间PeakTechApp {     公共部分类Form1:表格     {         Pt340 pt340 =新Pt340();         delegate void SetTextCallback(string text);         公共Form1()         {             的InitializeComponent();         }

POST [Organization URI]/api/data/v8.2/accounts HTTP/1.1
Content-Type: application/json; charset=utf-8
OData-MaxVersion: 4.0
OData-Version: 4.0
Accept: application/json
{
    "name": "Sample Account",
    "creditonhold": false,
    "address1_latitude": 47.639583,
    "description": "This is the description of the sample account",
    "revenue": 5000000,
    "accountcategorycode": 1
}

}

0 个答案:

没有答案