如何在C#中异步调用任何方法


110

有人可以给我看一小段代码,演示如何在c#中异步调用方法吗?

Answers:


131

如果使用action.BeginInvoke(),则必须在某个地方调用EndInvoke-否则框架必须在堆上保存异步调用的结果,从而导致内存泄漏。

如果您不希望使用async / await关键字跳至C#5,则可以只使用.Net 4中的Task Parallels库。它比使用BeginInvoke / EndInvoke更好,并且提供了一种干净的方法来触发-并忘记异步作业:

using System.Threading.Tasks;
...
void Foo(){}
...
new Task(Foo).Start();

如果您有调用带有参数的方法,则可以使用lambda简化调用,而不必创建委托:

void Foo2(int x, string y)
{
    return;
}
...
new Task(() => { Foo2(42, "life, the universe, and everything");}).Start();

我非常确定(但肯定不是肯定的),C#5 async / await语法只是Task库周围的语法糖。


2
如果还不清楚,那么最终的假设是:async / await是正确的,但是它将极大地改变代码的外观。
2014年

我正在尝试使用创建事件然后委托的方法来进行此操作,对吗?如果是这样,我如何结束任务。干杯
Joster


24

这是一种实现方法:

// The method to call
void Foo()
{
}


Action action = Foo;
action.BeginInvoke(ar => action.EndInvoke(ar), null);

当然Action,如果方法具有不同的签名,则需要替换为另一种类型的委托


1
当我们调用foo时,我该如何传递您未显示的参数?
托马斯

您可以放置​​一个对象代替null。让Foo接受一个类型为object的输入参数。然后,您必须在Foo中将对象转换为适当的类型。
丹妮丝·斯基德莫尔

4

如果您负担得起新手知识,请查看MSDN文章“ 使用Async和Await进行异步编程”。它已添加到.NET 4.5。

链接的示例代码片段(其本身来自此MSDN示例代码项目):

// Three things to note in the signature: 
//  - The method has an async modifier.  
//  - The return type is Task or Task<T>. (See "Return Types" section.)
//    Here, it is Task<int> because the return statement returns an integer. 
//  - The method name ends in "Async."
async Task<int> AccessTheWebAsync()
{ 
    // You need to add a reference to System.Net.Http to declare client.
    HttpClient client = new HttpClient();

    // GetStringAsync returns a Task<string>. That means that when you await the 
    // task you'll get a string (urlContents).
    Task<string> getStringTask = client.GetStringAsync("http://msdn.microsoft.com");

    // You can do work here that doesn't rely on the string from GetStringAsync.
    DoIndependentWork();

    // The await operator suspends AccessTheWebAsync. 
    //  - AccessTheWebAsync can't continue until getStringTask is complete. 
    //  - Meanwhile, control returns to the caller of AccessTheWebAsync. 
    //  - Control resumes here when getStringTask is complete.  
    //  - The await operator then retrieves the string result from getStringTask. 
    string urlContents = await getStringTask;

    // The return statement specifies an integer result. 
    // Any methods that are awaiting AccessTheWebAsync retrieve the length value. 
    return urlContents.Length;
}

报价:

如果AccessTheWebAsync在调用GetStringAsync和等待其完成之间没有任何工作,则可以通过以下单个语句中的调用和等待来简化代码。

string urlContents = await client.GetStringAsync();

链接中有更多详细信息。


我将如何使用这项技术并设置超时时间?
Su Llewellyn

1
public partial class MainForm : Form
{
    Image img;
    private void button1_Click(object sender, EventArgs e)
    {
        LoadImageAsynchronously("http://media1.santabanta.com/full5/Indian%20%20Celebrities(F)/Jacqueline%20Fernandez/jacqueline-fernandez-18a.jpg");
    }

    private void LoadImageAsynchronously(string url)
    {
        /*
        This is a classic example of how make a synchronous code snippet work asynchronously.
        A class implements a method synchronously like the WebClient's DownloadData(…) function for example
            (1) First wrap the method call in an Anonymous delegate.
            (2) Use BeginInvoke(…) and send the wrapped anonymous delegate object as the last parameter along with a callback function name as the first parameter.
            (3) In the callback method retrieve the ar's AsyncState as a Type (typecast) of the anonymous delegate. Along with this object comes EndInvoke(…) as free Gift
            (4) Use EndInvoke(…) to retrieve the synchronous call’s return value in our case it will be the WebClient's DownloadData(…)’s return value.
        */
        try
        {
            Func<Image> load_image_Async = delegate()
            {
                WebClient wc = new WebClient();
                Bitmap bmpLocal = new Bitmap(new MemoryStream(wc.DownloadData(url)));
                wc.Dispose();
                return bmpLocal;
            };

            Action<IAsyncResult> load_Image_call_back = delegate(IAsyncResult ar)
            {
                Func<Image> ss = (Func<Image>)ar.AsyncState;
                Bitmap myBmp = (Bitmap)ss.EndInvoke(ar);

                if (img != null) img.Dispose();
                if (myBmp != null)
                    img = myBmp;
                Invalidate();
                //timer.Enabled = true;
            };
            //load_image_Async.BeginInvoke(callback_load_Image, load_image_Async);             
            load_image_Async.BeginInvoke(new AsyncCallback(load_Image_call_back), load_image_Async);             
        }
        catch (Exception ex)
        {

        }
    }
    protected override void OnPaint(PaintEventArgs e)
    {
        if (img != null)
        {
            Graphics grfx = e.Graphics;
            grfx.DrawImage(img,new Point(0,0));
        }
    }
By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.