欧美大屁股bbbbxxxx,狼人大香伊蕉国产www亚洲,男ji大巴进入女人的视频小说,男人把ji大巴放进女人免费视频,免费情侣作爱视频

歡迎來到入門教程網(wǎng)!

C#教程

當(dāng)前位置:主頁 > 軟件編程 > C#教程 >

C#實(shí)現(xiàn)的Win32控制臺(tái)線程計(jì)時(shí)器功能示例

來源:本站原創(chuàng)|時(shí)間:2020-01-10|欄目:C#教程|點(diǎn)擊: 次

本文實(shí)例講述了C#實(shí)現(xiàn)的Win32控制臺(tái)線程計(jì)時(shí)器功能。分享給大家供大家參考,具體如下:

在C#中提供了三種類型的計(jì)時(shí)器:

1、基于 Windows 的標(biāo)準(zhǔn)計(jì)時(shí)器(System.Windows.Forms.Timer)
2、基于服務(wù)器的計(jì)時(shí)器(System.Timers.Timer)
3、線程計(jì)時(shí)器(System.Threading.Timer)

一、基于 Windows 的標(biāo)準(zhǔn)計(jì)時(shí)器(System.Windows.Forms.Timer)

首先注意一點(diǎn)就是:Windows 計(jì)時(shí)器是為單線程環(huán)境設(shè)計(jì)的
此計(jì)時(shí)器從Visual Basic 1.0 版起就存在于該產(chǎn)品中,并且基本上未做改動(dòng)

這個(gè)計(jì)時(shí)器是使用最簡(jiǎn)單的一種,只要把工具箱中的Timer控件拖到窗體上,然后設(shè)置一下事件和間隔時(shí)間等屬性就可以了

二、基于服務(wù)器的計(jì)時(shí)器(System.Timers.Timer)

System.Timers.Timer不依賴窗體,是從線程池喚醒線程,是傳統(tǒng)的計(jì)時(shí)器為了在服務(wù)器環(huán)境上運(yùn)行而優(yōu)化后的更新版本,在VS2008的工具箱中沒有提供現(xiàn)成的控件,需要手工編碼使用此計(jì)時(shí)器

三、線程計(jì)時(shí)器(System.Threading.Timer)

線程計(jì)時(shí)器也不依賴窗體,是一種簡(jiǎn)單的、輕量級(jí)計(jì)時(shí)器,它使用回調(diào)方法而不是使用事件,并由線程池線程提供支持。對(duì)消息不在線程上發(fā)送的方案中,線程計(jì)時(shí)器是非常有用的。

這里只針對(duì)控制臺(tái)線程計(jì)時(shí)器的使用給出代碼示例,后續(xù)會(huì)給出其它幾種的代碼示例:

using System;
using System.Collections.Generic;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading;
/************************************************************************/
/* CSharp控制臺(tái)線程計(jì)時(shí)器代碼示例
 * Powered by:testcs_dn
 * Blog:http://blog.csdn.net/testcs_dn
 */
/************************************************************************/
namespace CSharp控制臺(tái)線程計(jì)時(shí)器代碼示例
{
  /// <summary>
  /// 控制臺(tái)線程計(jì)時(shí)器代碼示例
  /// 這里展示了Windows API SetConsoleCtrlHandler函數(shù)的應(yīng)用,同時(shí)展示了線程計(jì)時(shí)器的使用;
  /// Author:testcs_dn
  /// Date:2015-01-03
  /// </summary>
  class Program
  {
    /// <summary>
    /// 計(jì)時(shí)器回調(diào)函數(shù),在這里處理計(jì)時(shí)時(shí)間是否到達(dá)的判斷以及要做的事情;
    /// </summary>
    /// <param name="obj"></param>
    public static void workOvertimeTimerCallback(object obj)
    {
      DateTime dt = DateTime.Now;
      if (dt.Hour == 16 && dt.Minute > 0)
      {
        Console.WriteLine("ok");
      }
    }
    //計(jì)時(shí)器變量
    public static System.Threading.Timer workOvertimeTimer = null;
    //定義處理程序委托
    delegate bool ConsoleCtrlDelegate(int dwCtrlType);
    const int CTRL_CLOSE_EVENT = 2;
    //導(dǎo)入SetCtrlHandlerHandler API
    [DllImport("kernel32.dll")]
    private static extern bool SetConsoleCtrlHandler(ConsoleCtrlDelegate HandlerRoutine, bool Add);
    static void Main(string[] args)
    {
      ConsoleCtrlDelegate newDelegate = new ConsoleCtrlDelegate(HandlerRoutine);
      if (SetConsoleCtrlHandler(newDelegate, true))
      {
        //初始化計(jì)時(shí)器
        workOvertimeTimer = new System.Threading.Timer(new TimerCallback(workOvertimeTimerCallback), null, 1000, 10000);
        //這里執(zhí)行你自己的任務(wù),我舉例輸出“...”,為了展示長(zhǎng)時(shí)間的任務(wù),我用了一個(gè)死循環(huán);
        //避免輸出太多,使用了Sleep;
        //注意:Sleep的時(shí)間不可太長(zhǎng),否則可能影響Console.ReadKey(),導(dǎo)致不能接收用戶輸入;
        while (true)
        {
          Console.WriteLine("...");
          Thread.Sleep(100);
        }
      }
      else
      {
        Console.WriteLine("抱歉,API注入失敗,按任意鍵退出!");
        Console.ReadKey();
      }
    }
    /// <summary>
    /// 處理程序例程,在這里編寫對(duì)指定事件的處理程序代碼
    /// </summary>
    /// <param name="CtrlType"></param>
    /// <returns></returns>
    static bool HandlerRoutine(int CtrlType)
    {
      switch (CtrlType)
      {
        case CTRL_CLOSE_EVENT:    //用戶要關(guān)閉Console了
          Console.WriteLine();
          Console.WriteLine("任務(wù)還沒有完成,確認(rèn)要退出嗎?(Y/N)");
          ConsoleKeyInfo ki = Console.ReadKey();
          return ki.Key == ConsoleKey.Y;
        default:
          return true;
      }
    }
  }
}

更多關(guān)于C#相關(guān)內(nèi)容感興趣的讀者可查看本站專題:《C#字符串操作技巧總結(jié)》、《C#數(shù)組操作技巧總結(jié)》、《C#中XML文件操作技巧匯總》、《C#常見控件用法教程》、《C#程序設(shè)計(jì)之線程使用技巧總結(jié)》、《C#操作Excel技巧總結(jié)》、《WinForm控件用法總結(jié)》、《C#數(shù)據(jù)結(jié)構(gòu)與算法教程》及《C#面向?qū)ο蟪绦蛟O(shè)計(jì)入門教程》

希望本文所述對(duì)大家C#程序設(shè)計(jì)有所幫助。

網(wǎng)頁制作CMS教程網(wǎng)絡(luò)編程軟件編程腳本語言數(shù)據(jù)庫服務(wù)器

如果侵犯了您的權(quán)利,請(qǐng)與我們聯(lián)系,我們將在24小時(shí)內(nèi)進(jìn)行處理、任何非本站因素導(dǎo)致的法律后果,本站均不負(fù)任何責(zé)任。

聯(lián)系QQ:835971066 | 郵箱:835971066#qq.com(#換成@)

Copyright © 2002-2020 腳本教程網(wǎng) 版權(quán)所有