当前位置:网站首页>c#委托、线程、定时器学习感悟
c#委托、线程、定时器学习感悟
2022-04-22 05:43:00 【从刻意到习惯】
委托
定义方式举例:public delegate void DogFunction(int a)
该类型的委托针对返回值类型为void,参数类型为单个int的方法
for exeample:void method(int a)
形象解释下委托:可以看作是在窗口买票事件,委托相当于特定窗口,方法相当于特定人群,不同的人群需要在不同的窗口买票。一种类型对应一种类型,公共规则不能乱,委托与委托的对象也是如此。
表现形式:`
Example ex = new Example();
DogFunction dog = new DogFunction(ex.test1);
dog(18); // 相当于 ex.test1(18)
// ex.test1(18);
dog = new DogFunction(ex.test2);
dog(19); // ex.test2(19);`
委托的作用:个人觉得·委托的好处在于简化,在方法较多的情况下,每个方法都需要实例化,使用委托可以将同一种类型的方法都代表了,简单明了。
线程
c#线程可以使用委托的方法来实现定义方式举例:
MyCounter counter = new MyCounter();
Thread t = new Thread(new ThreadStart(counter.Run));
t.Start();
说明:t.IsBackground = true;//设TRUE的线程则主线程退出就退出,设FALSE的线程则继续执行。MyCounter 是一个类,run是里面定义的一个方法,使用委托实现线程中的一些功能
定时器
表现形式:
Timer timer = new Timer(new TimerCallback(SimpleJob)
, null
, 1000//第一次间隔时间
, 1000);//前后间隔时间
public static void SimpleJob(object state)
{
for (int i = 1; i <= 3; i++)
{
Console.WriteLine( i );
Thread.Sleep(350);
}
Console.WriteLine("... OK ");
}
定时器和线程差不多,以上是我对他们的理解,暂时理解到这种程度,如果有不对的地方希望批评指正
版权声明
本文为[从刻意到习惯]所创,转载请带上原文链接,感谢
https://blog.csdn.net/weixin_43491924/article/details/116527188
边栏推荐
猜你喜欢

STM32 study notes 4 - HC_ Commissioning record of SR04 ultrasonic ranging module

AD5724 双极性ADC

Blue Bridge Cup Sprint - binary enumeration

Blue Bridge Cup embedded expansion board learning nixie tube

Blue Bridge Cup embedded expansion board learning lis302dl

日常学习记录——解决graphviz中文乱码问题

STM32学习笔记2——设置GPIO寄存器实现流水灯

蓝桥杯嵌入式扩展板学习之光敏电阻

第72篇 LeetCode题目练习(五) 5.最长回文子串

pykmip测试
随机推荐
m1芯片上编译arm64的openssl
Part 84 leetcode sword refers to offer dynamic programming (I) Fibonacci sequence
Blue Bridge Cup embedded expansion board learning nixie tube
Jeecgboot online form development - control configuration
The Localtime function affects performance
汇编 makefile
10 - process control - while loop statement
chorme调试工具
geojson文件shapefile文件 批量 互转 小工具
Intel SGX thread lock
为CPropertySheet窗口添加最小化按钮
第72篇 LeetCode题目练习(五) 5.最长回文子串
DS18B20 of Blue Bridge Cup embedded expansion board learning
VB操作excel 格式设置及打印页面设置(精简)
Photoresist for learning of Blue Bridge Cup embedded expansion board
11 - process control - for loop
二分查找及其经典应用(左边界,右边界等)
hp unix上编译openssl并使用
Code color difference of QT learning
QT添加pri编译运行: error: fatal error: no input files问题解决