处理C#中异步方法中发生的异常 [英] Handle a exception that happens in a asynchronous method in C#

查看:484
本文介绍了处理C#中异步方法中发生的异常的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

从现在起,我是异步编码的初学者,只是看不到在此示例中可以做什么。



我有两个项目,一个有与某个服务进行通信的类,另一个是WinForms项目。



因此,我在WinForms中有另一个项目的引用,该引用使我可以创建



问题是因为这个原因,我不能使用System.Windows.Forms.MessageBox。

p>

然后,我会在Form类中使用try ... catch,但是...现在的问题是我无法获取异常,系统只是



因此,这是我的小项目,其中的类包装了连接到服务器的逻辑。 / p>

(ps:当我指的是项目时,我的意思是我在Visual Studio 2013中有一个包含两个项目的解决方案,一个类库和一个Win Forms应用程序)

  p公共类客户端
{
public客户端(字符串主机,int端口)
{
主机=主机;
Porta =端口;

Client =新的TcpClient();

Connect();
}

公共字符串Host {get;组; }
public int Port {get;组; }

公共TcpClient客户端{组; }

私有异步void Connect()
{
等待Client.ConnectAsync(Host,Port);
}
}

这是另一个具有主要形式的项目。 / p>

 公共局部类Form1:表单
{
public Form1()
{
InitializeComponent();
}

私有无效Form1_Load(对象发送者,EventArgs e)
{
try
{
Client client = new Client( localhost ,8080);
}
catch(SocketException socketException)
{
MessageBox.Show(socketException.Message);
}
}
}


解决方案

避免异步作废。您不能捕获从 async void 方法抛出的异常。

 公共异步任务ConnectAsync()
{
等待Client.ConnectAsync(Host,Port);
}

private void Form1_Load(对象发送者,EventArgs e)
{
try
{
Client client = new Client( localhost ,8080);
等待client.ConnectAsync();
}
catch(SocketException socketException)
{
MessageBox.Show(socketException.Message);
}
}


From now I'm a beginner in Async coding, I just can't see what I could do in this example.

I have two projects, one have the classes to communicate to a certain service, and the another one is the WinForms project.

So I have a reference in the WinForms from the other project that allows me to create instances of the Client object to use it like I want.

The problem is, because of that, I can't use System.Windows.Forms.MessageBox.

Then, I'd use the try...catch in the Form class, but... The problem now is that I can't get the exception, the system just don't grab it and continues to execute untill the app crash itself.

So, here's my little project with the class that wraps the logic to connect into the server.

(ps.: When I mean projects, I mean that I have a solution in Visual Studio 2013 with two projects, a Class Library and a Win Forms Application)

public class Client
{
    public Client(string host, int port)
    {
        Host = host;
        Porta = port;

        Client = new TcpClient();

        Connect();
    }

    public string Host { get; set; }
    public int Port { get; set; }

    public TcpClient Client { get; set; }

    private async void Connect()
    {
        await Client.ConnectAsync(Host, Port);
    }
}

And here's the other project with the main form.

public partial class Form1 : Form
{
    public Form1()
    {
        InitializeComponent();
    }

    private void Form1_Load(object sender, EventArgs e)
    {
        try
        {
            Client client = new Client("localhost", 8080);
        }
        catch (SocketException socketException)
        {
            MessageBox.Show(socketException.Message);
        }
    }
}

解决方案

Avoid async void. You cannot catch exceptions thrown from async void methods.

public async Task ConnectAsync()
{
    await Client.ConnectAsync(Host, Port);
}

private void Form1_Load(object sender, EventArgs e)
{
    try
    {
        Client client = new Client("localhost", 8080);
        await client.ConnectAsync();
    }
    catch (SocketException socketException)
    {
        MessageBox.Show(socketException.Message);
    }
}

这篇关于处理C#中异步方法中发生的异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