欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

C#使用DateTime.Now靜態(tài)屬性動態(tài)獲得系統(tǒng)當(dāng)前日期和時(shí)間

 更新時(shí)間:2024年01月22日 09:21:10   作者:wenchm  
本文主要介紹了C#使用DateTime.Now靜態(tài)屬性動態(tài)獲得系統(tǒng)當(dāng)前日期和時(shí)間,DateTime結(jié)構(gòu)的Now靜態(tài)屬性只是得到一個(gè)系統(tǒng)時(shí)間對象,該時(shí)間對象不會隨著系統(tǒng)時(shí)間的變化而變化,如果要動態(tài)顯示系統(tǒng)時(shí)間,可以使用計(jì)時(shí)器間隔地獲取系統(tǒng)時(shí)間對象并顯示,感興趣的可以了解一下

使用DateTime結(jié)構(gòu)的Now靜態(tài)屬性可以輕松地獲取當(dāng)前系統(tǒng)時(shí)間。

DateTime結(jié)構(gòu)的Now靜態(tài)屬性只是得到一個(gè)系統(tǒng)時(shí)間對象,該時(shí)間對象不會隨著系統(tǒng)時(shí)間的變化而變化,如果要動態(tài)顯示系統(tǒng)時(shí)間,可以使用計(jì)時(shí)器間隔地獲取系統(tǒng)時(shí)間對象并顯示。

可以適當(dāng)?shù)厥褂镁€程替代Timer控件。在窗體應(yīng)用程序開發(fā)過程中,應(yīng)當(dāng)盡量避免使用窗體線程做高強(qiáng)度的運(yùn)算或IO操作,如果窗體線程參與了過多的運(yùn)算,會導(dǎo)致用戶的操作不能及時(shí)分配到資源,用戶界面會出現(xiàn)卡或無響應(yīng)情況。

一、實(shí)例

1.源碼

//使用DateTime的Now靜態(tài)屬性動態(tài)獲取系統(tǒng)時(shí)間
namespace _053
{
    public partial class Form1 : Form
    {

        public Form1()
        {
            InitializeComponent();
            Load += Form1_Load;
        }
        private void Form1_Load(object? sender, EventArgs e)
        {
            SuspendLayout();
            // 
            // Form1
            // 
            AutoScaleDimensions = new SizeF(7F, 17F);
            AutoScaleMode = AutoScaleMode.Font;
            ClientSize = new Size(394, 41);
            Name = "Form1";
            StartPosition = FormStartPosition.CenterScreen;
            Text = "動態(tài)獲取系統(tǒng)時(shí)間";         
            ResumeLayout(false);

            Thread thread = new(//創(chuàng)建線程
                () =>                   //使用lambda表達(dá)式
                {
                    while (true)        //無限循環(huán)
                    {
                        Invoke(         //操作窗體線程
                              (MethodInvoker)delegate ()//使用匿名方法
                              {
                                  Refresh();//刷新窗體
                                  Graphics graphics = CreateGraphics(); //創(chuàng)建繪圖對象
                                  graphics.DrawString("系統(tǒng)時(shí)間:" +     //在窗體中繪出系統(tǒng)時(shí)間
                                      DateTime.Now.ToString("yyyy年MM月dd日 HH時(shí)mm分ss秒"),
                                      new Font("Times New Roman", 14),
                                      Brushes.Blue,
                                      new Point(10, 10));
                              });
                        Thread.Sleep(1000);//線程掛起1秒鐘
                    }
                })
                {
                    IsBackground = true   //將線程設(shè)置為后臺線程
                };
            thread.Start();               //線程開始執(zhí)行
        }
    }
}

2.生成效果

1秒鐘一刷新。

二、相關(guān)知識點(diǎn)

1.Thread類 

創(chuàng)建和控制線程,設(shè)置其優(yōu)先級并獲取其狀態(tài)。 

(1)Thread.Sleep()方法

將當(dāng)前線程掛起指定的時(shí)間。

  • 重載
