如果长时间重复运行,如何使过程不像崩溃那样重复? [英] how to make process for,while,repeat not like crash if while run repeat very long?

查看:67
本文介绍了如果长时间重复运行,如何使过程不像崩溃那样重复?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在Windows Phone上使用VS2010 C#,如果此方法运行迭代时间很长,我如何使进程迭代(方法重复或重复)不像崩溃一样,并且显示进度完成.

Using VS2010 c# for windows phone, how do I make process iteration (method for or while or repeat) not like crash and showing progress finish if this method run iteration very long.

private void button4_Click(object sender, RoutedEventArgs e)
        {
            progressBar1.Maximum = 10000;
            for (int i = 1; i <= 10000; i++)
            {
                textBox1.Text = Convert.ToString(i);
                progressBar1.Value = i;
            }
        }




在进程运行时,我希望进度条逐步显示进度.
谢谢




While the process is running, I want progress bar show the progress step by step.
thanks

推荐答案

代码没有意义,但可能您需要处理一些真实数据并稍后在UI中通知进度.

您需要一个额外的线程,别无其他(不要重复常见的谬误,不要使用计时器)!

创建一个单独的线程.这是一个非常健壮的方式的代码示例:
如何将ref参数传递给线程 [ ^ ].

您无法从非UI线程调用与UI相关的任何操作.相反,您需要使用System.Windows.Threading.DispatcherInvokeBeginInvoke方法(对于Forms或WPF)或System.Windows.Forms.Control(仅对于Forms).

在我过去的答案中,您将找到有关其工作原理的详细说明和代码示例:
Control.Invoke()与Control.BeginInvoke() [ ^ ],
Treeview Scanner和MD5的问题 [如何获取keydown事件在vb.net中的不同线程上操作 [启用禁用+多线程后控件事件不会触发 [ ^ ].

—SA
The code makes no sense, but probably you need to process some real data and notify the progress in UI later on.

You need an extra thread, nothing else (don''t repeat common fallacy, don''t use timers)!

Create a separate thread. This is a code sample for a very robust way:
How to pass ref parameter to the thread[^].

You cannot call anything related to UI from non-UI thread. Instead, you need to use the method Invoke or BeginInvoke of System.Windows.Threading.Dispatcher (for both Forms or WPF) or System.Windows.Forms.Control (Forms only).

You will find detailed explanation of how it works and code samples in my past answers:
Control.Invoke() vs. Control.BeginInvoke()[^],
Problem with Treeview Scanner And MD5[^].

See also more references on threading:
How to get a keydown event to operate on a different thread in vb.net[^],
Control events not firing after enable disable + multithreading[^].

—SA


您要在这里测量什么?您的循环将不需要很长时间就能达到10000,因此您的用户不太可能看到临时显示.您需要将您的代码与您要监视的任何后台活动进行同步.
What are you trying to measure here? Your loop will not take long to reach 10000 and so it''s unlikely that your user will see the interim displays. You need to synchronise your code with whatever background activity you are trying to monitor.


对于初学者,您永远不要设置progressBar1.Value = i且首先确保i不大于您最大值.
即prograssBar1.Value = Math.Min(progressBar1.Maximum,i);或类似性质的东西.然后,您还需要考虑Richard的回答.
For starters, you should never set progressBar1.Value = i with out first ensuring that i is not greater than your maximum value.
i.e. prograssBar1.Value = Math.Min( progressBar1.Maximum, i ); or something of that nature. Then you also need to take into account Richard''s answer as well.


这篇关于如果长时间重复运行,如何使过程不像崩溃那样重复?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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