Silverlight OpenFileDialog DoEvents 等效项

发布于 2024-08-10 18:11:08 字数 844 浏览 4 评论 0原文

我正在处理用户选择的大文件。我的代码如下所示:

if (FileDialog.ShowDialog() == true) { 
    // process really big file
}

这会冻结 UI,因此我尝试在用户选择文件之前先显示一条加载消息,以便向他们提供正在发生某事的视觉提示:

loadingMessage.Visibility = Visibility.Visible;
if (FileDialog.ShowDialog() == true) { 
    // process really big file
}

不幸的是,这仍然完全冻结了 UI,而文件正在处理中。

我发现如果我在选择文件后立即触发 MessageBox,效果会非常好。我认为它在后台执行“DoEvents”类型调用以在运行时获取刷新事件/ui 项目。

loadingMessage.Visibility = Visibility.Visible;
if (FileDialog.ShowDialog() == true) {
    MessageBox.Show("Sync!");
    // process really big file
}

在这种情况下,大文件的处理速度仍然很慢,但会显示加载消息,并且屏幕 UI 会同步(我正在实际执行一些其他操作,例如显示等待光标)。

问题:

Silverlight 没有 DoEvents 功能。除了 MessageBox.Show 之外,我还可以进行调用来实现同步 UI 并防止 OpenFileDialog 冻结 UI 的相同效果吗?

I'm processing large files after they are selected by the user. My code looks like the following:

if (FileDialog.ShowDialog() == true) { 
    // process really big file
}

This freezes up the UI so I tried to display a loading message first before a user selected the file to give them a visual cue that something was happening:

loadingMessage.Visibility = Visibility.Visible;
if (FileDialog.ShowDialog() == true) { 
    // process really big file
}

Unfortunately, this still completely freezes up the UI while the file is being processed.

What I have found that works perfectly is if I fire a MessageBox right after the file selection. I think it does a "DoEvents" type call under the hood to get flush event/ui items in the runtime.

loadingMessage.Visibility = Visibility.Visible;
if (FileDialog.ShowDialog() == true) {
    MessageBox.Show("Sync!");
    // process really big file
}

In cases like this the big file is still processed as slowly but the loading message is displayed and the screen UI gets synched up (I'm doing some other things in the real thing such as showing a wait cursor).

Question:

Silverlight has no DoEvents functionality. Is there a call I can make besides MessageBox.Show to have the same effect of synchronizing the UI and preventing the OpenFileDialog from freezing up the UI?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

九局 2024-08-17 18:11:08

使用BackgroundWorker 类在后台线程上处理非常大的文件?
这里有一个链接:如何:使用BackgroundWorker< /a>

Use the BackgroundWorker class to process your really big file on a background thread?
Here a link for you: How to: Use a BackgroundWorker

又怨 2024-08-17 18:11:08

使用BackgroundWorker 在后台执行繁重的计算。当你想通知UI操作完成时,可以使用上述类的RunWorkerCompleted事件。另一种方法是使用完全独立的线程,然后使用 Dispatcher.BeginInvoke() 回调到 UI 线程。请记住,使用单独线程中的 .NET,如果没有这种同步,您将无法访问任何 GUI 组件。

Use a BackgroundWorker to execute the heavy computation in the background. When you want to notify the UI that the operation is complete, the RunWorkerCompleted event of the aforementioned class can be used. The alternative to this is to use a completely separate thread and then callback to the UI thread using Dispatcher.BeginInvoke(). Remember that with .NET in a separate thread you cannot access any GUI components without this kind of synchronization.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文