C # assign timer and thread and GC,
Code
Code highlighting produced by Actipro CodeHighlighter (freeware) http://www.CodeHighlighter.com/--> // small Zhang class
Public class MrZhang
{
// In fact, the person who is sad about buying a ticket is John
Public static void BuyTicket ()
{
Console. WriteLine ("NND, let me buy a ticket every time, chicken! ");
}
Public static void BuyMovieTicket ()
{
Console. WriteLine ("I'll go, pick up a girl myself, and let me bring movie tickets! ");
}
}
// James class
Class MrMing
{
// Declare a delegate, which is actually a "command"
Public delegate void BugTicketEventHandler ();
Public static void Main (string [] args)
{
// Here is what this command is for. In this example, MrZhang. BuyTicket "buy a ticket for John"
BugTicketEventHandler myDelegate = new BugTicketEventHandler (MrZhang. BuyTicket );
// At this time, the delegate is attached with a specific method
MyDelegate ();
Console. ReadKey ();
}
}
BugTicketEventHandler myDelegate = new BugTicketEventHandler(MrZhang.BuyTicket);
This is the delegate declaration method. BugTicketEventHandler (delegate method); the delegate method must be added because the Delegate constructor is not empty.
Note: The delegate parameters and return types must be consistent with the delegate method. For example:
public delegate void BugTicketEventHandler();
Public static void BuyTicket ()
{
Console. WriteLine ("NND, let me buy a ticket every time, chicken! ");
}
Delegated chain:
MyDelegate + = MrZhang. BuyMovieTicket;
End!
The purpose of the delegate is similar to the function pointer of c ++, but it is improved. you can pass the function as a parameter in several functions, but you do not know the time to run it in advance. This is a typical application example. for example, if you want to implement the addition, subtraction, multiplication, division of two operands, you can define the operations required for a delegate function, in this case, you can define another operation implemented by a function with the delegate as the parameter, and then pass the corresponding function in.
Event listening Function + = delegate (function execution required)
Used in System. Timers. Timer
Three timer forms
// 1. Implement the timer for events triggered at user-defined intervals. This timer is most suitable for Windows Forms applications and must be used in Windows.
System. Windows. Forms. Timer
// 2. provides a mechanism for executing methods at a specified interval. This class cannot be inherited.
System. Threading. Timer
// 3. generate periodic events in the application.
System. Timers. Timer
The following two types should be different threads and the form control cannot be used directly.
Timer of the second type. there is a delay in the end of dispose (), similar to buying a breakfast queue. When the maximum breakfast (thread reaches), multiple requests for breakfast (thread) will be recorded, causing the execution to end, continuous resource consumption.
The third timer. stop () will cancel the application. Although there are several latencies, the effect is not big
Therefore, the first thread does not need to be applied. A single thread will be suspended, and the second and third threads need to be applied.
Write it at the end. GC: The garbage collection device does not touch the DLL for the time being. If there is no conflict, use close and dispose. The analysis structure may conflict with GC. In fact, if dispose is not pursued, it does not matter.
Note that the first buffer is cleared in the clear and end of response, and the second is ended in the js? Direct output.
———————————————————————————————————————————————
Communication between different threads
mythread = new Thread(new ThreadStart(BeginListen));
mythread.Start();
private void BeginListen()
{
SetLabelEventHandler setLabelEventHandler = delegate () // anonymous delegate + Invoke
{
Label1.Text = "I changed it ";
};
Label1.Invoke (setLabelEventHandler );
}—————————————————————————————————————————————————
Delegate and event
http://www.cnblogs.com/shensr/archive/2005/11/24/283653.html