如何等待,直到事件不会阻塞执行在UI完成 [英] How to wait until an event is finished without blocking execution in the UI

查看:121
本文介绍了如何等待,直到事件不会阻塞执行在UI完成的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图找出我怎么能确保该事件已经让code运行的其余部分之前被解雇。

I’m trying to figure out how can I make sure that the event has fired before letting the rest of the code run.

我勾了这样一个事件:

public static class ServiceUrlQueryParameters
{
      public static void ServiceUrlQueryParameters()
      {
          ... 
          dynamicMapServiceLayer.Initialized += new EventHandler<EventArgs>(DynamicMapServiceLayerQuery_Initialized); 
          ...
      }
}

所以,现在,在code有一个连接监听器的事件,并会等到事件触发。但我不希望任何其他在该类发生,直到该事件已解雇了,因为那个地图服务层的初始化设置带来了信息说的code需要的下一行。在code在应用程序和图形用户界面的其余部分应继续运行,但。

So now, the code has attached a listener to the event and will wait until the event fires. But I don't want anything else in that class to happen until that event has fired because that map service layer initialization brings sets up the info that the next lines of code need. The code in the rest of the application and the GUI should continue to run, though.

我曾经有过的code。在事件处理监听器的方法是这样的其余部分。

I used to have the rest of the code in the event handling listener method like this.

private void DynamicMapServiceLayer_Initialized(object sender, System.EventArgs evArgs) 
{ 
   Query query = GetParameterQuery(); 
   QueryTask queryTask = new QueryTask(GetRestURL(dynMapServLayer));
   queryTask.ExecuteCompleted += GraphicQueryTask_ExecuteCompleted; 
   ... 
   queryTask.ExecuteAsync(query); 
} 

但是,这没有任何意义,因为执行查询语义不相关的服务层得到初始化。 把查询code在运行时,地图图层初始化似乎没有牛逼的逻辑给我一个事件处理方法。 所以,现在我有这样的:

But that doesn't make sense because executing a query is not semantically related to a service layer getting initialized. Putting query code in an event handler method that runs when a map layer is initialized doesn’t seem t logical to me. So, now I have it like this:

public static class ServiceUrlQueryParameters
{
      public static void ServiceUrlQueryParameters()
      { 
          // No more Initialized event hookup to any event handling listener    
      }

    public static void QueryUrlParameters()
    {
        if ( ! dynMapServLayer.IsInitialized)
        {
            return; 
        }
        Query query = GetParameterQuery();
        QueryTask queryTask = new QueryTask(GetRestURL(dynMapServLayer));
        queryTask.ExecuteCompleted += GraphicQueryTask_ExecuteCompleted;
        queryTask.Failed += QueryTask_Failed;
        queryTask.ExecuteAsync(query);   
    }
}

但是,这不是个好主意,因为当QueryUrlParameters被调用时,初始化事件仍可能不会解雇(也许它永远不会)。

But that is not such a great idea because when the QueryUrlParameters is called, the Initialized event still might not have fired (and maybe it never will).

推荐答案

主要的问题是,执行查询语义不相关的服务层得到初始化,所以它没有任何意义,把查询中的层初始化事件的监听器方法。把查询code在运行时,地图图层初始化事件处理程序的方法是不符合逻辑的。

The main problem was that executing a query is not semantically related to a service layer getting initialized and so it didn't make sense to put the query in the layer initialization event's listener method. Putting query code in an event handler method that runs when a map layer is initialized isn't logical.

我还没有开始考查.NET 4.5任务具有平行库中的任何严肃的深度,所以我已经决定这样做。

I haven't yet started on examining the .NET 4.5 Task Parrallel library in any serious depth, so I've decided to do this.

在事件处理监听器的方法,我打了一个电话,以这样的查询方法。

In the event handling listener method, I made a call to the query method like this.

private void DynamicMapServiceLayer_Initialized(object sender, System.EventArgs evArgs) 
{ 
   QueryUrlParameters();
} 

然后我把参数查询过程中,有一个相匹配的责任名称的方法:

And then I keep the parameter querying procedure in a method that has a name that matches its responsibility:

public static void QueryUrlParameters()
{
   Query query = GetParameterQuery();
   QueryTask queryTask = new QueryTask(GetRestURL(dynMapServLayer));
   queryTask.ExecuteCompleted += GraphicQueryTask_ExecuteCompleted;
   queryTask.Failed += QueryTask_Failed;
   queryTask.ExecuteAsync(query);   
}

这是还没有这样一个很好的解决方案。照片 我将有preferred等待已初始化事件更优雅的方式;但看起来,这是我的一切,现在。

This is still not such a great solution.
I would have preferred a more elegant way of waiting for the event to have initialized; but it looks like this is all I've got for now.

这篇关于如何等待,直到事件不会阻塞执行在UI完成的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