在学习这个部分之前,搜索了很多资料,也看了很多源代码。但是再看看微软的MSDN发现,网上流传的与MSDN (.NET 2.0) 的说法完全不同。于是我很茫然,一直以为C# 2.0 怎么这么难啊。
但是今天,别人说C#原来这么简单的说法,又一次得到了我自己的同感。
很多人说,在.NET框架中,启动多线程,需要使用Thread空间,这的确不错。不过,对于我这样的初学者,我可以只使用BackgroundWoker控件来实现。
很多人说,要在多线程中传递数据,需要设置一个包装的类什么的,总之好像很麻烦的样子。因为我现在还是不清楚,所以也不知道该怎么写。不过,我这个初学者,也找到了自己的解决办法,那就是设置全局变量,直接传递数据,不过,在同步数据之前可以将全局变量lock一下,来保证安全。嘿嘿。
下面就把我个人的拙作,献丑一下,请多多包涵。如果您发现我有哪里做得不好,请多多评批指正。
在学习这个部分之前,搜索了很多资料,也看了很多源代码。但是再看看微软的MSDN发现,网上流传的与MSDN (.NET 2.0) 的说法完全不同。于是我很茫然,一直以为C# 2.0 怎么这么难啊。
但是今天,别人说C#原来这么简单的说法,又一次得到了我自己的同感。
很多人说,在.NET框架中,启动多线程,需要使用Thread空间,这的确不错。不过,对于我这样的初学者,我可以只使用BackgroundWoker控件来实现。
很多人说,要在多线程中传递数据,需要设置一个包装的类什么的,总之好像很麻烦的样子。因为我现在还是不清楚,所以也不知道该怎么写。不过,我这个初学者,也找到了自己的解决办法,那就是设置全局变量,直接传递数据,不过,在同步数据之前可以将全局变量lock一下,来保证安全。嘿嘿。
下面就把我个人的拙作,献丑一下,请多多包涵。如果您发现我有哪里做得不好,请多多评批指正。
1
namespace 多线程1
2

{
3
partial class Form1
4
{
5
/**//// <summary>
6
/// 必需的设计器变量。
7
/// </summary>
8
private System.ComponentModel.IContainer components = null;
9
10
/**//// <summary>
11
/// 清理所有正在使用的资源。
12
/// </summary>
13
/// <param name="disposing">如果应释放托管资源,为 true;否则为 false。</param>
14
protected override void Dispose(bool disposing)
15
{
16
if (disposing && (components != null))
17
{
18
components.Dispose();
19
}
20
base.Dispose(disposing);
21
}
22
23
Windows 窗体设计器生成的代码#region Windows 窗体设计器生成的代码
24
25
/**//// <summary>
26
/// 设计器支持所需的方法 - 不要
27
/// 使用代码编辑器修改此方法的内容。
28
/// </summary>
29
private void InitializeComponent()
30
{
31
this.components = new System.ComponentModel.Container();
32
this.label5 = new System.Windows.Forms.Label();
33
this.labelTimeReal = new System.Windows.Forms.Label();
34
this.label7 = new System.Windows.Forms.Label();
35
this.labelTimeCount = new System.Windows.Forms.Label();
36
this.textBoxURI = new System.Windows.Forms.TextBox();
37
this.buttonDownload = new System.Windows.Forms.Button();
38
this.textBox1 = new System.Windows.Forms.TextBox();
39
this.timer1 = new System.Windows.Forms.Timer(this.components);
40
this.backgroundWorker1 = new System.ComponentModel.BackgroundWorker();
41
this.SuspendLayout();
42
//
43
// label5
44
//
45
this.label5.AutoSize = true;
46
this.label5.Location = new System.Drawing.Point(114, 9);
47
this.label5.Name = "label5";
48
this.label5.Size = new System.Drawing.Size(53, 12);
49
this.label5.TabIndex = 4;
50
this.label5.Text = "准确时间";
51
//
52
// labelTimeReal
53
//
54
this.labelTimeReal.AutoSize = true;
55
this.labelTimeReal.Location = new System.Drawing.Point(176, 8);
56
this.labelTimeReal.Name = "labelTimeReal";
57
this.labelTimeReal.Size = new System.Drawing.Size(29, 12);
58
this.labelTimeReal.TabIndex = 5;
59
this.labelTimeReal.Text = "0000";
60
//
61
// label7
62
//
63
this.label7.AutoSize = true;
64
this.label7.Location = new System.Drawing.Point(5, 9);
65
this.label7.Name = "label7";
66
this.label7.Size = new System.Drawing.Size(53, 12);
67
this.label7.TabIndex = 6;
68
this.label7.Text = "系统计时";
69
//
70
// labelTimeCount
71
//
72
this.labelTimeCount.AutoSize = true;
73
this.labelTimeCount.Location = new System.Drawing.Point(67, 8);
74
this.labelTimeCount.Name = "labelTimeCount";
75
this.labelTimeCount.Size = new System.Drawing.Size(29, 12);
76
this.labelTimeCount.TabIndex = 7;
77
this.labelTimeCount.Text = "0000";
78
//
79
// textBoxURI
80
//
81
this.textBoxURI.Location = new System.Drawing.Point(7, 24);
82
this.textBoxURI.Name = "textBoxURI";
83
this.textBoxURI.Size = new System.Drawing.Size(141, 21);
84
this.textBoxURI.TabIndex = 8;
85
//
86
// buttonDownload
87
//
88
this.buttonDownload.Location = new System.Drawing.Point(154, 24);
89
this.buttonDownload.Name = "buttonDownload";
90
this.buttonDownload.Size = new System.Drawing.Size(62, 23);
91
this.buttonDownload.TabIndex = 9;
92
this.buttonDownload.Text = "Download";
93
this.buttonDownload.UseVisualStyleBackColor = true;
94
this.buttonDownload.Click += new System.EventHandler(this.buttonDownload_Click);
95
//
96
// textBox1
97
//
98
this.textBox1.Location = new System.Drawing.Point(5, 53);
99
this.textBox1.Multiline = true;
100
this.textBox1.Name = "textBox1";
101
this.textBox1.ScrollBars = System.Windows.Forms.ScrollBars.Vertical;
102
this.textBox1.Size = new System.Drawing.Size(209, 169);
103
this.textBox1.TabIndex = 10;
104
//
105
// timer1
106
//
107
this.timer1.Enabled = true;
108
this.timer1.Interval = 1000;
109
this.timer1.Tick += new System.EventHandler(this.timer1_Tick);
110
//
111
// backgroundWorker1
112
//
113
this.backgroundWorker1.DoWork += new System.ComponentModel.DoWorkEventHandler(this.backgroundWorker1_DoWork);
114
this.backgroundWorker1.RunWorkerCompleted += new System.ComponentModel.RunWorkerCompletedEventHandler(this.backgroundWorker1_RunWorkerCompleted);
115
//
116
// Form1
117
//
118
this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 12F);
119
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
120
this.ClientSize = new System.Drawing.Size(218, 223);
121
this.Controls.Add(this.textBox1);
122
this.Controls.Add(this.buttonDownload);
123
this.Controls.Add(this.textBoxURI);
124
this.Controls.Add(this.labelTimeCount);
125
this.Controls.Add(this.label7);
126
this.Controls.Add(this.labelTimeReal);
127
this.Controls.Add(this.label5);
128
this.Name = "Form1";
129
this.Text = "Form1";
130
this.ResumeLayout(false);
131
this.PerformLayout();
132
133
}
134
135
#endregion
136
137
private System.Windows.Forms.Label label5;
138
private System.Windows.Forms.Label labelTimeReal;
139
private System.Windows.Forms.Label label7;
140
private System.Windows.Forms.Label labelTimeCount;
141
private System.Windows.Forms.TextBox textBoxURI;
142
private System.Windows.Forms.Button buttonDownload;
143
private System.Windows.Forms.TextBox textBox1;
144
private System.Windows.Forms.Timer timer1;
145
private System.ComponentModel.BackgroundWorker backgroundWorker1;
146
}
147
}
148
149