溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶服務(wù)條款》

C#中怎么實(shí)現(xiàn)代碼延時(shí)

發(fā)布時(shí)間:2021-08-04 13:52:32 來(lái)源:億速云 閱讀:667 作者:Leah 欄目:開發(fā)技術(shù)

本篇文章給大家分享的是有關(guān)C#中怎么實(shí)現(xiàn)代碼延時(shí),小編覺得挺實(shí)用的,因此分享給大家學(xué)習(xí),希望大家閱讀完這篇文章后可以有所收獲,話不多說(shuō),跟著小編一起來(lái)看看吧。


Task.Delay();異步實(shí)現(xiàn)

using System;
using System.Threading.Tasks;

namespace csharpYS
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("Mian方法第一次輸出...");
            #region 第一種形式 
            var task_1 = Task.Run(async delegate
            {
                await Task.Delay(3000);
                Console.WriteLine("3秒后執(zhí)行,方式一 輸出語(yǔ)句...");
                return "異步執(zhí)行result"; //可以得到一個(gè)返回值(int,bool,string都試了)
            });
            #endregion

            Console.WriteLine("Mian方法第二次輸出,調(diào)用延時(shí)...");
            Console.WriteLine("task_1 的 Status:{0}, 結(jié)果: {1}",task_1.Status, task_1.Result);
            Console.WriteLine("第一種形式,延時(shí)結(jié)束...");

            #region 第二種形式
            Task task_2 = Task.Run(task_YS);
            //task_2.Wait();  //注釋打開則等待task_2延時(shí),注釋掉則不等待
            #endregion

            Console.WriteLine("Mian方法最后一次輸出,Main方法結(jié)束...");
            Console.ReadKey();
        }

        public static async Task task_YS()
        {
            await Task.Delay(5000);
            Console.WriteLine("5秒后執(zhí)行,方式二 輸出語(yǔ)句...");
        }
    }

}

下圖一為注釋運(yùn)行結(jié)果,圖二為注釋打開運(yùn)行結(jié)果:(建議使用時(shí)自行實(shí)踐)

C#中怎么實(shí)現(xiàn)代碼延時(shí)

C#中怎么實(shí)現(xiàn)代碼延時(shí)

覺得上面方法不適用的童鞋,可以試試使用線程的方式:

線程實(shí)現(xiàn):

簡(jiǎn)例:

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

namespace ExceptionDeme
{
    class ThreadDemo
    {
        static void Main(string[] args)
        {
            Console.WriteLine("Main方法開始執(zhí)行...");
            Thread threadA = new Thread(DownLoadFile);
            threadA.Start();
            Console.WriteLine("Main方法執(zhí)行結(jié)束...");

            Console.ReadKey();
        }

        static void DownLoadFile()
        {
            //模擬開始下載 2S 后完成
            Console.WriteLine("開始下載,此協(xié)程的Id是:" + Thread.CurrentThread.ManagedThreadId);
            Thread.Sleep(2000);
            Console.WriteLine("下載完成");
        }
}

C#中怎么實(shí)現(xiàn)代碼延時(shí)

相關(guān)連接:
C# 線程簡(jiǎn)介
C# 開啟線程的幾種方式

計(jì)時(shí)器方式實(shí)現(xiàn):

 class Program
    {
        static void Main(string[] args)
        {
   			Console.WriteLine("Czhenya  ... Main Start");
            WaitFunctions(2);
            Console.WriteLine("Czhenya  ... Main End");
		}
		
 	    public static void WaitFunctions(int waitTime)
        {
            if (waitTime <= 0) return;

            Console.WriteLine("開始執(zhí)行 ...");
            DateTime nowTimer = DateTime.Now;
            int interval = 0;
            while (interval < waitTime)
            {
                TimeSpan spand = DateTime.Now - nowTimer;
                interval = spand.Seconds;
            }

            Console.WriteLine(waitTime + "秒后繼續(xù) ...");
        }
  }

執(zhí)行截圖:

C#中怎么實(shí)現(xiàn)代碼延時(shí)

以上就是C#中怎么實(shí)現(xiàn)代碼延時(shí),小編相信有部分知識(shí)點(diǎn)可能是我們?nèi)粘9ぷ鲿?huì)見到或用到的。希望你能通過(guò)這篇文章學(xué)到更多知識(shí)。更多詳情敬請(qǐng)關(guān)注億速云行業(yè)資訊頻道。

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI