Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/309.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 是什么阻止我的事件处理程序方法完成?_C#_Wpf_Concurrency_Exception Handling_Event Handling - Fatal编程技术网

C# 是什么阻止我的事件处理程序方法完成?

C# 是什么阻止我的事件处理程序方法完成?,c#,wpf,concurrency,exception-handling,event-handling,C#,Wpf,Concurrency,Exception Handling,Event Handling,我使用一个SerialPort,WPF作为用户界面,一个“ui”对象来处理应用程序逻辑,以及一个围绕SerialPort的包装器 当触发SerialPort.DataReceived事件时,将调用所讨论的方法。它开始运行,然后在读取数据之后,似乎就退出了,尽管它还有很多事情要做 它似乎没有抛出任何异常,也没有表现得像卡在循环中一样(同步阅读应该超时+我会收到大量的“我读了一件东西”),我被难住了。几乎所有的GiveFeedback调用都是我试图调试正在发生的事情的结果,因为我无法在我正在编写这篇

我使用一个SerialPort,WPF作为用户界面,一个“ui”对象来处理应用程序逻辑,以及一个围绕SerialPort的包装器

当触发SerialPort.DataReceived事件时,将调用所讨论的方法。它开始运行,然后在读取数据之后,似乎就退出了,尽管它还有很多事情要做

它似乎没有抛出任何异常,也没有表现得像卡在循环中一样(同步阅读应该超时+我会收到大量的“我读了一件东西”),我被难住了。几乎所有的GiveFeedback调用都是我试图调试正在发生的事情的结果,因为我无法在我正在编写这篇文章的机器上进行测试

“ui”符号指的是一个对象,当调用它的GiveFeedback(字符串)时触发事件,最终通过调用Dispatcher.BeginInvoke(stuff)更新文本框的内容

以下是完整的代码:


public void DataReceived(对象发送方,SerialDataReceivedEventArgs e)
{
尝试
{
给予反馈(“收到数据…开始”);
列表字节=新列表();
int r=comm.comPort.ReadByte();
而(r!=-1)
{
字节。添加((字节)r);
尝试
{
r=comm.comPort.ReadByte();
}
捕获(异常ioe)
{
r=-1;
ui.GiveFeedback(ioe.Message);
}
给出反馈(“读取一个字节…”);
GiveFeedback(“读取:”+System.Text.Encoding.ASCII.GetString(bytes.ToArray())+“”;
}
//它永远不会到达这条线。
//它从不抛出任何类型的异常。
抛出新的ApplicationException(“我扔了自己”);
i、 GiveFeedback(“完成读取:”+System.Text.Encoding.ASCII.GetString(bytes.ToArray())+“”;
字符串msg=BitConverter.ToString(bytes.ToArray()).Replace(“-”,”);
字符串submsg;
如果(消息长度>5)
submsg=msg.Substring(0,5);
其他的
{
submsg=msg.Substring(0,2);
}
ui.GiveFeedback(“msg:+msg”);
ui.GiveFeedback(“submsg:+submsg”);
开关(submsg)
{
案例“10”:
ui.GiveFeedback(“'Incoming--VMC:Reset-10'--CommInterface”);
ACK();
打破
案例“00”:
//显示数据(MessageType.Incoming,“VMC:ACK-00\n”);
ui.GiveFeedback(“'Incoming--VMC:ACK-00'--CommInterface”);
打破
案例“12”:
GiveFeedback(“'Incoming--VMC:Poll-12'--CommInterface”);
开关(ui.Stage)
{
案例阶段。会话选择:
BeginSession();
Poll();
打破
违约:
ACK();
打破
}
打破
案例“14 01”:
//DisplayData(MessageType.Incoming,“VMC:启用读卡器-14 01\n”);
GiveFeedback(“'Incoming--VMC:Enable Reader-14 01'--CommInterface”);
ACK();
打破
案例“14 00”:
//DisplayData(MessageType.Incoming,“VMC:禁用读卡器-14 00\n”);
GiveFeedback(“'Incoming--VMC:Disable Reader-14 00'--CommInterface”);
ACK();
打破
案例“11 00”:
GiveFeedback(“'Incoming--VMC:Setup Config-11 00'--CommInterface”);
ConfigurationDataLevel01();
打破
案例“11 01”:
ui.GiveFeedback(“'Incoming--VMC:Setup Max-Min Price-00'--CommInterface”);
SetupMaxMinPrices();
打破
案例“13 00”:
//显示数据(MessageType.Incoming,“VMC:Vend请求-13 00\n”);
ui.GiveFeedback(“'Incoming--VMC:Vend Request-13 00'--CommInterface”);
if(ui.Stage==Stages.SessionSelect)
{
ui.NextStage();
ApproveVend();
}
其他的
DenyVend();
打破
案例“13 02”:
//显示数据(MessageType.Incoming,“VMC:Vend Success-13 02\n”);
ui.GiveFeedback(“'Incoming--VMC:VendSuccess-13 02'--CommInterface”);
ui.NextStage();
ACK();
打破
案例“1305”:
//DisplayData(MessageType.Incoming,“VMC:现金销售-13 05\n”);
ui.GiveFeedback(“'Incoming--VMC:Cash Sale-13 05'--CommInterface”);
ACK();
打破
案例“1304”:
//DisplayData(MessageType.Incoming,“VMC:会话完成-13 04\n”);
GiveFeedback(“'Incoming--VMC:sessioncomplete-1304'--CommInterface”);
ACK();
打破
案例“17 00”:
//显示数据(MessageType.Incoming,“VMC:Expansion命令-17 00\n”);
GiveFeedback(“'Incoming--VMC:Expansion Command-17 00'--CommInterface”);
打破
违约:
//显示数据(MessageType.Incoming,“未知\n”);
ui.GiveFeedback(“'Incoming--VMC:Unknown--?”--CommInterface”);
ACK();
打破
}
ui.GiveFeedback(“数据接收…结束”);
}
捕获(例外情况除外)
{
GiveFeedback(“抛出异常!”);
ui.给予反馈(例如信息);
ex=ex.InnerException;
while(ex!=null)
{
ui.给予反馈(例如信息);
ex=ex.InnerException;
}
}
}
我不知道发生了什么事
public void DataReceived(object sender, SerialDataReceivedEventArgs e)
{
    try
    {
        ui.GiveFeedback("DataReceived... Begin");
        List<byte> bytes = new List<byte>();
        int r = comm.comPort.ReadByte();
        while ( r != -1 )
        {
            bytes.Add((byte) r);
            try
            {
                r = comm.comPort.ReadByte();
            }
            catch(Exception ioe)
            {
                r = -1;
                ui.GiveFeedback(ioe.Message);
            }
            ui.GiveFeedback("read a byte...");
            ui.GiveFeedback("read : '" + System.Text.Encoding.ASCII.GetString(bytes.ToArray()) + "'");
        }
        //It NEVER reaches this line.
        //It NEVER throws an exception of any kind.
        throw new ApplicationException("I threw myself");
        i.GiveFeedback("done read : '" + System.Text.Encoding.ASCII.GetString(bytes.ToArray()) + "'");

        string msg = BitConverter.ToString(bytes.ToArray()).Replace("-", " ");

        string submsg;
        if ( msg.Length > 5 )
            submsg = msg.Substring(0, 5);
        else
        {
            submsg = msg.Substring(0, 2);
        }
        ui.GiveFeedback("msg: " + msg);
        ui.GiveFeedback("submsg: " + submsg);

        switch ( submsg )
        {
            case "10":
                ui.GiveFeedback("'Incoming -- VMC: Reset - 10' -- CommInterface");
                ACK();
                break;

            case "00":
                //DisplayData(MessageType.Incoming, " VMC: ACK - 00\n");
                ui.GiveFeedback("'Incoming -- VMC: ACK - 00' -- CommInterface");
                break;

            case "12":
                ui.GiveFeedback("'Incoming -- VMC: Poll - 12' -- CommInterface");
                switch ( ui.Stage )
                {
                    case Stages.SessionSelect:
                        BeginSession();
                        Poll();
                        break;
                        default:
                            ACK();
                            break;
                }
                break;

            case "14 01":
                //DisplayData(MessageType.Incoming, " VMC: Enable Reader - 14 01\n");
                ui.GiveFeedback("'Incoming -- VMC: Enable Reader - 14 01' -- CommInterface");
                ACK();
                break;

            case "14 00":
                //DisplayData(MessageType.Incoming, " VMC: Disable Reader - 14 00\n");
                ui.GiveFeedback("'Incoming -- VMC: Disable Reader - 14 00' -- CommInterface");
                ACK();
                break;

            case "11 00":
                ui.GiveFeedback("'Incoming -- VMC: Setup Config - 11 00' -- CommInterface");
                ConfigurationDataLevel01();
                break;

            case "11 01":
                ui.GiveFeedback("'Incoming -- VMC: Setup Max Min Price - 00' -- CommInterface");
                SetupMaxMinPrices();
                break;

            case "13 00":
                //DisplayData(MessageType.Incoming, " VMC: Vend Request - 13 00\n");
                ui.GiveFeedback("'Incoming -- VMC: Vend Request - 13 00' -- CommInterface");
                if ( ui.Stage == Stages.SessionSelect )
                {
                    ui.NextStage();
                    ApproveVend();
                }
                else
                    DenyVend();
                break;

            case "13 02":
                //DisplayData(MessageType.Incoming, " VMC: Vend Success - 13 02\n");
                ui.GiveFeedback("'Incoming -- VMC: VendSuccess - 13 02' -- CommInterface");
                ui.NextStage();
                ACK();
                break;

            case "13 05":
                //DisplayData(MessageType.Incoming, " VMC: Cash Sale - 13 05\n");
                ui.GiveFeedback("'Incoming -- VMC: Cash Sale - 13 05' -- CommInterface");
                ACK();
                break;

            case "13 04":
                //DisplayData(MessageType.Incoming, " VMC: Session Complete - 13 04\n");
                ui.GiveFeedback("'Incoming -- VMC: Session Complete - 13 04' -- CommInterface");
                ACK();
                break;

            case "17 00":
                //DisplayData(MessageType.Incoming, " VMC: Expansion Command - 17 00\n");
                ui.GiveFeedback("'Incoming -- VMC: Expansion Command - 17 00' -- CommInterface");
                break;

            default:
                //DisplayData(MessageType.Incoming, "unknown\n");
                ui.GiveFeedback("'Incoming -- VMC: Unknown - ?? ??' -- CommInterface");
                ACK();
                break;
        }
        ui.GiveFeedback("DataReceived... End");
    }
    catch(Exception ex)
    {
        ui.GiveFeedback("Exception Thrown!");
        ui.GiveFeedback(ex.Message);
        ex = ex.InnerException;
        while (ex != null)
        {
            ui.GiveFeedback(ex.Message);
            ex = ex.InnerException;
        }
    }
}