C#

C# 各种名词

Posted by Kerwen Blog on March 14, 2018

线程

C# 线程

1
2
3
4
5
6
7
8
9
10
11
12
static void Main(string[] args)
{
    Thread t = new Thread(Child);
    t.Start();
    Console.WriteLine("This is main thread.");
    Console.ReadKey();
}

static void Child()
{
    Console.WriteLine("This is in thread.");
}

运行结果:

1
2
This is main thread.
This is in thread.

程序运行的结果不能保证哪个先输出,因为线程是由操作系统调度,每次哪个线程在前面可以不同
传入参数

1
2
3
4
5
6
7
8
9
10
11
static void Main(string[] args)
{
    Thread t2 = new Thread(ChildWithParam);
    t2.Start("test");
    Console.ReadKey();
}

static void ChildWithParam(object str)
{
    Console.WriteLine("Child with parameter: " + str.ToString());
}

异步

同步异步

异步,多线程和并行的区别
http://kerwenzhang.github.io/c%23/2016/11/16/MultiThread-vs-Asynchronous/

异步(Asynchronized)和同步(Synchronised)是相对的,同步就是顺序执行,执行完一个再执行下一个,需要等待、协调运行。异步就是彼此独立,在等待某事件的过程中继续做自己的事,不需要等待这一事件完成后再工作。线程就是实现异步的一个方式。异步是让调用方法的主线程不需要同步等待另一线程的完成,从而可以让主线程干其它的事情。
异步和多线程并不是一个同等关系,异步是最终目的,多线程只是我们实现异步的一种手段。

线程的适用范围则是那种需要长时间CPU运算的场合。 当需要执行I/O操作时,使用异步操作比使用线程+同步 I/O操作更合适。

回调函数

https://www.cnblogs.com/weloveshare/p/5755871.html
http://www.jb51.net/article/98976.htm
https://www.cnblogs.com/cuihongyu3503319/archive/2010/11/12/1875467.html

反射

委托(delegate)

http://kerwenzhang.github.io/c%23/2015/11/17/CSharp-delegate/

1
2
3
4
5
6
7
8
//第1步,申明,并说明该委托的签名(即参数个数,类型)  
public delegate int SomeDelegate(string s, bool b);  
//第2步,定义符合委托签名(即参数个数与类型一致)的函数。  
private int SomeFunction(string s, bool b){...} 
//第3步,创建一个委托,把函数名作为参数传递给该委托的构造函数。  
SomeDelegate sd = new SomeDelegate(SomeFunction);  
//第4步,使用该委托的实例调用该实例函数。  
sd("somestring", true); 

Example:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
public delegate void BugTicketEventHandler(); 
public class MrZhang     
{   
    public static void BuyTicket()     
    {     
        Console.WriteLine("Buy ticket");     
    }     
}      
  
class MrMing     
{  
    public delegate void BugTicketEventHandler();     

    public static void Main(string[] args)     
    {        
        BugTicketEventHandler myDelegate = new BugTicketEventHandler(MrZhang.BuyTicket);   
        
        myDelegate();     
        Console.ReadKey();     
    }     
}    

委托的参数和返回类型,都要和你要具体委托的方法要一致

事件(Event)

http://kerwenzhang.github.io/c%23/2015/11/17/CSharp-event/
事件最常用的应用场景是图形用户界面(GUI),如WinForm和WebForm,需要程序来对事件作出响应。可以是一个按钮点击事件,菜单选择事件,文件传输完成事件等。简单的说,某件事发生了,你必须要作出响应。你不能预测事件发生的顺序。只能等事件发生,再作出相应的动作来处理。

事件的发生一般都牵扯2个角色: 事件发行者(Publisher):一个事件的发行者,也称作是发送者(sender),当发行者本身状态信息变动时,便触发一个事件,并通知说有的事件订阅者。
事件订阅者(Subscriber):对事件感兴趣的对象,也称为Receiver,可以注册感兴趣的事件,在事件发行者触发一个事件后,会自动执行这段代码。

发布和订阅所有的对象都能发布一系列事件供其他类订阅。当发布对象触发事件的时候,所有订阅的类都会被通知到。有了这种机制,你的对象可以说,“我要告诉你们一些事情”,其他的类会说“好的,告诉我们发生什么了”。 注意发布类不需要知道谁订阅了事件。 它只管触发事件。谁响应了事件,怎么响应的,发布类都不关心。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
//第1步,申明一个委托
public delegate int SomeDelegate(string s, bool b);
//第2步,定义符合委托签名(即参数个数与类型一致)的函数。
private int SomeFunction(string s, bool b){...}
//--前2步都相同
//第3步,申明一个的事件,并定义该事件能接受的签名。
public event SomeDelegate SomethingHappened;
//第4步,通过事件来保存符合签名的函数(可以是多个)
myObj.SomethingHappened += new SomeDelegate(SomeFunction);
//第5步,根据需要在适当的时机,运行事件中保存的函数
if( SomethingHappened != null ) 
{
    foreach( SomeDelegate sd in SomethingHappened.GetInvocationList() ) 
    {
        int result = sd("somestring", true);
    }
}

Example:

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
public class Publisher   
{ 
    public delegate void PublishEventHander(); 
    public event PublishEventHander OnPublish; 
    
    public void PublishNews()   
    {
        if (OnPublish != null)   
        {   
            Console.WriteLine("最新一期的《火影忍者》今天出版哦!");   
            OnPublish();   
        }   
    }   
}   

public class MrMing   
{
    public static void ReceiveNews()   
    {   
        Console.WriteLine("嘎嘎,我已经收到最新一期的《火影忍者》啦!!");       
    }   
} 

class Story   
{   
    public static void Main(string[] args)   
    {   
        Publisher publisher = new Publisher();   

        publisher.OnPublish += new Publisher.PublishEventHander(MrMing.Receive);   
        //另一种事件注册方式   
        //publisher.OnPublish += MrMing.Receive;   

        publisher.PublishNews();   
        Console.ReadKey();   
    }   
}  

事件的本质就是一个委托链

Handler

托管

Timer

链接