自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

探索.NET中的定時(shí)器:選擇最適合你的應(yīng)用場(chǎng)景

開發(fā) 后端
今天看到一網(wǎng)友聊.NET中的定時(shí)器,我也把我知道和大家分享一下。 在.NET中,有多種定時(shí)器的實(shí)現(xiàn),每一種都有其特定的應(yīng)用場(chǎng)景和特點(diǎn)。下面將分別介紹這幾種定時(shí)器,并提供相應(yīng)的實(shí)例源代碼。

概述:.NET提供多種定時(shí)器,如System.Windows.Forms.Timer適用于UI,System.Web.UI.Timer用于Web,System.Diagnostics.Timer用于性能監(jiān)控,System.Threading.Timer和System.Timers.Timer用于一般定時(shí)任務(wù)。在.NET 6及以上,還有更直觀的System.Threading.PeriodicTimer。選擇合適的定時(shí)器,提升應(yīng)用性能和用戶體驗(yàn)。

今天看到一網(wǎng)友聊.net中的定時(shí)器,我也把我知道和大家分享一下。 在.NET中,有多種定時(shí)器的實(shí)現(xiàn),每一種都有其特定的應(yīng)用場(chǎng)景和特點(diǎn)。下面將分別介紹這幾種定時(shí)器,并提供相應(yīng)的實(shí)例源代碼。

1. System.Windows.Forms.Timer

應(yīng)用場(chǎng)景: 適用于Windows Forms應(yīng)用程序中需要與UI線程交互的場(chǎng)景。

特點(diǎn): 在UI線程中工作,可以直接訪問(wèn)和操作UI控件。

實(shí)例:

using System;
using System.Windows.Forms;

public class MainForm : Form
{
    private Timer timer;

    public MainForm()
    {
        // 創(chuàng)建定時(shí)器,每秒觸發(fā)一次
        timer = new Timer();
        timer.Interval = 1000;
        timer.Tick += TimerTick;

        // 啟動(dòng)定時(shí)器
        timer.Start();
    }

    private void TimerTick(object sender, EventArgs e)
    {
        // 在UI線程中更新UI
        label1.Text = "定時(shí)器觸發(fā)時(shí)間:" + DateTime.Now;
    }

    static void Main()
    {
        Application.Run(new MainForm());
    }
}

2. System.Windows.Threading.DispatcherTimer

應(yīng)用場(chǎng)景: 適用于WPF應(yīng)用程序中需要與UI線程交互的場(chǎng)景。

特點(diǎn): 基于WPF的 Dispatcher,可直接訪問(wèn)和操作UI控件。

實(shí)例:

using System;
using System.Windows;
using System.Windows.Threading;

public partial class MainWindow : Window
{
    private DispatcherTimer timer;

    public MainWindow()
    {
        // 創(chuàng)建定時(shí)器,每秒觸發(fā)一次
        timer = new DispatcherTimer();
        timer.Interval = TimeSpan.FromSeconds(1);
        timer.Tick += TimerTick;

        // 啟動(dòng)定時(shí)器
        timer.Start();
    }

    private void TimerTick(object sender, EventArgs e)
    {
        // 在UI線程中更新UI
        label1.Content = "定時(shí)器觸發(fā)時(shí)間:" + DateTime.Now.ToString("HH:mm:ss");
    }

    static void Main()
    {
        Application app = new Application();
        app.Run(new MainWindow());
    }
}

3. System.Web.UI.Timer

應(yīng)用場(chǎng)景: 適用于ASP.NET Web應(yīng)用程序中,用于在Web頁(yè)面上執(zhí)行定時(shí)操作。

特點(diǎn): 在Web頁(yè)面的服務(wù)器端運(yùn)行,適用于Web Forms。

實(shí)例:

ASP.NET Web Forms中在aspx頁(yè)面的代碼:

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
        <asp:ScriptManager runat="server" />
        <asp:UpdatePanel runat="server">
            <ContentTemplate>
                <asp:Label ID="Label1" runat="server" Text="定時(shí)器觸發(fā)時(shí)間:" />
                <asp:Timer ID="Timer1" runat="server" Interval="1000" OnTick="TimerTick"></asp:Timer>
            </ContentTemplate>
        </asp:UpdatePanel>
    </form>
</body>
</html>

對(duì)應(yīng)的代碼文件 Default.aspx.cs

using System;

public partial class _Default : System.Web.UI.Page
{
    protected void TimerTick(object sender, EventArgs e)
    {
        // 在服務(wù)器端更新UI
        Label1.Text = "定時(shí)器觸發(fā)時(shí)間:" + DateTime.Now.ToString("HH:mm:ss");
    }
}

4. System.Diagnostics.Timer

應(yīng)用場(chǎng)景: 適用于性能計(jì)數(shù)器的定時(shí)器,用于性能監(jiān)控和測(cè)量。

特點(diǎn): 基于性能計(jì)數(shù)器的定時(shí)器。

實(shí)例:

using System;
using System.Diagnostics;

class Program
{
    static void Main()
    {
        // 創(chuàng)建定時(shí)器,每秒觸發(fā)一次
        Timer timer = new Timer(1000);
        
        // 定時(shí)器觸發(fā)事件
        timer.Elapsed += TimerElapsed;

        // 啟動(dòng)定時(shí)器
        timer.Start();

        // 阻止程序退出
        Console.ReadLine();
    }