Sleep(Int32)將當(dāng)前線程掛起指定的毫秒數(shù)。
Sleep(TimeSpan)將當(dāng)前線程掛起指定的時(shí)間。
  •  Sleep(Int32)

將當(dāng)前線程掛起指定的毫秒數(shù)。

public static void Sleep (int millisecondsTimeout);

參數(shù)
millisecondsTimeout    Int32
掛起線程的毫秒數(shù)。 如果 millisecondsTimeout 參數(shù)的值為零,則該線程會將其時(shí)間片的剩余部分讓給任何已經(jīng)準(zhǔn)備好運(yùn)行的、具有同等優(yōu)先級的線程。 如果沒有其他已經(jīng)準(zhǔn)備好運(yùn)行的、具有同等優(yōu)先級的線程,則不會掛起當(dāng)前線程的執(zhí)行。

例外
ArgumentOutOfRangeException
超時(shí)值為負(fù)且不等于 Infinite。
// Sleep(Int32)
// 使用 Sleep 方法來阻止應(yīng)用程序的main線程。
namespace ConsoleApp12
{
    class Example
    {
        static void Main()
        {
            for (int i = 0; i < 5; i++)
            {
                Console.WriteLine("Sleep for 2 seconds.");
                Thread.Sleep(2000);
            }
            Console.WriteLine("Main thread exits.");
        }
    }
}

/* 運(yùn)行結(jié)果:
Sleep for 2 seconds.
Sleep for 2 seconds.
Sleep for 2 seconds.
Sleep for 2 seconds.
Sleep for 2 seconds.
Main thread exits.
 */
  • Sleep(TimeSpan)        將當(dāng)前線程掛起指定的時(shí)間。
public static void Sleep (TimeSpan timeout);

參數(shù)
timeout    TimeSpan
掛起線程的時(shí)間量。 如果 timeout 參數(shù)的值為 Zero,則該線程會將其時(shí)間片的剩余部分讓給任何已經(jīng)準(zhǔn)備好運(yùn)行的、具有同等優(yōu)先級的線程。 如果沒有其他已經(jīng)準(zhǔn)備好運(yùn)行的、具有同等優(yōu)先級的線程,則不會掛起當(dāng)前線程的執(zhí)行。

例外
ArgumentOutOfRangeException
的 timeout 值為負(fù),不等于 Infinite 以毫秒為單位,或大于 Int32.MaxValue 毫秒。
// Sleep(TimeSpan)
// 使用 Sleep(TimeSpan) 方法重載來阻止應(yīng)用程序的main線程五次,每次兩秒。
namespace ConsoleApp13
{
    class Example
    {
        static void Main()
        {
            TimeSpan interval = new(0, 0, 2);

            for (int i = 0; i < 5; i++)
            {
                Console.WriteLine("Sleep for 2 seconds.");
                Thread.Sleep(interval);
            }
            Console.WriteLine("Main thread exits.");
        }
    }
}
/* 運(yùn)行結(jié)果:
Sleep for 2 seconds.
Sleep for 2 seconds.
Sleep for 2 seconds.
Sleep for 2 seconds.
Sleep for 2 seconds.
Main thread exits.
 */

(2)Thread(ThreadStart)

初始化 Thread 類的新實(shí)例。

public Thread (System.Threading.ThreadStart start);

參數(shù)
start    ThreadStart
表示開始執(zhí)行此線程時(shí)要調(diào)用的方法的 ThreadStart 委托。

例外
ArgumentNullException
start 參數(shù)為 null。
// Thread(ThreadStart)
// 創(chuàng)建并執(zhí)行靜態(tài)方法的線程
namespace ConsoleApp10
{
    class Test
    {   
        static void Main()
        {
            Work.DoWork();
            Thread newthread = new(Work.DoWork);    //創(chuàng)建線程=把要處理的方法放進(jìn)這個(gè)線程
            Thread newThread = new(new ThreadStart(Work.DoWork));   //等效語句
            newthread.Start();
            newThread.Start();
        }
    }

