WPF 在进行时不更新文本框

发布于 2024-10-03 16:17:03 字数 419 浏览 3 评论 0原文

我有这样的代码:

void wait(int ms)
{
    System.Threading.Thread.Sleep(ms);
}

private void button1_Click(object sender, RoutedEventArgs e)
{
    info.Text = "step 1";
    wait(1000);
    info.Text = "step 2";
    wait(1000);
    info.Text = "step 3";
    wait(1000);
    info.Text = "step 4";
    wait(1000);
}

问题是 textbox.text 在整个 void button1_Click 完成后更新。它没有在播出中更新:(

请问,该怎么做?

I have this code:

void wait(int ms)
{
    System.Threading.Thread.Sleep(ms);
}

private void button1_Click(object sender, RoutedEventArgs e)
{
    info.Text = "step 1";
    wait(1000);
    info.Text = "step 2";
    wait(1000);
    info.Text = "step 3";
    wait(1000);
    info.Text = "step 4";
    wait(1000);
}

And problem is that textbox.text is updated after whole void button1_Click finished. It is not updated ON AIR :(

Please, How to do that?

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

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

发布评论

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

评论(4

梦魇绽荼蘼 2024-10-10 16:17:03

就这样做吧。

private void button1_Click(object sender, RoutedEventArgs e)
    {
        ThreadPool.QueueUserWorkItem((o) =>
             {
                 Dispatcher.Invoke((Action) (() => info.Text = "step 1"));
                 wait(1000);
                 Dispatcher.Invoke((Action) (() => info.Text = "step 2"));
                 wait(1000);
                 Dispatcher.Invoke((Action) (() => info.Text = "step 3"));
                 wait(1000);
                 Dispatcher.Invoke((Action) (() => info.Text = "step 4"));
                 wait(1000);
             });
    }

Just do this.

private void button1_Click(object sender, RoutedEventArgs e)
    {
        ThreadPool.QueueUserWorkItem((o) =>
             {
                 Dispatcher.Invoke((Action) (() => info.Text = "step 1"));
                 wait(1000);
                 Dispatcher.Invoke((Action) (() => info.Text = "step 2"));
                 wait(1000);
                 Dispatcher.Invoke((Action) (() => info.Text = "step 3"));
                 wait(1000);
                 Dispatcher.Invoke((Action) (() => info.Text = "step 4"));
                 wait(1000);
             });
    }
‘画卷フ 2024-10-10 16:17:03

button1_Click 方法返回之前,GUI 线程不会刷新。这就是为什么您只能看到最后一个值。您必须将长方法放入异步调用中或使用线程。

The GuI Thread won't refresh until the button1_Click method returns. That's why you only see the last value. You have to put long methods into asynchronous calls or use threads.

抚笙 2024-10-10 16:17:03

测量/排列(以及渲染)是异步发生的,因此如果您想强制屏幕更新,则需要调用 UpdateLayout。

The measure/arrange (and therefore the render) happens asynchronously so if you want to force the screen to update then you would need to call UpdateLayout.

空心空情空意 2024-10-10 16:17:03

如果您尝试使用 DispatcherFrame 来模拟 DoEvents 将完成的形式会怎样:

您可能需要包含 System.Security.Permissions 和 System.Windows.Threading。之后,在每次睡眠后调用 DoEvents() 即可获得所需的结果。

[SecurityPermissionAttribute(SecurityAction.Demand, Flags = SecurityPermissionFlag.UnmanagedCode)]
public void DoEvents()
{
    DispatcherFrame frame = new DispatcherFrame();
    Dispatcher.CurrentDispatcher.BeginInvoke(DispatcherPriority.Background,
        new DispatcherOperationCallback(ExitFrame), frame);
    Dispatcher.PushFrame(frame);
}

public object ExitFrame(object f)
{
    ((DispatcherFrame)f).Continue = false;

    return null;
}

链接到 MSDN 文章:http://msdn。 microsoft.com/en-us/library/system.windows.threading.dispatcher.pushframe.aspx

What if you tried a DispatcherFrame to simulate what forms DoEvents would accomplish:

You may need to include System.Security.Permissions and System.Windows.Threading. After that pplace a call to DoEvents() after each of your sleeps and you get the desired result.

[SecurityPermissionAttribute(SecurityAction.Demand, Flags = SecurityPermissionFlag.UnmanagedCode)]
public void DoEvents()
{
    DispatcherFrame frame = new DispatcherFrame();
    Dispatcher.CurrentDispatcher.BeginInvoke(DispatcherPriority.Background,
        new DispatcherOperationCallback(ExitFrame), frame);
    Dispatcher.PushFrame(frame);
}

public object ExitFrame(object f)
{
    ((DispatcherFrame)f).Continue = false;

    return null;
}

Link to MSDN article: http://msdn.microsoft.com/en-us/library/system.windows.threading.dispatcher.pushframe.aspx

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