获得"访问被拒绝。 (从HRESULT异常:0X80070005(E_ACCESSDENIED))"在Windows 8应用程序时DispatchTimer使用消息对话框?对话框、

2023-09-03 04:28:23 作者:一人走天下

我想使用在消息对话框中发送定时改变用户到时完成。但有时它提供了以下错误:访问被拒绝(从HRESULT异常:0X80070005(E_ACCESSDENIED))。如何解决此问题?

code:

 公共DetailPage()
        {
      定时器=新DispatcherTimer();
            timer.Tick + = dispatcherTimer_Tick;
            timer.Interval =新时间跨度(0,0,1);
            this.txtTimer.Text = GlobalVariables.totalTime.Minutes +:+ GlobalVariables.totalTime.Seconds +分钟;
            timer.Start();
}



  异步无效dispatcherTimer_Tick(对象发件人,对象E)
    {
        如果(GlobalVariables.totalTime.Minutes大于0 || GlobalVariables.totalTime.Seconds大于0)
        {
            GlobalVariables.totalTime = GlobalVariables.totalTime.Subtract(新时间跨度(0,0,1));
            this.txtTimer.Text = GlobalVariables.totalTime.Minutes +:+ GlobalVariables.totalTime.Seconds +分钟;
        }
        其他
        {
            timer.Tick  -  = dispatcherTimer_Tick;
            timer.Stop();

            MessageDialog signInDialog =新MessageDialog(时间到,会话过期);

            //添加的命令,并设置其回调
            signInDialog.Commands.Add(新UICommand(OK,(命令)=>
            {
                this.Frame.Navigate(typeof运算(主页),AllGroups);
            }));

            //设置将被默认调用的命令
            signInDialog.DefaultCommandIndex = 1;

            //显示消息对话框
            等待signInDialog.ShowAsync();
        }
    }
 

我收到错误的:

  //显示消息对话框
        等待signInDialog.ShowAsync();
 

解决方案

像杰夫说,计时器Tick事件处理程序code是不同的线程比UI线程中运行。你必须回到这个UI线程的用户界面在操作什么:消息对话框中,更改属性等

  //一些code在您的网页定时器
定时器=新DispatcherTimer {间隔=新的时间跨度(0,0,1)};
timer.Tick + = TimerOnTick;
timer.Start();

//事件处理计时器滴答
私人无效TimerOnTick(对象发件人,对象o)
{
    timer.Stop();
    变种MD =新MessageDialog(测试);

    this.Dispatcher.RunAsync(CoreDispatcherPriority.Normal,()=> md.ShowAsync());
}
 
XMLHttpRequest 网络错误 0x80070005, 拒绝访问 解决办法

请注意,我做停在事件处理程序的计时器。如果不及时关闭消息对话框另一个显示之前,你会得到否认二号ShowAsync过访问(因为第一个仍处于打开状态)。

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 HRESULT: 0x80070005 (E_ACCESSDENIED))". How to resolve this?

Code:

 public DetailPage()
        {
      timer = new DispatcherTimer();
            timer.Tick += dispatcherTimer_Tick; 
            timer.Interval = new TimeSpan(0, 0, 1);
            this.txtTimer.Text = GlobalVariables.totalTime.Minutes + ":" + GlobalVariables.totalTime.Seconds + "mins";
            timer.Start();
}



  async void dispatcherTimer_Tick(object sender, object e)
    {
        if (GlobalVariables.totalTime.Minutes > 0 || GlobalVariables.totalTime.Seconds > 0)
        {
            GlobalVariables.totalTime = GlobalVariables.totalTime.Subtract(new TimeSpan(0, 0, 1));
            this.txtTimer.Text = GlobalVariables.totalTime.Minutes + ":" + GlobalVariables.totalTime.Seconds + " mins";
        }
        else
        {
            timer.Tick -= dispatcherTimer_Tick;
            timer.Stop();

            MessageDialog signInDialog = new MessageDialog("Time UP.", "Session Expired");

            // Add commands and set their callbacks
            signInDialog.Commands.Add(new UICommand("OK", (command) =>
            {
                this.Frame.Navigate(typeof(HomePage), "AllGroups");
            }));

            // Set the command that will be invoked by default
            signInDialog.DefaultCommandIndex = 1;

            // Show the message dialog
            await signInDialog.ShowAsync();
        }
    }

I am getting the error at:

 // Show the message dialog
        await signInDialog.ShowAsync();

解决方案

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).