    class Work
    {      
        public static void DoWork()
        {
            Console.WriteLine("Thread newThread=new()和 newThread.Start()總是成對出現(xiàn)的,");
            Console.WriteLine("前者負(fù)責(zé)創(chuàng)建一個(gè)新線程,后者負(fù)責(zé)執(zhí)行這個(gè)線程");
        }
    }
}
// 運(yùn)行結(jié)果:
/*
Thread newThread=new()和 newThread.Start()總是成對出現(xiàn)的,
前者負(fù)責(zé)創(chuàng)建一個(gè)新線程,后者負(fù)責(zé)執(zhí)行這個(gè)線程
Thread newThread=new()和 newThread.Start()總是成對出現(xiàn)的,
前者負(fù)責(zé)創(chuàng)建一個(gè)新線程,后者負(fù)責(zé)執(zhí)行這個(gè)線程
Thread newThread=new()和 newThread.Start()總是成對出現(xiàn)的,
前者負(fù)責(zé)創(chuàng)建一個(gè)新線程,后者負(fù)責(zé)執(zhí)行這個(gè)線程

 */
// 創(chuàng)建執(zhí)行實(shí)例方法的線程
namespace ConsoleApp11
{
    class Test
    {
        static void Main()
        {
            Work threadWork = new(); //創(chuàng)建實(shí)例方法
            Thread newthread = new(Work.DoWork);    
            Thread newThread = new(new ThreadStart(Work.DoWork));
            newthread.Start();
            newThread.Start();
        }
    }

    class Work
    {
        public static void DoWork()
        {
            Console.WriteLine("Thread newThread=new()和 newThread.Start()總是成對出現(xiàn)的,");
            Console.WriteLine("前者負(fù)責(zé)創(chuàng)建一個(gè)新線程,后者負(fù)責(zé)執(zhí)行這個(gè)線程");
        }
    }
}
//運(yùn)行結(jié)果:
/*
Thread newThread=new()和 newThread.Start()總是成對出現(xiàn)的,
前者負(fù)責(zé)創(chuàng)建一個(gè)新線程,后者負(fù)責(zé)執(zhí)行這個(gè)線程
Thread newThread=new()和 newThread.Start()總是成對出現(xiàn)的,
前者負(fù)責(zé)創(chuàng)建一個(gè)新線程,后者負(fù)責(zé)執(zhí)行這個(gè)線程

 */

(3)Thread.IsBackground 屬性

獲取或設(shè)置一個(gè)值,該值指示某個(gè)線程是否為后臺線程。

public bool IsBackground { get; set; }

屬性值
Boolean
如果此線程為或?qū)⒊蔀楹笈_線程,則為 true;否則為 false。

例外
ThreadStateException
線程終止。
// Thread.IsBackground 屬性
// 創(chuàng)建前臺線程和后臺線程,對前臺線程和后臺線程的行為進(jìn)行對比。
//  前臺線程使進(jìn)程保持運(yùn)行,直到完成其 for 循環(huán)并終止。
//  由于前臺線程已完成執(zhí)行,因此后臺線程將終止進(jìn)程。
namespace ConsoleApp15
{
    class Example
    {
        static void Main()
        {
            BackgroundTest shortTest = new(5);
            Thread foregroundThread =
               new(new ThreadStart(shortTest.RunLoop));

            BackgroundTest longTest = new(50);
            Thread backgroundThread =
                new(new ThreadStart(longTest.RunLoop))
                {
                    IsBackground = true
                };

            foregroundThread.Start();
            backgroundThread.Start();
        }
    }

