• C# 多线程


    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 = test(3, 5);
                //Console.WriteLine(num);
                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()
            {
                // operation.Invoke();
                this.Invoke(operation);
            }
            public void test3()
            {
                textBox1.Text = "123";
            }
    
            //使用lamada表达式
            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 action = this.DoSomethingLong;
                 调用委托(同步调用)
                //action.Invoke("btnAsync_Click_1");
                 异步调用委托
                //action.BeginInvoke("btnAsync_Click_2", null, null);
    
                Action<string> action = this.DoSomethingLong;
                for (int i = 0; i < 5; i++)
                {
                    //Thread.Sleep(5);
                    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")}***************");
    
            }
            //上述结论: BeginInvoke的返回值就是callback委托的参数。
    
    
    
    
            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, "异步调用返回值");
    
                //endinvoke
                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
  • 相关阅读:
    leetcode 46. 全排列(经典)
    数据挖掘——如何利用Python实现产品关联性分析apriori算法篇
    JAVA中如何实现代码优化(技巧讲解)
    AI时代你一定要知道的Agent概念
    代码随想录算法训练营第五十八天| 583. 两个字符串的删除操作 72. 编辑距离
    同花顺股票交易接口的端口限速
    Rpc-实现Zookeeper注册中心
    直销系统开发是找开发公司还是外包团队?
    新型大数据架构之湖仓一体(Lakehouse)架构特性说明——Lakehouse 架构(一)
    2.22号qt
  • 原文地址:https://blog.csdn.net/qq_42607586/article/details/133923782