    static void TimerElapsed(object sender, ElapsedEventArgs e)
    {
        Console.WriteLine("定時(shí)器觸發(fā)時(shí)間:" + e.SignalTime);
    }
}

5. System.Timers.Timer

應(yīng)用場(chǎng)景: 適用于需要在一定時(shí)間間隔內(nèi)重復(fù)執(zhí)行任務(wù)的場(chǎng)景,如定時(shí)數(shù)據(jù)采集、日志記錄等。

特點(diǎn): 基于事件觸發(fā)機(jī)制,可在多線程環(huán)境中使用,但注意處理線程同步。

實(shí)例:

using System;
using System.Timers;

class Program
{
    static void Main()
    {
        // 創(chuàng)建定時(shí)器,每秒觸發(fā)一次
        Timer timer = new Timer(1000);

        // 定時(shí)器觸發(fā)事件
        timer.Elapsed += TimerElapsed;

        // 啟動(dòng)定時(shí)器
        timer.Start();

        // 阻止程序退出
        Console.ReadLine();
    }

    static void TimerElapsed(object sender, ElapsedEventArgs e)
    {
        Console.WriteLine("定時(shí)器觸發(fā)時(shí)間:" + e.SignalTime);
    }
}

6. System.Threading.Timer

應(yīng)用場(chǎng)景: 適用于需要在一定時(shí)間間隔內(nèi)執(zhí)行任務(wù),但不需要與UI線程交互的場(chǎng)景,如后臺(tái)任務(wù)的調(diào)度。

特點(diǎn): 基于線程池,不依賴于UI線程,需要注意線程同步和異常處理。

實(shí)例:

using System;
using System.Threading;

class Program
{
    static void Main()
    {
        // 創(chuàng)建定時(shí)器,每秒觸發(fā)一次
        Timer timer = new Timer(TimerCallback, null, 0, 1000);

        // 阻止程序退出
        Console.ReadLine();
    }

    static void TimerCallback(object state)
    {
        Console.WriteLine("定時(shí)器觸發(fā)時(shí)間:" + DateTime.Now);
    }
}

7. System.Threading.PeriodicTimer (需要.NET 6及以上版本)

應(yīng)用場(chǎng)景: 適用于需要定期執(zhí)行任務(wù)的場(chǎng)景,替代 System.Threading.Timer。

特點(diǎn): .NET 6及以上版本引入的新型定時(shí)器,提供更直觀的API和更穩(wěn)定的性能。

實(shí)例:

using System;
using System.Threading;

class Program
{
    static void Main()
    {
        // 創(chuàng)建定時(shí)器,每秒觸發(fā)一次
        using (PeriodicTimer timer = new PeriodicTimer(TimeSpan.FromSeconds(1)))
        {
            // 定時(shí)器觸發(fā)事件
            timer.Elapsed += TimerElapsed;

            // 啟動(dòng)定時(shí)器
            timer.Start();

            // 阻止程序退出
            Console.ReadLine();
        }
    }

    static void TimerElapsed(object sender, EventArgs e)
    {
        Console.WriteLine("定時(shí)器觸發(fā)時(shí)間:" + DateTime.Now);
    }
}

這些定時(shí)器各自適用于不同的場(chǎng)景,選擇合適的定時(shí)器取決于你的具體需求和應(yīng)用程序類型。

在使用定時(shí)器時(shí),請(qǐng)注意處理好線程同步、資源釋放等問(wèn)題,以確保應(yīng)用程序的穩(wěn)定性和性能。

責(zé)任編輯:姜華 來(lái)源: 今日頭條
相關(guān)推薦

2021-02-14 10:09:04

數(shù)據(jù)目錄數(shù)據(jù)元數(shù)據(jù)

2023-10-30 18:40:55

LVS負(fù)載均衡

2011-09-29 13:32:45

iPhone閱讀

2011-02-21 14:35:13

瀏覽器Chrome火狐

2019-03-10 22:21:47

框架AI開發(fā)

2016-07-14 16:27:54

linux

2011-08-01 09:57:14

Linux發(fā)行版

2020-09-21 09:53:49

編程開發(fā)職務(wù)

2018-09-07 06:30:50

物聯(lián)網(wǎng)平臺(tái)物聯(lián)網(wǎng)IOT

2012-03-20 09:32:24

Linux服務(wù)器

2023-12-06 07:40:53

MySQL時(shí)間類型

2012-12-13 21:50:43

2012-09-26 09:37:50

2017-09-01 11:59:59

Android

2016-01-26 09:58:28

云存儲(chǔ)云服務(wù)云安全

2020-03-17 15:55:12

Redis數(shù)據(jù)庫(kù)命令

2015-09-23 13:28:01

大數(shù)據(jù)分析軟件

2018-07-16 08:50:31

固態(tài)硬盤內(nèi)存

2022-12-26 14:51:48

人工智能

2011-12-05 14:28:07

商用筆記本
點(diǎn)贊
收藏

51CTO技術(shù)棧公眾號(hào)