捕获块永远不会在首次启动应用程序时返回

时间:2018-12-26 15:55:28

标签: c# wpf xaml

我有以下代码:

public partial class MainWindow : Window
{
    private BackgroundWorker backgroundConnect = new BackgroundWorker();

    public MainWindow()
    {
        InitializeComponent();
        backgroundConnect = (BackgroundWorker)FindResource("BackgroundConnect");
    }
    private void ConnectNow_MouseUp(object sender, MouseButtonEventArgs e)
    {
        try
        {
            //Getting IP and port from UI text boxes here, combine them into IPEndpoint
            if (backgroundConnect.IsBusy == false)
                backgroundConnect.RunWorkerAsync(InputIPEndpoit);
        }
        catch (Exception ex)
        {
            ResponseOutputBox.Text     = ex.Message;                
        }            
    }

    private void BackgroundConnect_DoWork(object sender, DoWorkEventArgs e)
    {            
        IPEndPoint InputIPEndpoit = (IPEndPoint)e.Argument;
        e.Result = SemTCPCommandClient.SendReceiveCommand(InputIPEndpoit);           //Method to transfer command to remote host
    }

    private void BackgroundConnect_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
    {            
        if (e.Error != null)
        {
            ResponseOutputBox.Text = e.Error.ToString();          //Here execution falls in case unsuccessful connection attempt is done after first start
        }
        else if (e.Cancelled)
        {
            //Never fires 'cause no CANCEL is ever called
        }
        else
            ResponseOutputBox.Text = e.Result.ToString();
    }
}

public class SemTCPCommandClient
    {        
        private static TcpClient Client;
        private static NetworkStream IncomingStream;
        const string Command = "Test_command";

        public static string SendReceiveCommand(IPEndPoint iPEndPoint)
        {
            try
            {                
                Client = new TcpClient();                
                Client.Connect(iPEndPoint);
                IncomingStream = Client.GetStream();
                byte[] payLoad = ASCIIEncoding.ASCII.GetBytes(Command);        
                IncomingStream.Write(payLoad, 0, payLoad.Length);
                byte[] IncomingPacket = new byte[256];                                                                      //buffer for incoming string from server
                IncomingStream.Read(IncomingPacket, 0, IncomingPacket.Length);
                string IncomingString = Encoding.ASCII.GetString(IncomingPacket);                                           //COnverting bytes to string.
                Client.Close();
                IncomingStream.Close();
                return IncomingString;
            }
            catch (SocketException e)
            {
                Client.Close();
                IncomingStream.Close();
                return Convert.ToString(e.SocketErrorCode);                                                                 //This is NEVER executed at first application start
            }
            catch (Exception e)
            {
                Client.Close();
                IncomingStream.Close();
                return e.Message;
            }
        }
    }

如果我启动我的应用程序,则在没有服务器运行时单击“连接”按钮,我会遇到异常:System.NullReferenceException抛出了BackgroundConnect_RunWorkerCompleted。我试图在return的{​​{1}}块中将断点设置为catch,但从未达到。但是,如果在第一个应用程序启动后首次连接尝试成功(服务器已启动),则所有操作均按预期进行。即使关闭服务器,也会执行catch返回,例如显示“ ConnectionRefused”。 我想这是一个明显的问题,但是我已经花了3个小时来尝试... 编辑:我知道我应该收到SendReceiveCommand,因为来自catch的NullReferenceException被忽略了。我看不到它被忽略的原因,也不明白从原则上如何可以从非return忽略return

编辑:

问题是void也关闭了相应的流,所以我的Client.Close()块抛出了异常。感谢@ daniel-mann和@ivkremer。重写代码:

catch

0 个答案:

没有答案