C # delegating timer and thread as well as GC

Source: Internet
Author: User
Tags event listener ticket

Code
Code highlighting produced by Actipro Codehighlighter (freeware) http://www.CodeHighlighter.com/-->//Xiao Zhang Class
public class Mrzhang
{
In fact, the sad figure of buying a ticket is Xiao Zhang
public static void Buyticket ()
{
Console.WriteLine ("NND, every time let me go to buy tickets, chicken!" ");
}
public static void Buymovieticket ()
{
Console.WriteLine ("I go, I pick up the girls, and let me bring the movie ticket!" ");
}
}
Xiao Ming class
Class Mrming
{
Declaring a delegate is, in fact, a "command."
public delegate void Bugticketeventhandler ();
public static void Main (string[] args)
{
Here is the specific explanation of what this command is doing, this example is Mrzhang.buyticket "small ticket Buy Ticket"
Bugticketeventhandler mydelegate = new Bugticketeventhandler (mrzhang.buyticket);
At this time the delegate was attached to a specific method
MyDelegate ();
Console.readkey ();
}
}
Bugticketeventhandler mydelegate = new Bugticketeventhandler (mrzhang.buyticket);
This is the declaration method of the delegate, Bugticketeventhandler (the delegate method), and the delegate's method must be added because the delegate's constructor is not empty.
Note: The parameter and return type of the delegate must be the same as the method you want to delegate, for example:
public delegate void Bugticketeventhandler ();

public static void Buyticket ()
{
Console.WriteLine ("NND, every time let me go to buy tickets, chicken!" ");
}

Entrust chain:

MyDelegate + = Mrzhang.buymovieticket;

End!


The purpose of a delegate is similar to that of a C + + function pointer, but it is improved. It is a typical application example that you can pass a function parameter to a time when you have several functions and you do not know what to do in advance. For example, if you want to implement the subtraction of two operands, then you can define a delegate four functions specifically with what operation requires the user to decide, then you can define a function as a delegate as a parameter to implement what the operation of the corresponding function to pass in the line

Event Listener Function + = delegate (need to execute function)
Used in the System.Timers.Timer.

Three types of Timer
1. Implement a timer that raises events at user-defined intervals. This timer is best used in Windows Forms applications and must be used in Windows.
System.Windows.Forms.Timer

2. Provides a mechanism for executing a method at a specified time interval. This class cannot be inherited.
System.Threading.Timer

3. Generate recurring events in the application.
System.Timers.Timer
The following two types should be different threads and cannot directly use the controls above the form
The second timer.dispose () end has a delay, similar to buying a breakfast line, when the maximum breakfast (thread is reached), multiple requests for breakfast (threads) are recorded, resulting in an end-of-execution behavior, sustained resource consumption.
The Third Kind of timer.stop () will cancel the application, although there are several delays that affect little
So, the first one without the application thread, single-threaded will be suspended animation, the second and third need to apply for thread
Write it in the end. GC, the garbage collector temporarily does not contact the DLL, non-conflict with the close and dispose bar, destructors may conflict GC, in fact, dispose if not pursue may also not matter
Note that in response clear and end the first cleanup buffer, the second end 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";
};
}—————————————————————————————————————————————————
Delegate and Event
Http://www.cnblogs.com/shensr/archive/2005/11/24/283653.html

C # delegating timer and thread as well as GC

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.