为了停止执行UI线程,messageBox.Show()会做什么? [英] What does messageBox.Show() do in order to stop the execution of a UI thread?

查看:155
本文介绍了为了停止执行UI线程,messageBox.Show()会做什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在WPF应用程序中,我正在将消息输出到文本框,并且在这些消息之间,我正在调用设置仪器的函数.但是,所有消息都同时出现(在函数调用结束时).

In a WPF application, I am outputting messages to a text box and in between these messages I am calling a function that sets up an instrument. However the messages all appear at the same time (at end of function call).

我真的不知道如何清楚地解释我的问题.我会尽力... 我有一个WPF应用程序,该应用程序使用串行端口从仪器中获取数据.该应用程序包含一些按钮和一个用于输出消息的文本框.按下按钮后,应用程序会向仪器发送命令以更改某些参数(通过功能Set_COMM_MODE()),然后返回.更改大约需要5-10秒.因此,我要做的是:在呼叫之前输出请稍候"消息,在呼叫返回之后输出完成"消息. 代码中的OuputText()函数仅调用TextBox.AppendText()方法. 我的问题:呼叫返回时,所有输出文本都会在文本框中显示出来.我希望收到请稍候... ,然后5-10秒钟后收到完成" 消息.但这一切都是同时出现的.但是,当我在第一个消息之后(在函数调用之前)放置MessageBox时,该消息将出现在文本框输出中(不包含正在调用的函数).但是问题是我必须在MessageBox上按OK才能继续.

I do not really know how to explain my problem clearly. I'll try... I have a WPF application which takes data from an instrument using the serial port. The application contains some buttons and a text box for output messages. Upon pressing a button, the application sends a command to the instrument to change some parameters (through a function Set_COMM_MODE()), and returns. The change takes around 5-10 seconds. Thus what I did is: I outputted a "Please Wait" message before the call and a "Done" message after the call return. The OuputText() function in the code only calls the TextBox.AppendText() method. My Problem: All the output text is splurted out on the text box upon the call return. I expected a Please Wait... then 5-10s later a "Done" message. But it is all appearing at the same time. However, when I put a MessageBox after the 1st message (before the function call), the message appears on the textbox output (w/o the function being called). However the problem is that I have to press OK on the MessageBox in order to continue.

Q(1):如何避免后者发生而不必求助于MessageBox
问(2):MessageBox为创建此行为需要做什么?

Q(1): How can I have the latter happening w/o having to resort to a MessageBox
Q(2): What does a MessageBox do in order to create this behaviour?

我尝试过:使用Dispatch.Invoke()方法在另一个线程上运行OutputText. 这(暂停类似MessageBox.Show()的窗口)是一个类似的问题可以解决我的问题,但似乎没有得到明确的答案,而且我对解决方案也不太了解.

I tried: using the Dispatch.Invoke() method to run the OutputText on another thread. This (Pause a window like MessageBox.Show()) is a similar problem to what I have, but didn't seem to get a definitive answer, and I did not understand the solution well.

void StartTest_btn_Click(object sender, RoutedEventArgs e)
{
    OutputText("Please Wwait\r\n"); //<---- This should appear at once.

    MessageBox.Show("Please Wwait"); //<--without this, both messages appear at same time after 10s.

    Set_COMM_MODE("02"); //<--- This takes around 5-10s

    OutputText("Done\r\n"); //<--- This should appear 5-10s later
}

我希望一次显示请稍候" ,然后在函数Set_COMM_MODE()返回后5-10秒钟后显示完成" 消息.

I expect a "Please wait" to show at once, then 5-10s later the "Done" message to show, after return of function Set_COMM_MODE().

推荐答案

正如我在评论中所写,您应该真正将(主)UI线程仅用于UI. 在工作线程上执行其他所有长时间运行的非UI操作.

As I wrote in my comment, you should really use the (main) UI thread for UI only. Perform any other long-running non-UI operations on worker threads.

您可能不会介意10秒钟的挂起" UI,但是肯定会让用户感到烦恼.另外,阻止UI线程将使Windows认为您的应用程序已冻结,因此您将获得漂亮的不响应"标志以及所有相关的东西.这不仅看起来很糟糕,而且还会引起各种副作用.

You might not mind the "hang" UI for 10 seconds, but users will surely be annoyed. Also, blocking the UI thread will cause Windows to think that your app is frozen, so you'll get that nice "not responding" badge and all related stuff. This not only looks awful but also can cause various side-effects.

您应该真正环顾四周,看看.NET为您提供了哪些解决此类问题的方法.

You should really take a look around and see what .NET offers you for such kind of problems.

看,这是您的工作流程:

Look, this is your workflow:

  1. 打印一条消息
  2. 开始初始化
  3. ???
  4. 初始化完成->打印完成"
  5. 开始操作

这是什么?这是异步处理.您可以开始操作并异步继续-这意味着仅在操作完成时.

What is this? This is an asynchronous processing. You start an action and continue asynchronously - that means, only when the action is completed.

.NET为此提供了许多工具. APM(异步编程模型).但是,实现异步处理的最简洁,最强大的方法是TAP-基于任务的异步编程模式,通常称为async/await.

.NET offers you a lot of tools for that, e.g. the APM (Asynchronous Programming Model). But the most neat and powerful way for implementing asynchronous processing is the TAP - Task-based Asynchronous Programming pattern, better known as async/await.

看,使用TAP只需几行就可以解决您的问题:

Look, your problem can be solved with a couple of lines using the TAP:

async void StartTest_btn_Click(object sender, RoutedEventArgs e)
{
    OutputText("Please Wait\r\n");      

    // Set_COMM_MODE will be executed on a worker thread!
    // The main (UI) thread won't block. It will continue.
    // But the StartTest_btn_Click method itself will pause until the task is finished.
    await Task.Run(() => Set_COMM_MODE("02"));

    // This line will only be executed, after the Set_COMM_MODE call is completed;
    // furthermore, it will be executed on the UI thread again!
    OutputText("Done\r\n");
}

您应该真正了解有关TAP之一的现代编程技术的更多信息.

You should really learn more about the modern programming techniques, which the TAP is one of.

这篇关于为了停止执行UI线程,messageBox.Show()会做什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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