zoukankan      html  css  js  c++  java
  • C# 函数调用学习代码

    一、前言

      本章只是我看教程学习的代码,仅仅作为自己使用的笔记。不做过多的介绍,毕竟我学得不精。

    二、代码

      本段代码的目标是实现,先输入1 ,在输出2.

      1、使用 Task.Delay().Wait()

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading;
    using System.Threading.Tasks;
    
    /// <summary>
    /// 函数调用学习
    /// </summary>
    namespace ConsoleApp1
    {
        class Program
        {
            static void Main(string[] args)
            {
                ///当前时间
                var bt = DateTime.Now.Ticks;
    
                new Test().DoEvent();
    
                Task.Delay(1).Wait();
    
                Console.WriteLine(2);
    
                Console.ReadLine();
    
            }
        }
    
        class Test
        {
            public void DoEvent()
            {
                var x = new Thread(new ThreadStart(() =>
                  {
                      Console.WriteLine("1");
                  }));
                x.Start();
            }
        }
    }

      2、使用委托

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading;
    using System.Threading.Tasks;
    
    /// <summary>
    /// 函数调用学习
    /// </summary>
    namespace ConsoleApp1
    {
        class Program
        {
            static void Main(string[] args)
            {
                ///当前时间
                var bt = DateTime.Now.Ticks;
    
                new Test().DoEvent(() =>
                {
                    Console.WriteLine("2");
    
                });
                Console.WriteLine($"{ (double)(DateTime.Now.Ticks - bt) / (double)10_000}/ms");
                Console.ReadLine();
    
            }
        }
    
        class Test
        {
            public void DoEvent(Action action)
            {
                
                var x = new Thread(new ThreadStart(() =>
                  {
                      Console.WriteLine("1");
                      action();
                      
                  }));
                x.Start();
               
            }
        }
    }

      3、使用Task

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading;
    using System.Threading.Tasks;
    
    /// <summary>
    /// 函数调用学习
    /// </summary>
    namespace ConsoleApp1
    {
        class Program
        {
            static void Main(string[] args)
            {
                ///当前时间
                var bt = DateTime.Now.Ticks;
                new Test().DoEvent();
                Console.WriteLine($"{ (double)(DateTime.Now.Ticks - bt) / (double)10_000}/ms");
                Console.ReadLine();
    
            }
        }
    
        class Test
        {
            public void DoEvent()
            {
                var t = new Task(() => { });//使用这样就可以先打印 1 在打印 2;
                var x = new Thread(new ThreadStart(() =>
                  {
                      Console.WriteLine("1");
                      t.Start();
                  }));
                x.Start();
                t.Wait();
            }
        }
    
    }
  • 相关阅读:
    [转]进程间通信----pipe和fifo
    [转]udev
    [转]netlink
    [转]进程间通信-----管道
    [转]socket
    [转]armv8 memory system
    [转]内核态和用户态
    [转]dpdk内存管理
    meeting and robert rules
    notion
  • 原文地址:https://www.cnblogs.com/gzbit-zxx/p/12361295.html
Copyright © 2011-2022 走看看