简体   繁体   English

我的AutoResetEvent代码有什么问题?

[英]Whats wrong with my AutoResetEvent code?

I have this code which seems pretty straightforward but the AutoResetEvent never gets signalled. 我有这段代码,看起来很简单,但是AutoResetEvent从未发出信号。 Nothing seems to get returned from the web services and the WaitAll just times out after ten seconds. Web服务似乎什么也没有返回,而WaitAll在十秒钟后才超时。 Everything works fine without the threading jiggerypokery so its not a web service issue. 没有线程jiggerypokery,一切工作正常,因此它不是Web服务问题。 What am I doing wrong? 我究竟做错了什么?

    AutoResetEvent[] autoEvents;
    ObservableCollection<Tx3.ResourceService.ResourceTime> resourceTime;
    ObservableCollection<Tx3.ResourceService.ResourceTimeDetail> resourceTimeDetail;

    private void UserControl_Loaded(object sender, RoutedEventArgs e)
    {
        autoEvents = new AutoResetEvent[]
        {
            new AutoResetEvent(false),
            new AutoResetEvent(false),
        };

        var resourceService = getResourceServiceClient();

        // Get ResourceTime data for this user
        resourceService.ListResourceTimeAsync(CategoryWorkItemId, ResourceId);
        resourceService.ListResourceTimeCompleted += new EventHandler<Tx3.ResourceService.ListResourceTimeCompletedEventArgs>(resourceService_ListResourceTimeCompleted);

        // Get ResourceTimeDetails
        resourceService.ListResourceTimeDetailAsync(CategoryWorkItemId, ResourceId);
        resourceService.ListResourceTimeDetailCompleted += new EventHandler<ListResourceTimeDetailCompletedEventArgs>(resourceService_ListResourceTimeDetailCompleted);

        WaitHandle.WaitAll(autoEvents, 10000);

        System.Diagnostics.Debug.WriteLine("do something with both datasets");
    }

    void resourceService_ListResourceTimeCompleted(object sender, Tx3.ResourceService.ListResourceTimeCompletedEventArgs e)
    {
        resourceTime = e.Result;
        autoEvents[0].Set();
    }

    void resourceService_ListResourceTimeDetailCompleted(object sender, ListResourceTimeDetailCompletedEventArgs e)
    {
        resourceTimeDetail = e.Result;
        autoEvents[1].Set();
    }

I can offer a naive first guess: it looks like you're adding the event handlers after calling the methods that start the asynchronous operations; 我可以提供一个幼稚的第一个猜测:看起来您在调用启动异步操作的方法之后添加了事件处理程序; it's possible there's a race condition in there or some other issue. 那里可能存在竞争状况或其他问题。 Could you switch the order of operations so you attach the event handler, and then begin the operation? 您可以切换操作顺序,以便附加事件处理程序,然后开始操作吗?

These are AutoResetEvent objects -- looks like you want a ManualResetEvent -- the auto version triggers anything waiting, but immediately resets. 这些是AutoResetEvent对象-看起来像是您想要的ManualResetEvent-自动版本会触发所有等待的内容,但会立即重置。 Manual ones stay triggered so if the callback happens before you get to the WaitAll, it'll just fall through immediately. 手动操作会保持触发状态,因此如果回调在您进入WaitAll之前发生,它将立即掉线。

Also, qid is correct -- you're attaching your event handlers too late too...so there's two different bugs going on here. 同样,qid是正确的-您太晚附加事件处理程序了...所以这里发生了两个不同的错误。

Are you using this code on a thread that is marked with the STA attribute, for example the main UI thread? 您是否在标有STA属性的线程(例如主UI线程)上使用此代码? If so, the WaitAll method is not supported on these threads. 如果是这样,则这些线程不支持WaitAll方法。

Check here . 在这里检查。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM