C#入门之异步编程

在很多情况下,我们的任务前后之间没有必然的联系的,所以我们可以不需要等待前面命令结束,才开始后面的任务。我们可以多个任务并行运行,这就是异步编程。

首先我们先来看一个常规的程序:

using System;
using System.Diagnostics;
using System.Linq;
using System.Net.NetworkInformation;

namespace ConsoleApp30
{
    class Program
    {
        static void Main(string[] args)
        {
            Stopwatch stopwatch = Stopwatch.StartNew();    // 统计程序运行的时间
            Ping ping = new Ping();
            String NetNumber = "192.168.0.";
            int[] HosNumer = Enumerable.Range(1, 254).ToArray();
            foreach( int i in HosNumer )
            {
                string IP = NetNumber + i;
                PingReply pingReply = ping.Send($"{IP}");
                if(pingReply.Status == IPStatus.Success)
                {
                    Console.WriteLine($"The IP {IP} ping is pass.");
                }
                else
                {
                    Console.WriteLine($"The IP {IP} ping is failed.");
                }
            }
            Console.WriteLine($"Time is {stopwatch.ElapsedMilliseconds}");
        }
    }
}

这是一个 ping 程序,它会 ping 指定的 IP 的地址的状态,并返回结果。

运行结果:

The IP 192.168.0.1 ping is pass.
The IP 192.168.0.2 ping is failed.
The IP 192.168.0.3 ping is failed.
The IP 192.168.0.4 ping is failed.
The IP 192.168.0.5 ping is failed.
......
......
The IP 192.168.0.252 ping is failed.
The IP 192.168.0.253 ping is failed.
The IP 192.168.0.254 ping is failed.
Time is 751872     // 程序运行的时间

使用异步的方式:

using System;
using System.Diagnostics;
using System.Linq;
using System.Net.NetworkInformation;
using System.Threading.Tasks;

namespace ConsoleApp30
{
    class Program
    {
        static async Task Main(string[] args)   // 这里要使用 async
        {
            Stopwatch stopwatch = Stopwatch.StartNew();    // 统计程序运行的时间
            Ping ping = new Ping();
            String NetNumber = "192.168.0.";
            int[] HosNumer = Enumerable.Range(1, 254).ToArray();
            foreach( int i in HosNumer )
            {
                string IP = NetNumber + i;
                 // 添加 await, 并且使用 ping 的方式也有所不同
                PingReply pingReply = await ping.SendPingAsync($"{IP}");  
                if(pingReply.Status == IPStatus.Success)
                {
                    Console.WriteLine($"The IP {IP} ping is pass.");
                }
                else
                {
                    Console.WriteLine($"The IP {IP} ping is failed.");
                }
            }
            Console.WriteLine($"Time is {stopwatch.ElapsedMilliseconds}");
        }
    }
}

运行结果:

The IP 192.168.0.1 ping is pass.
The IP 192.168.0.2 ping is failed.
The IP 192.168.0.3 ping is failed.
The IP 192.168.0.4 ping is failed.
The IP 192.168.0.5 ping is failed.
......
......
The IP 192.168.0.250 ping is failed.
The IP 192.168.0.251 ping is failed.
The IP 192.168.0.252 ping is failed.
The IP 192.168.0.253 ping is failed.
The IP 192.168.0.254 ping is failed.
Time is 438322

这次测试,所花费的时间,比之前少了很多。


<<:  Swift纯Code之旅 Day19. 「ViewController好乱(1) - MVC介绍)」

>>:  Day 29 - ios 开发实作 (今天还要继续吃吗APP-3)

Ubuntu巡航记(4) -- Rust 安装

前言 Rust 是一个现代版的 C/C++ 程序语言,它加入物件导向、套件安装(cargo)、函数式...

【HTML】标记?标签?HTML元素?

【前言】 本系列为个人前端学习之路的学习笔记,在过往的学习过程中累积了很多笔记,如今想藉着IT邦帮忙...

Day16 Combine 03 - Subscriber

Subscriber 与Publisher 相对应,是观察者模式中的Observer,Publish...

你是谁、你的过去都不重要,成功的能力永远都从你开始。

你是谁、你的过去都不重要,成功的能力永远都从你开始。 It doesn’t matter who y...

[D27] : 一个Queue+Docker在Local的实作(1/4)

没想到这周会炸忙 =皿= 这篇目的是想做到一个很简单的Docker和Message Queue实作...