    class BackgroundTest(int maxIterations)
    {
        readonly int maxIterations = maxIterations;
        public void RunLoop()
        {
            for (int i = 0; i < maxIterations; i++)
            {
                Console.WriteLine("{0} count: {1}",
                    Thread.CurrentThread.IsBackground ?
                       "Background Thread" : "Foreground Thread", i);
                Thread.Sleep(250);
            }
            Console.WriteLine("{0} finished counting.",
                              Thread.CurrentThread.IsBackground ?
                              "Background Thread" : "Foreground Thread");
        }
    }
}
// 運(yùn)行結(jié)果:
/*
Foreground Thread count: 0
Background Thread count: 0
Background Thread count: 1
Foreground Thread count: 1
Background Thread count: 2
Foreground Thread count: 2
Background Thread count: 3
Foreground Thread count: 3
Background Thread count: 4
Foreground Thread count: 4
Background Thread count: 5
Foreground Thread finished counting.

 */

(4)Invoke( )

詳見本文作者發(fā)表的其他文章,C#用Parallel.Invoke 方法盡可能并行執(zhí)行提供的每個(gè)操作

2.CreateGraphics()

3.DateTime.Now 

4.(MethodInvoker)delegate ()

5.TimeSpan(Int32, Int32, Int32)

將 TimeSpan 結(jié)構(gòu)的新實(shí)例初始化為指定的小時(shí)數(shù)、分鐘數(shù)和秒數(shù)。

(1)定義

public TimeSpan (int hours, int minutes, int seconds);

參數(shù)
hours    Int32
小時(shí)數(shù)。

minutes    Int32
分鐘數(shù)。

seconds    Int32
秒數(shù)。

例外
ArgumentOutOfRangeException
參數(shù)指定小于 TimeSpanTimeSpan.MinValue 或大于 TimeSpan.MaxValue 的值。

注解
將指定的 hours、 minutes和 seconds 轉(zhuǎn)換為時(shí)鐘周期,該值初始化此實(shí)例。

(2)示例

// Example of the TimeSpan( int, int, int ) constructor.
// 創(chuàng)建多個(gè) TimeSpan 對象,該重載將 初始化 TimeSpan 為指定的小時(shí)數(shù)、分鐘數(shù)和秒數(shù)。
namespace ConsoleApp14
{
    class TimeSpanCtorIIIDemo
    {
        static void CreateTimeSpan(int hours, int minutes,int seconds)
        {
            TimeSpan elapsedTime = new(hours, minutes, seconds);
            string ctor = string.Format("TimeSpan( {0}, {1}, {2} )",hours, minutes, seconds); 
            Console.WriteLine("{0,-37}{1,16}",ctor, elapsedTime.ToString());//輸出格式
        }

