2012-08-02 87 views
1

在我的应用程序中,我有三个异步事件。c中的异步事件#

所有这些都完成后,我需要调用一些Method1()。

我该如何实现这个逻辑?

更新

这里是我的异步事件之一:每个事件处理程序中设置

public static void SetBackground(string moduleName, Grid LayoutRoot) 
     { 
      var feedsModule = FeedHandler.GetInstance().ModulesSetting.Where(type => type.ModuleType == moduleName).FirstOrDefault(); 
      if (feedsModule != null) 
      { 
       var imageResources = feedsModule.getResources().getImageResource("Background") ?? 
            FeedHandler.GetInstance().MainApp.getResources().getImageResource("Background"); 

       if (imageResources != null) 
       { 

        //DownLoad Image 
        Action<BitmapImage> onDownloaded = bi => LayoutRoot.Background = new ImageBrush() { ImageSource = bi, Stretch = Stretch.Fill }; 
        CacheImageFile.GetInstance().DownloadImageFromWeb(new Uri(imageResources.getValue()), onDownloaded); 
       } 
      } 
     } 
+0

哪里是事件?在每一个事件我从网上 – nawfal 2012-08-02 08:03:05

+0

,它会更容易讲一个解决方案 – revolutionkpi 2012-08-02 08:03:42

+0

一些数据,如果u显示我们律”你的代码位 – nawfal 2012-08-02 08:04:38

回答

2

位字段(或3布尔值)。每个事件处理程序检查的条件得到满足,然后调用方法1()

tryMethod1() 
{ 
    if (calledEvent1 && calledEvent2 && calledEvent3) { 
     Method1(); 
     calledEvent1 = false; 
     calledEvent2 = false; 
     calledEvent3 = false; 
    } 
} 


eventHandler1() { 
    calledEvent1 = true; 
    // do stuff 
    tryMethod1(); 
} 
1

不因任何其他信息,什么工作是使用计数器。只是一个初始化为3的int变量,在所有处理程序中递减,并检查相等为0,并且该情况继续。

+0

一件事:如果你的事件在不同的线程解雇你需要的事件处理器同步。 – Mene 2012-08-02 08:09:51

+0

我应该如何同步事件处理程序? – revolutionkpi 2012-08-02 08:13:02

+0

使用互斥锁,或在c#中使用'lock'在所有3个处理程序中使用相同的引用。 – Mene 2012-08-02 08:14:36

0

你应该为此使用WaitHandles。这是一个简单的例子,但它应该给你的基本思路:

List<ManualResetEvent> waitList = new List<ManualResetEvent>() { new ManualResetEvent(false), new ManualResetEvent(false) }; 

    void asyncfunc1() 
    { 
     //do work 
     waitList[0].Set(); 
    } 
    void asyncfunc2() 
    { 
     //do work 
     waitList[1].Set(); 
    } 

    void waitFunc() 
    { 
     //in non-phone apps you would wait like this: 
     //WaitHandle.WaitAll(waitList.ToArray()); 
     //but on the phone 'Waitall' doesn't exist so you have to write your own: 
     MyWaitAll(waitList.ToArray()); 

    } 
    void MyWaitAll(WaitHandle[] waitHandleArray) 
    { 
     foreach (WaitHandle wh in waitHandleArray) 
     { 
      wh.WaitOne(); 
     } 
    }