2011-04-06 61 views

回答

99

如果使用action.BeginInvoke(),則表示have to call 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語法是合理的t任務庫周圍的語法糖。

+2

如果還不清楚,最後的async re:async/await是正確的,但它會顯着改變代碼的外觀。 – Gusdor 2014-11-21 13:25:54

+0

我試圖用創建事件然後委託的方法,這是正確的嗎?如果是這樣,我該如何結束這項任務。乾杯 – Joster 2018-01-05 10:45:55

24

這裏有一個辦法做到這一點:你需要使用其它類型的委託,以取代Action

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


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

當然,如果你可以在方法具有不同的簽名

+1

當我們調用foo然後我如何可以傳遞參數,ü沒有表現出? – Thomas 2013-03-27 07:39:24

+0

@Thomas,我不明白你的問題...... – 2013-03-27 10:27:48

+0

代替null你可以放一個對象。讓Foo帶一個輸入參數的類型對象。然後您必須將對象轉換爲Foo中的適當類型。 – 2017-03-02 22:50:54

4

退房MSDN文章Asynchronous Programming with Async and Await負擔得起玩新的東西。它被添加到.NET 4.5中。從鏈接

示例代碼片段(它本身從this MSDN sample code project):

// 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(); 

更多細節都在link

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