WPF自定义任务对话框显示多个 [英] Wpf custom task dialog showing multiple

查看:161
本文介绍了WPF自定义任务对话框显示多个的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在WPF中创建了自己的自定义任务对话框"窗口.我希望它遵循我的应用程序的样式,并且我要放置一些自定义内容,因此我更喜欢不使用win32任务对话框.

我的应用程序使用异步任务(异步/等待)从数据库加载数据.如果在加载数据时出现错误/异常,则ui线程将显示一个(自定义)TaskDialog,其中包含错误信息.

win32任务对话框将显示其自身的一个实例,而其他待显示的TaskDialogs仅在第一个返回后才显示(或者实际上似乎阻止了等待的任务继续).问题是我的自定义任务对话框在以下位置显示了多个错误: (等待的任务)同时显示在屏幕上.

我理解问题的方式(也许我在这里错了)是,尽管使用了ShowDialog(),但我的任务对话框仍在UI线程上运行并泵送消息.进一步的同步主窗口代码没有执行,但是如果一个任务完成等待,它将返回到UI线程执行,并假设存在数据加载错误,将显示另一个(自定义)任务对话框:

1)主窗口开始在多个等待的任务中加载数据
2)等待的任务返回错误/异常
3)使用ShowDialog()显示自定义任务对话框窗口 4)在自定义任务对话框仍显示的同时,另一个等待的任务也返回了一个错误
5)继续代码在当前显示任务对话框的UI线程上执行
6)第一个自定义任务对话框显示在第一个
上 等

我真的无法弄清楚Win32任务对话框如何在保持UI线程活动的同时阻止对自身的多次调用.我尝试了许多不同的解决方案,但似乎没有任何正常工作.

我意识到我可以使我的任务对话框异步,在其上使用信号灯和WaitAsync,但是由于我的任务对话框需要返回TaskDialogResult,因此我需要等待每次对任务对话框的调用,这将导致太多代码更改.

我知道我一定在这里想念什么,有人对此有任何想法吗?

我尝试过的事情:

使我的任务对话框使用信号灯和WaitAsync进行异步调用是可行的,但是我的任务对话框需要返回TaskDialog结果,因此我不得不等待每次调用,但不幸的是,代码更改太多.

我尝试在单独的线程中运行任务对话框,并阻塞主UI线程,直到任务对话框返回.那确实给了我想要的效果,但是阻塞主UI线程会在其他地方引起问题.这似乎也不是正确的方法.

I''ve made my own custom Task Dialog window in WPF. I wanted it to follow the style of my application, and I have some custom content to place on it, so I prefer not using the win32 task dialog.

My application uses asynchronous tasks (async/await) to load data from the database. If there''s an error/exception while loading data, the ui thread will show a (custom) TaskDialog with error information.

The win32 task dialog will show one instance of itself, and other pending TaskDialogs to show will only show up after the first has returned (or actually it seems to block awaited task continuations) The problem is that my custom task dialog is showing multiple errors at the same time (from awaited tasks), showing up multiple times on the screen.

The way I understand the issue (and perhaps I''m wrong here) is that despite using ShowDialog(), my task dialog is running on the UI thread and pumping messages. Further synchronous main window code is not executing, but if a task finishes awaiting, it returns to the UI thread to execute, and assuming there was a data load error, another (custom) task dialog is shown:

1) Main window starts data loading in multiple awaited tasks
2) awaited task returns with error/exception
3) Custom task dialog window is shown with ShowDialog()
4) While custom task dialog still showing, another awaited task returns with an error as well
5) Continuation code executes on UI thread which is current showing task dialog
6) A second custom task dialog is shown over the first
etc

I really can''t figure out how the Win32 taskdialog is blocking multiple calls to itself while keeping the UI thread alive. I''ve tried many different solutions, and nothing seems to work properly.

I realize I could make my task dialog async, use a semaphore and WaitAsync on it, but since my task dialog needs to return a TaskDialogResult, I''d need to await every call to my task dialog which would be way too many code changes.

I know I must be missing something here, anyone have any ideas on this?

What I have tried:

Making my task dialog call async, using a semaphore and WaitAsync on it works, but my task dialog needs to return a TaskDialog result, and so I''d have to await every call to it, unfortunately that''s too many code changes.

I''ve tried running my task dialog in a separate thread and blocking the main UI thread until the task dialog returns. That does give me the desired effect, but blocking the main UI thread is causing issues elsewhere. This also doesn''t seem like the proper way to do it.

推荐答案

使用动作队列来缓冲对话框.现在,您将出队&依次执行动作(显示对话)...
Use a Queue of Actions to buffer the Dialogs. Now you Dequeue & execute the actions (displaying of dialogues) in sequence, one at a time...


这篇关于WPF自定义任务对话框显示多个的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