c# - WPF 使用计时器重复一个 Action

标签 c# wpf xaml timer

所以我是 WPF 的新手,我只是想制作一个简单的小程序。当您按下开始按钮时,它会不断打印假代码,直到您按下停止按钮。我试图让它重复,直到停止按钮被击中 10 种不同的方式,但它们都没有工作。 TextBlock 元素将更新一次(或永远不会),然后整个程序变得不可用并且加载光标出现。我猜它不是经历一个循环,然后更新 TextBlocks,而是在后台执行所有操作,而不是进行视觉更新。

 public partial class MainWindow : Window
{
    System.Windows.Threading.DispatcherTimer dispatcherTimer = new System.Windows.Threading.DispatcherTimer();

    Random r1 = new Random();

    bool stop = false;
    int numUse

    public MainWindow()
    {
        InitializeComponent();


        dispatcherTimer.Interval = new TimeSpan(0, 0, 1);
        dispatcherTimer.Start();

    }

    //Executes when the start button is hit, begins timer
    private void Button_Click(object sender, RoutedEventArgs e)
    {
        do
        {
            dispatcherTimer.Tick += dispatcherTimer_Tick;
        } while (stop == false);
    }

    //Executes when the stop button is hit, ends timers do while loop
    private void Button_Click_1(object sender, RoutedEventArgs e)
    {
        stop = true;
    }

    private void dispatcherTimer_Tick(object sender, EventArgs e)
    {

            numUse = r1.Next(1, 2);

            if (numUse == 1)
            {
                CodeBlock1.Text = "struct group_info init_groups = { .usage = ATOMIC_INIT(2) }; ";
                CodeBlock2.Text = "";
                CodeBlock3.Text = "struct group_info *groups_alloc(int gidsetsize){ ";
                CodeBlock4.Text = "struct group_info *group_info; ";
                CodeBlock5.Text = "int nblocks; ";
                CodeBlock6.Text = "int i; ";
                CodeBlock7.Text = "";
                CodeBlock8.Text = "initialize stream";
            }
            else if (numUse == 2)
            {
                CodeBlock1.Text = "if (gidsetsize <= NGROUPS_SMALL) ";
                CodeBlock2.Text = "group_info->blocks[0] = group_info->small_block; ";
                CodeBlock3.Text = " else {  ";
                CodeBlock4.Text = " for (i = 0; i < nblocks; i++) { ";
                CodeBlock5.Text = "b = (void *)__get_free_page(GFP_USER);  ";
                CodeBlock6.Text = " goto out_undo_partial_alloc;  ";
                CodeBlock7.Text = "} ";
                CodeBlock8.Text = "";
            } else
            {

            }
    }



}

我试过 for 循环,do while,以不同的顺序使用不同的方法。我知道我在走那些路线时可能搞砸了,所以任何方法都适合我的目的。显然,在这种情况下我使用的是定时器。

最佳答案

不必在Button_Clickwhile循环中重复调用dispatcherTimer.Tick += dispatcherTimer_Tick。我怀疑当这个循环运行时,消息泵上的任何其他东西都不会运行,包括来自 DispatcherTimer 的滴答。

您可能可以取消 stop 并仅通过从代码中的任何位置调用 Stop() 直接对 DispatcherTimer 进行操作。

也许这样:

public MainWindow()
{
    InitializeComponent();


    dispatcherTimer.Interval = new TimeSpan(0, 0, 1);
    dispatcherTimer.Tick += dispatcherTimer_Tick; // set it up here

}

 //Executes when the start button is hit, begins timer
private void Button_Click(object sender, RoutedEventArgs e)
{
    dispatcherTimer.Start(); // start timer

}

//Executes when the stop button is hit, ends timers do while loop
private void Button_Click_1(object sender, RoutedEventArgs e)
{
    dispatcherTimer.Stop(); // stop timer
}

关于c# - WPF 使用计时器重复一个 Action ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32685538/

相关文章:

c# - WinRT - XAML 绑定(bind) DataContext

c# - 使用 Autofixture 控制对象树的生成深度

c# - WPF - 在文本框外部单击时移除焦点

c# - 如何平滑路径图的WPF线段

c# - 从 ListView ItemDataBound 获取数据值

c# - 设置变量 'as type'

c# - WPF TextBox UpdateSourceTrigger=LostFocus 不断更新数据

.net - WPF 进度条无法正确左对齐

c# - XAML元素只是为了将C#应用程序中常见DataContext的子元素分组?

c# - 制作自己的 Windows 8 应用程序主题