4

我有这个代码:

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 完成后更新。它没有在 AIR 上更新 :(

请问,怎么做?

4

4 回答 4

9

就这样做吧。

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);
             });
    }
于 2010-11-18T14:46:07.377 回答
3

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

于 2010-11-17T14:45:01.000 回答
0

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

于 2010-11-17T14:45:30.603 回答
-1

如果您尝试使用 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

于 2010-11-17T16:51:32.837 回答