Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/apache-spark/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# c语言中的异步事件#_C#_Windows Phone 7 - Fatal编程技术网

C# c语言中的异步事件#

C# c语言中的异步事件#,c#,windows-phone-7,C#,Windows Phone 7,在我的应用程序中,我有三个异步事件 所有这些都完成后,我需要调用一些Method1() 我如何实现这个逻辑 更新 以下是我的一个异步事件: public static void SetBackground(string moduleName, Grid LayoutRoot) { var feedsModule = FeedHandler.GetInstance().ModulesSetting.Where(type => type.ModuleT

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

所有这些都完成后,我需要调用一些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);
                }
            }
        }
publicstaticvoidsetbackground(字符串moduleName,网格布局root)
{
变量feedsModule=FeedHandler.GetInstance().ModuleSetting.Where(type=>type.ModuleType==moduleName.FirstOrDefault();
如果(feedsModule!=null)
{
var imageResources=feedsModule.getResources().getImageResource(“背景”)??
FeedHandler.GetInstance().MainApp.getResources().getImageResource(“背景”);
if(imageResources!=null)
{
//下载图像
操作onDownloaded=bi=>LayoutRoot.Background=new ImageBrush(){ImageSource=bi,Stretch=Stretch.Fill};
CacheImageFile.GetInstance().DownloadImageFromWeb(新Uri(imageResources.getValue()),OnDownload);
}
}
}
每个事件处理程序设置的位字段(或3个布尔值)。每个事件处理程序检查条件是否满足,然后调用Method1()


在没有其他信息的情况下,使用计数器是可行的。只是一个初始化为3的int变量,在所有处理程序中递减,并检查是否等于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();
        }
    }
List waitList=new List(){new ManualResetEvent(false),new ManualResetEvent(false)};
void asyncfunc1()
{
//工作
等待列表[0]。集合();
}
void asyncfunc2()
{
//工作
等待列表[1]。集合();
}
void waitFunc()
{
//在非手机应用程序中,您将按如下方式等待:
//WaitHandle.WaitAll(waitList.ToArray());
//但在手机上“Waitall”并不存在,所以你必须自己写:
MyWaitAll(waitList.ToArray());
}
void MyWaitAll(WaitHandle[]waitHandleArray)
{
foreach(waitHandleArray中的waithandlewh)
{
wh.WaitOne();
}
}

在每次事件中,我都会从Internet上获得一些数据如果您向我们展示少量代码,那么告诉解决方案就更容易了。还有一件事:如果您的事件在不同的线程中触发,您需要同步EventHandler。我应该如何同步EventHandler?使用互斥锁,或者在c#中,在所有3个处理程序中使用具有相同引用的
lock
    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();
        }
    }