簡體   English   中英

如何在c#中異步調用任何方法

[英]How to call any method asynchronously in c#

有人可以給我看一小段代碼,演示如何在 C# 中異步調用方法嗎?

如果您使用 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 庫的語法糖。

從 .Net 4.5 開始,您可以使用 Task.Run 來簡單地開始一個動作:

void Foo(string args){}
...
Task.Run(() => Foo("bar"));

Task.Run 與 Task.Factory.StartNew

這是一種方法:

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


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

當然,如果方法具有不同的簽名,則需要將Action替換為另一種類型的委托

如果您有能力嘗試新東西,請查看 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();

更多詳細信息在鏈接中。

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));
        }
    }

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM