我必须缺少明显的东西,但是我根本不明白为什么Windows Forms Application中的主表单需要20秒才能显示出来。我的程序尝试采用在控制台应用程序示例中找到的TPL代码示例,并以Windows窗体/ GUI进行工作。我知道任务排队需要20秒才能完成,但是那段代码发生得太快以至于表单没有时间完成初始化吗?我尝试在“ InitializeComponent();”之后放置一个延迟。希望它能给表格带来时间;并且还尝试在其后仅放置一个消息框,但在我插入的延迟后仍会得到相同的延迟。没有form_load方法,因此无需检查。
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
// Time the test(s)
Stopwatch stoppwatch = new Stopwatch();
stoppwatch.Start();
Console.SetOut(new ControlWriter(tb1)); // just redirects console.writeline to the form's textbox "tb1"
// #################################### job queue ########################################
var q = new TPLDataflowMultipleHandlers(); //
var numbers = Enumerable.Range(1,10);
foreach (var num in numbers)
{
F1TimeDelay(2);
q.Enqueue(num.ToString());
}
// Stop the timer and return the elapsed number of milliseconds.
stoppwatch.Stop();
Console.WriteLine("XXXXXXXXXX <STATUS> Elapsed time = {0} minutes. XXXXXXXXXX",(int)stoppwatch.Elapsed.TotalMinutes);
}
顺便说一句,当表单最终显示出来时,文本框充满了我希望从启动的线程中看到的消息(线程ID等信息)。
F1TimeDelay(2)是2秒的延迟,而排队的作业仅是10秒长的任务,以演示并行运行的10个任务。发送到队列的编号最终是10个任务的作业编号。