Concurrency in csharp (Asynchronous, Parallel, and Multithreaded Programming)

http://stephencleary.com/projects/

 

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
    /// <summary>
    ///
    /// </summary>
    public partial class Form2 : Form
    {
 
        //[ComVisible(false)]
        //public delegate void ParameterizedThreadStart(object objfield);
 
 
 
        /// <summary>
        ///
        /// </summary>
        public Form2()
        {
            InitializeComponent();
        }
        /// <summary>
        ///
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void Form2_Load(object sender, EventArgs e)
        {
            MyDu du = new MyDu();
 
            Control.CheckForIllegalCrossThreadCalls = false;
            Thread t1 = new Thread(new ThreadStart(TestMethod));
            Thread t2 = new Thread(new ParameterizedThreadStart(TestMethod));
            //Thread t3 = new Thread(new ThreadStart(du.setData));
            ThreadPool.QueueUserWorkItem(new WaitCallback(TaskProc1), new Person { Id = 1, Name = "测试", Count =100 });
 
            du.BegID = 100;
            du.EndID = 20000;
            du.Le1 = this.label3;
            du.Le2 = this.label4;
            Thread t3 = new Thread(new ThreadStart(du.Run));
 
            t1.IsBackground = true;
            t2.IsBackground = true;
            t3.IsBackground = true;
            t1.Start();
            t2.Start("hello");
            t3.Start();
        }
        /// <summary>
        ///
        /// </summary>
        /// <param name="x"></param>
        public void TaskProc1(object x)   //执行的任务
        {
            Person person = x as Person;
 
        }
        //
        private delegate void InvokeCallback(string msg);
        //
        private delegate void SetLabelDelegate(string value);
        /// <summary>
        ///
        /// </summary>
        void TestMethod()
        {
 
            //InvokeCallbackmsgCallback = new InvokeCallback(m_comm_MessageEvent);
            string v = "geovind du 1";
            this.label1.Text = v;
            //if (this.InvokeRequired)
            //{
            //    SetLabelDelegate d = new SetLabelDelegate(TestMethod);
            //    this.Invoke(d, new object[] { v});
            //}
            //else
            //{
            //    this.label1.Text = v;
            //}
        }
 
        void TestMethod(object data)
        {
            if (this.InvokeRequired)
            {
                SetLabelDelegate d = new SetLabelDelegate(TestMethod);
                this.Invoke(d, new object[] { data });
            }
            else
            {
                string datastr = data as string;
                this.label2.Text = datastr;
            }
        }
 
    }
 
    public class Person
    {
        public int Id { get; set; }
        public string Name { get; set; }
        public int Count { get; set; }
  
    }
    /// <summary>
    ///
    /// </summary>
    class MyDu
    {
       
            private int begID;
 
            public int BegID
 
            {
 
                set{this.begID=value;}
 
            }
 
            private int endID;
 
            public int EndID
 
            {
 
                set{this.endID=value;}
 
            }
 
            private Label le1;
 
            public Label Le1
            {
                set { this.le1 = value; }
            }
 
            private Label le2;
 
            public Label Le2
            {
                set { this.le2 = value; }
            }
           /// <summary>
           ///
           /// </summary>
            public void Run()
            {
 
                method(begID, endID, le1, le2);
 
            }
            /// <summary>
            ///
            /// </summary>
            /// <param name="begin"></param>
            /// <param name="end"></param>
            private void method(int begin,int end,Label labe,Label labe2)
            {
                labe.Text = begin.ToString();
                labe2.Text = end.ToString();
            }
 
    
}

  

posted @   ®Geovin Du Dream Park™  阅读(375)  评论(0编辑  收藏  举报
编辑推荐:
· AI与.NET技术实操系列:基于图像分类模型对图像进行分类
· go语言实现终端里的倒计时
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
阅读排行:
· 25岁的心里话
· 闲置电脑爆改个人服务器(超详细) #公网映射 #Vmware虚拟网络编辑器
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· 零经验选手,Compose 一天开发一款小游戏!
· 一起来玩mcp_server_sqlite,让AI帮你做增删改查!!
历史上的今天:
2015-10-27 csharp: InvokeHelper
2015-10-27 Free Slideshow, Gallery And Lightboxes Scripts
2015-10-27 35 Gallery – Ajax Slide
< 2025年3月 >
23 24 25 26 27 28 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 1 2 3 4 5
点击右上角即可分享
微信分享提示