Getting quot;Access is denied. (Exception from HRESULT: 0x80070005 (E_ACCESSDENIED))quot; in Windows 8 App when using Message dialog in DispatchTimer?(获取“访问被拒绝.(来自 HRESULT 的异常:0x80070005 (E_ACCESSDENIED))在 DispatchTimer 中使用消息对话框时在 Windows 8 应用程序中?) - IT屋-
问题描述
我正在尝试使用调度计时器中的消息对话框在时间完成时更改用户.但有时它会给出以下错误:访问被拒绝.(来自 HRESULT 的异常:0x80070005(E_ACCESSDENIED))".如何解决?
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?
代码:
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();
推荐答案
就像 Jeff 所说,计时器 Tick 事件处理程序代码运行在与 UI 线程不同的线程上.您必须返回此 UI 线程来操作 UI 中的任何内容:消息对话框、更改属性等.
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());
}
请注意,我确实在事件处理程序中停止了计时器.如果您没有在显示另一个消息对话框之前及时关闭消息对话框,您也会在第二个 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).
这篇关于获取“访问被拒绝.(来自 HRESULT 的异常:0x80070005 (E_ACCESSDENIED))"在 DispatchTimer 中使用消息对话框时在 Windows 8 应用程序中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:获取“访问被拒绝.(来自 HRESULT 的异常:0x80070005 (E_ACCESSDENIED))"在 DispatchTimer 中使用消息对话框时在 Windows 8 应用程序中?


基础教程推荐
- JSON.NET 中基于属性的类型解析 2022-01-01
- 全局 ASAX - 获取服务器名称 2022-01-01
- 如何动态获取文本框中datagridview列的总和 2022-01-01
- 从 VS 2017 .NET Core 项目的发布目录中排除文件 2022-01-01
- 经典 Asp 中的 ResolveUrl/Url.Content 等效项 2022-01-01
- 将事件 TextChanged 分配给表单中的所有文本框 2022-01-01
- 首先创建代码,多对多,关联表中的附加字段 2022-01-01
- 是否可以在 asp classic 和 asp.net 之间共享会话状态 2022-01-01
- 在 VS2010 中的 Post Build 事件中将 bin 文件复制到物 2022-01-01
- 错误“此流不支持搜索操作"在 C# 中 2022-01-01