C # Asynchronous: Implementing one of the simplest asynchronous

Source: Internet
Author: User

Asynchronous is the method of asynchronous execution, this is a good understanding.

What's the use of asynchrony?

I've only heard it before, and I don't want to dispute it. But it was time to meet the need for this.

Like what:

Regular execution, if not asynchronous method, also do not use timer, only with Thread.Sleep to interval.

The execution method itself takes time, and the meaning of timing changes--two times of execution must be more than 1 minutes apart.

Here is the simplest implementation of an asynchronous demo:

using System using System.Collections.Generic; using System.Linq; using
System.Threading;

Using System.Diagnostics;
        Namespace ConsoleApplication20 {Class Program {public delegate void Testdelegate (string name);
            static void Main (string[] args) {testdelegate d = Test;
            Console.WriteLine ("Beginning: {0:yyyy-mm-dd HH:mm:ss.fff}", DateTime.Now);
            D.begininvoke ("Xiaoming 1", NULL, NULL);
            D.begininvoke ("Xiaoming 2", NULL, NULL);
            D.begininvoke ("Xiaoming 3", NULL, NULL);
            D.begininvoke ("Xiao Ming 4", NULL, NULL);
            D.begininvoke ("Xiaoming 5", NULL, NULL);

            Console.WriteLine ("End: {0:yyyy-mm-dd HH:mm:ss.fff}", DateTime.Now);
        Console.read ();  static void Test (string name) {Console.WriteLine ("TestMethod: {0:yyyy-mm-dd HH:mm:ss.fff}")
        {1} ", DateTime.Now, name); }
    }
}

Visible by Result: BeginInvoke is indeed asynchronous when invoked.


Attached: Timer code

Using System;
Using System.Collections.Generic;
Using System.Linq;
Using System.Text;
Using System.Timers;
Using System.Threading;

Namespace ConsoleApplication21
{
    class program
    {
        static void Main (string[] args)
        {
            System.Timers.Timer Timer = new System.Timers.Timer ();
            Timer. Enabled = true;
            Timer. Interval = 1000;
            Timer. Start ();

            Timer. Elapsed + = new Elapsedeventhandler (printpoint);
            Console.WriteLine ("End");
            Console.read ();
        }

        static void Printpoint (object sender, Elapsedeventargs e) 
        {
            DateTime dtcurr = DateTime.Now;
            Console.WriteLine ("{0:yyyy-mm-dd HH:mm:ss.fff}", Dtcurr);
            Thread.Sleep ();
            Console.WriteLine ("{0:yyyy-mm-dd hh:mm:ss.fff}_{1:yyyy-mm-dd HH:mm:ss.fff}", Dtcurr, DateTime.Now);}}

The result is visible: The Timer event is also asynchronous, each event even if the execution time is longer, does not cause the next timed event delay execution.





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.