        static void Main()
        {
            Console.WriteLine(
                "This example of the TimeSpan( int, int, int ) " +
                "\nconstructor generates the following output.\n");
            Console.WriteLine("{0,-37}{1,16}", "Constructor", "Value");  //{0,-37}代表左對齊長度不足時(shí)空格抵
            Console.WriteLine("{0,-37}{1,16}", "-----------", "-----");     //{1,16}代表右對齊,長度不足時(shí)空格抵

            CreateTimeSpan(10, 20, 30);
            CreateTimeSpan(-10, 20, 30);
            CreateTimeSpan(0, 0, 37230);
            CreateTimeSpan(1000, 2000, 3000);
            CreateTimeSpan(1000, -2000, -3000);
            CreateTimeSpan(999999, 999999, 999999);
        }
    }
}
/*
This example of the TimeSpan( int, int, int )
constructor generates the following output.

Constructor                                     Value
-----------                                     -----
TimeSpan( 10, 20, 30 )                       10:20:30
TimeSpan( -10, 20, 30 )                     -09:39:30
TimeSpan( 0, 0, 37230 )                      10:20:30
TimeSpan( 1000, 2000, 3000 )              43.02:10:00
TimeSpan( 1000, -2000, -3000 )            40.05:50:00
TimeSpan( 999999, 999999, 999999 )     42372.15:25:39
*/

 到此這篇關(guān)于C#使用DateTime.Now靜態(tài)屬性動態(tài)獲得系統(tǒng)當(dāng)前日期和時(shí)間的文章就介紹到這了,更多相關(guān)C# DateTime.Now動態(tài)獲得日期內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • C#生成隨機(jī)ArrayList的方法

    C#生成隨機(jī)ArrayList的方法

    這篇文章主要介紹了C#生成隨機(jī)ArrayList的方法,實(shí)例分析了C#中ArrayList的相關(guān)操作技巧,需要的朋友可以參考下
    2015-06-06
  • C#操作RabbitMQ的完整實(shí)例

    C#操作RabbitMQ的完整實(shí)例

    這篇文章主要為大家詳細(xì)介紹了C#操作RabbitMQ的完整實(shí)例,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2017-08-08
  • C#?線程切換后上下文都去了哪里(.NET高級調(diào)試分析)

    C#?線程切換后上下文都去了哪里(.NET高級調(diào)試分析)

    總會有一些朋友問一個(gè)問題,在 Windows 中線程做了上下文切換,請問被切的線程他的寄存器上下文都去了哪里?這個(gè)問題其實(shí)比較底層,如果對操作系統(tǒng)沒有個(gè)體系層面的理解以及做過源碼分析,其實(shí)很難說明白,這篇我們就從.NET高級調(diào)試的角度分析,需要的朋友可以參考下
    2023-12-12
  • C#實(shí)現(xiàn)公式計(jì)算驗(yàn)證碼的示例詳解

    C#實(shí)現(xiàn)公式計(jì)算驗(yàn)證碼的示例詳解

    現(xiàn)在很多的平臺已經(jīng)不使用普通的數(shù)字、字母等驗(yàn)證碼了,取而代之的是拼圖類、選圖類、旋轉(zhuǎn)類或者計(jì)算類的驗(yàn)證碼。本文將利用C#實(shí)現(xiàn)一個(gè)公式計(jì)算驗(yàn)證碼,感興趣的可以了解一下
    2022-10-10
  • 探秘C# 6.0 的新特性

    探秘C# 6.0 的新特性

    本文的內(nèi)容包括引入C#6.0中的新的語言特性有哪些. 還有已經(jīng)被引入的代碼名稱為 “Roslyn”新編譯器. 編譯器是開放源碼的,并且可以從 codeplex 網(wǎng)站的這個(gè)地址下載到源代碼:https://roslyn.codeplex.com/.
    2015-03-03
  • C#中的9個(gè)“黑魔法”

    C#中的9個(gè)“黑魔法”

    這篇文章主要介紹了C#中的9個(gè)“黑魔法”與“騷操作”,本文通過實(shí)例代碼給大家講解的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2020-04-04
  • C#序列化與反序列化集合對象并進(jìn)行版本控制

    C#序列化與反序列化集合對象并進(jìn)行版本控制

    這篇文章介紹了C#序列化與反序列化集合對象并實(shí)現(xiàn)版本控制的方法,文中通過示例代碼介紹的非常詳細(xì)。對大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2022-09-09
  • C#異常處理總結(jié)及簡單實(shí)例

    C#異常處理總結(jié)及簡單實(shí)例

    這篇文章主要介紹了C#異常處理總結(jié)及簡單實(shí)例的相關(guān)資料,需要的朋友可以參考下
    2017-06-06
  • 基于C#實(shí)現(xiàn)文檔打印功能

    基于C#實(shí)現(xiàn)文檔打印功能

    在軟件開發(fā)過程中,文檔打印是一個(gè)常見的功能需求,本文將詳細(xì)介紹如何在C#中實(shí)現(xiàn)文檔打印,并通過代碼示例介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作有一定幫助,需要的朋友可以參考下
    2024-10-10
  • DevExpress實(shí)現(xiàn)禁用TreeListNode CheckBox的方法

    DevExpress實(shí)現(xiàn)禁用TreeListNode CheckBox的方法

    這篇文章主要介紹了DevExpress實(shí)現(xiàn)禁用TreeListNode CheckBox的方法,在項(xiàng)目開發(fā)中有應(yīng)用價(jià)值,需要的朋友可以參考下
    2014-08-08

最新評論