如何创建一个异步方法 [英] How to create an asynchronous method

查看:124
本文介绍了如何创建一个异步方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有简单的方法在我的C#应用​​程序,它从FTP服务器上挑选文件,并对其进行解析,并在数据库中存储的数据。我想这是异步的,因此该用户进行应用上的其他操作,一旦解析完成他已经得到消息,指出解析完成。

I have simple method in my C# app, it picks file from FTP server and parses it and stores the data in DB. I want it to be asynchronous, so that user perform other operations on App, once parsing is done he has to get message stating "Parsing is done".

我知道它可以通过异步方法调用实现,但我不知道该怎么做任何人可以帮我请??

I know it can achieved through asynchronous method call but I dont know how to do that can anybody help me please??

推荐答案

您需要使用代表和它们包含的异步运行的另一种方法的的BeginInvoke 方法。该方法的结束被委托运行,可以通知用户。例如:

You need to use delegates and the BeginInvoke method that they contain to run another method asynchronously. A the end of the method being run by the delegate, you can notify the user. For example:

class MyClass
{
    private delegate void SomeFunctionDelegate(int param1, bool param2);
    private SomeFunctionDelegate sfd;

    public MyClass()
    {
        sfd = new SomeFunctionDelegate(this.SomeFunction);
    }

    private void SomeFunction(int param1, bool param2)
    {
        // Do stuff

        // Notify user
    }

    public void GetData()
    {
        // Do stuff

        sfd.BeginInvoke(34, true, null, null);
    }
}

在<一起来阅读href=\"http://msdn.microsoft.com/en-us/library/2e08f6yc.aspx\">http://msdn.microsoft.com/en-us/library/2e08f6yc.aspx

这篇关于如何创建一个异步方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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