Getting “Access is denied. (Exception from HRESULT: 0x80070005 (E_ACCESSDENIED))” in Windows 8 App when using Message dialog in DispatchTimer?

后端 未结 2 2030
独厮守ぢ
独厮守ぢ 2021-01-15 20:15

I am trying to use message dialog in dispatch timer to alter user when the time is complete. but at times it gives following error: \"Access is denied. (Exception from HRESU

相关标签:
2条回答
  • 2021-01-15 20:59

    The dispatcherTimer_Tick method is running on a different thread than the UI. If you want to access stuff that is bound to the UI thread, like UX, you must get back onto the UI thread. Easiest way to do this is wrap your code with

    Dispatcher.RunAsync()
    
    0 讨论(0)
  • 2021-01-15 21:05

    Like Jeff says, the timer Tick event handler code is running on a different thread than the UI thread. You'll have to get back to this UI thread to manipulate anything in the UI: message dialog, changing properties, etc.

    // some code for the timer in your page
    timer = new DispatcherTimer {Interval = new TimeSpan(0, 0, 1)};
    timer.Tick += TimerOnTick;
    timer.Start();
    
    // event handler for the timer tick
    private void TimerOnTick(object sender, object o)
    {
        timer.Stop();
        var md = new MessageDialog("Test");
    
        this.Dispatcher.RunAsync(CoreDispatcherPriority.Normal, () => md.ShowAsync());
    }
    

    Note that I do stop the timer in the event handler. If you don't close a message dialog in time before another one is shown, you'll get an access denied on the 2nd ShowAsync too (because the first is still open).

    0 讨论(0)
提交回复
热议问题