using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Threading;
namespace demo_1018
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
Thread thread = new Thread(new ThreadStart(run));
thread.Start();
}
public void run()
{
int sum = 0;
for (int i = 0; i < 1000; i++)
{
sum = sum + i;
}
Console.WriteLine(sum);
}
private void button2_Click(object sender, EventArgs e)
{
Thread thread = new Thread(new ParameterizedThreadStart(run1));
thread.Start("你好");
}
public void run1(object A)
{
string str = A as string;
Console.WriteLine(str);
}
public delegate int Sum(int a, int b);
private void button3_Click(object sender, EventArgs e)
{
Sum sum = new Sum(test);
int num = sum.Invoke(3, 5);
Console.WriteLine(num);
}
public int test(int a, int b)
{
return a + b;
}
private void button4_Click(object sender, EventArgs e)
{
Thread thread = new Thread(test2);
thread.Start();
}
public void test2()
{
textBox1.Invoke(new Action(() => { textBox1.Text = "123"; }));
}
public delegate void Operation();
Operation operation;
private void button5_Click(object sender, EventArgs e)
{
operation = test3;
Thread thread = new Thread(run2);
thread.Start();
}
public void run2()
{
this.Invoke(operation);
}
public void test3()
{
textBox1.Text = "123";
}
private void button6_Click(object sender, EventArgs e)
{
Thread thread = new Thread(() => { Console.WriteLine("HELLO WORLD"); });
thread.Start();
}
private void button7_Click(object sender, EventArgs e)
{
Thread thread = new Thread(delegate () { Console.WriteLine("HELLO WORLD"); });
thread.Start();
}
private void button8_Click(object sender, EventArgs e)
{
Thread thread = new Thread(() =>
{
Thread.Sleep(6000);
Console.WriteLine("start a new thread");
});
thread.IsBackground = true;
thread.Start();
Console.WriteLine("main thread end");
}
private void button9_Click(object sender, EventArgs e)
{
BookShop bookShop = new BookShop();
Thread thread = new Thread(bookShop.Sale);
Thread thread1 = new Thread(bookShop.Sale);
thread.Start();
thread1.Start();
}
private delegate void setTextValueCallBack(int value);
private setTextValueCallBack setCallBack;
private void button10_Click(object sender, EventArgs e)
{
setCallBack = SetValue;
Thread thread = new Thread(run4);
thread.IsBackground = true;
thread.Start();
}
public void run4()
{
for (int i = 0; i < 10000; i++)
{
this.Invoke(setCallBack, i);
}
}
public void SetValue(int value)
{
textBox1.Text = value.ToString();
}
private void button11_Click(object sender, EventArgs e)
{
Console.WriteLine($"***************btnAsync_Click Start {Thread.CurrentThread.ManagedThreadId}");
Action<string> action = this.DoSomethingLong;
for (int i = 0; i < 5; i++)
{
string name = string.Format($"btnAsync_Click_{i}");
action.BeginInvoke(name, null, null);
}
Console.WriteLine($"***************btnAsync_Click End {Thread.CurrentThread.ManagedThreadId}");
}
private void button12_Click(object sender, EventArgs e)
{
Console.WriteLine($"****************btnSync_Click Start {Thread.CurrentThread.ManagedThreadId.ToString("00")} {DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.fff")}***************");
for (int i = 0; i < 5; i++)
{
string name = string.Format($"btnSync_Click_{i}");
this.DoSomethingLong(name);
}
}
private void DoSomethingLong(string name)
{
Console.WriteLine($"****************DoSomethingLong {name} Start {Thread.CurrentThread.ManagedThreadId.ToString("00")} {DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.fff")}***************");
long lResult = 0;
for (int i = 0; i < 1000000000; i++)
{
lResult += i;
}
Console.WriteLine($"****************DoSomethingLong {name} End {Thread.CurrentThread.ManagedThreadId.ToString("00")} {DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.fff")} {lResult}***************");
}
private void button13_Click(object sender, EventArgs e)
{
Console.WriteLine($"****************btnAsyncAdvanced_Click Start {Thread.CurrentThread.ManagedThreadId.ToString("00")} {DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.fff")}***************");
Action<string> action = this.DoSomethingLong;
action.BeginInvoke("btnAsyncAdvanced_Click", null, null);
Console.WriteLine($"到这里计算已经完成了。{Thread.CurrentThread.ManagedThreadId.ToString("00")}。");
Console.WriteLine($"****************btnAsyncAdvanced_Click End {Thread.CurrentThread.ManagedThreadId.ToString("00")} {DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.fff")}***************");
}
private void button14_Click(object sender, EventArgs e)
{
Console.WriteLine($"****************btnAsyncAdvanced_Click Start {Thread.CurrentThread.ManagedThreadId.ToString("00")} {DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.fff")}***************");
Action<string> action = this.DoSomethingLong;
AsyncCallback asyncCallback = p => { Console.WriteLine($"到这里计算已经完成了。{Thread.CurrentThread.ManagedThreadId.ToString("00")}。"); };
action.BeginInvoke("btnAsyncAdvanced_Click", asyncCallback, null);
Console.WriteLine($"****************btnAsyncAdvanced_Click End {Thread.CurrentThread.ManagedThreadId.ToString("00")} {DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.fff")}***************");
}
private void button15_Click(object sender, EventArgs e)
{
Console.WriteLine($"****************btnAsyncAdvanced_Click Start {Thread.CurrentThread.ManagedThreadId.ToString("00")} {DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.fff")}***************");
Action<string> action = this.DoSomethingLong;
IAsyncResult asyncResult = null;
AsyncCallback callback = p =>
{
Console.WriteLine(object.ReferenceEquals(p, asyncResult));
Console.WriteLine($"到这里计算已经完成了。{Thread.CurrentThread.ManagedThreadId.ToString("00")}。");
};
asyncResult = action.BeginInvoke("btnAsyncAdvanced_Click", callback, null);
Console.WriteLine($"****************btnAsyncAdvanced_Click End {Thread.CurrentThread.ManagedThreadId.ToString("00")} {DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.fff")}***************");
}
private void button16_Click(object sender, EventArgs e)
{
Func<int> action = () =>
{
Thread.Sleep(2000);
return DateTime.Now.Day;
};
int num = action.Invoke();
Console.WriteLine(num);
IAsyncResult asyncResult = null;
AsyncCallback asyncCallback = p => { Console.WriteLine(p.AsyncState); };
asyncResult = action.BeginInvoke(asyncCallback, "异步调用返回值");
int num1 = action.EndInvoke(asyncResult);
Console.WriteLine(num1);
}
}
public class BookShop
{
public int num = 1;
public void Sale()
{
lock (this)
{
int tmp = num;
if (tmp > 0)
{
Thread.Sleep(1000);
num = num - 1;
Console.WriteLine("售出一本图书,还剩{0}本", num);
}
else
{
Console.WriteLine("没有了");
}
}
}
}
}

- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 50
- 51
- 52
- 53
- 54
- 55
- 56
- 57
- 58
- 59
- 60
- 61
- 62
- 63
- 64
- 65
- 66
- 67
- 68
- 69
- 70
- 71
- 72
- 73
- 74
- 75
- 76
- 77
- 78
- 79
- 80
- 81
- 82
- 83
- 84
- 85
- 86
- 87
- 88
- 89
- 90
- 91
- 92
- 93
- 94
- 95
- 96
- 97
- 98
- 99
- 100
- 101
- 102
- 103
- 104
- 105
- 106
- 107
- 108
- 109
- 110
- 111
- 112
- 113
- 114
- 115
- 116
- 117
- 118
- 119
- 120
- 121
- 122
- 123
- 124
- 125
- 126
- 127
- 128
- 129
- 130
- 131
- 132
- 133
- 134
- 135
- 136
- 137
- 138
- 139
- 140
- 141
- 142
- 143
- 144
- 145
- 146
- 147
- 148
- 149
- 150
- 151
- 152
- 153
- 154
- 155
- 156
- 157
- 158
- 159
- 160
- 161
- 162
- 163
- 164
- 165
- 166
- 167
- 168
- 169
- 170
- 171
- 172
- 173
- 174
- 175
- 176
- 177
- 178
- 179
- 180
- 181
- 182
- 183
- 184
- 185
- 186
- 187
- 188
- 189
- 190
- 191
- 192
- 193
- 194
- 195
- 196
- 197
- 198
- 199
- 200
- 201
- 202
- 203
- 204
- 205
- 206
- 207
- 208
- 209
- 210
- 211
- 212
- 213
- 214
- 215
- 216
- 217
- 218
- 219
- 220
- 221
- 222
- 223
- 224
- 225
- 226
- 227
- 228
- 229
- 230
- 231
- 232
- 233
- 234
- 235
- 236
- 237
- 238
- 239
- 240
- 241
- 242
- 243
- 244
- 245
- 246
- 247
- 248
- 249
- 250
- 251
- 252
- 253
- 254
- 255
- 256
- 257
- 258
- 259
- 260
- 261
- 262
- 263
- 264
- 265
- 266
- 267
- 268
- 269
- 270
- 271
- 272
- 273
- 274
- 275
- 276
- 277
- 278
- 279
- 280