Silverlight:异步调用中的 WaitOne 同步未按预期运行

本文关键字:运行 同步 WaitOne 异步 调用 Silverlight | 更新日期: 2023-09-27 18:33:17

我正在尝试使用 ManualResetEvent 来阻止,直到执行回调,但从未到达回调,即使我尝试在另一个线程上运行回调也是如此。

waitHandle = new ManualResetEvent(false);
DataServiceQueryer<MyEntity> dataServiceQueryer = new DataServiceQueryer<MyEntity>(dsQuery.Expression);
ThreadPool.QueueUserWorkItem(new WaitCallback(stateInfo =>
{
    dataServiceQueryer.ExecuteQuery();
}));
// waits here forever
waitHandle.WaitOne();
public class DataServiceQueryer<T> 
{
    //field, properties
    public void ExecuteQuery()
    {
        // this block is definitely executed
        _asyncResult = _query.BeginExecute(new AsyncCallback(c =>
        {
            // this is never reached
            QueryOperationResponse<T> result = _query.EndExecute(c) as QueryOperationResponse<T>;
            MainPage.ListRecords = new ObservableCollection<T>(result) as ObservableCollectionEx<MyEntity>;
            MainPage.waitHandle.Set();
        }), _query);
        // neither is this!
        var test = _asyncResult.AsyncWaitHandle.WaitOne(0);
    }
}

有什么建议吗? 我最困惑的是为什么_asycResult任务似乎从未发生过。 我正在使用带有EF4的Silverlight 4和devart Oracle dotConnect提供程序。

Silverlight:异步调用中的 WaitOne 同步未按预期运行

在 silverlight 和 ui 线程上,这是预期行为(挂起的应用(。 我似乎找不到更多的官方文档,尽管我可以发誓我记得过去看到过它......

我不记得确切的细节,但我相当确定直接阻止 ui 线程会导致一切停止运行。 我似乎记得 UI 线程处理网络 IO。

无论您想在 WaitOne 调用后发生什么,都需要移动到回调。

虽然这不是您的确切情况,但此链接中有相关信息。 它解释了阻止 UI 线程可防止 Web 服务调用发出,因为它还会阻止 UI 消息队列。 我意识到您的呼叫正在从线程池中发出。 我仍然认为阻止 ui 线程会干扰,但我找不到具体解释原因的文档。

http://www.codeproject.com/Articles/31018/Synchronous-Web-Service-Calls-with-Silverlight-Dis(目前我能找到的最好的,我会在/是否找到我正在寻找的文章时更新(。