導航:首頁 > 使用方法 > ctimer使用方法

ctimer使用方法

發布時間:2022-05-05 17:03:36

① timer怎麼

Timer 控制項方案:響應 Timer 事件
經歷了 Timer 控制項的時間間隔後,Visual Basic 將生成 Timer 事件。通常,在響應此事件時將檢查某些一般條件,例如系統時鍾。
數字型時鍾是涉及 Timer 控制項的簡單而有用的應用程序。一旦理解此應用程序的工作方式,就可增強它的性能,實現鬧鍾、跑表或其它定時設備的功能。
Digital Clock 應用程序包含一個定時器和一個有邊框的標簽。

定時器在運行時不可見。下表列出了 Digital Clock 應用程序中的屬性設置值。

控制項 屬性 設置值
Label1 BorderStyle Fixed Single
Timer1 Interval 500(半秒)
Timer1 Enabled True

應用程序中的唯一過程是定時器的事件過程:

Private Sub Timer1_Timer ()
If lblTime.Caption <> CStr(Time) Then
lblTime.Caption = Time
End If
End Sub

過程調用內在的 Time 函數來顯示系統時間。此函數返回一個 Variant,以日期/時間數值 (VarType 7) 的形式包含當前時間。將它賦予某個字元串變數或屬性時,比如賦予本例中的 Caption 屬性時,Visual Basic 將用「控制面板」中指定的格式將其轉換成一個字元串。若要用其它格式顯示,可使用 Format 函數。
詳細信息 請參閱「Format 屬性」。
將定時器的 Interval 屬性設置為 500,按照上述原則將 Interval 設置為要區分的最短時間(本例中為一秒鍾)的一半。這會使 Timer 控制項代碼每半秒更新一次標簽。這樣做不僅浪費而且可能導致視覺顫動,因此代碼在改變標題之前要檢查當前時間是否與在標簽上顯示的時間不同。
無需另外編寫語句就可自定義數字時鍾的外觀。例如,可為標簽選擇其它字體或改變窗體的 BorderStyle 屬性。

② java 中timer類的用法是什麼

現在項目中用到需要定時去檢查文件是否更新的功能。timer正好用於此處。
用法很簡單,new一個timer,然後寫一個timertask的子類即可。
package comz.autoupdatefile;

import java.util.Timer;
import java.util.TimerTask;

public class M {
public static void main(String[] args) {
// TODO todo.generated by zoer
Timer timer = new Timer();
timer.schele(new MyTask(), 1000, 2000);
}
}

class MyTask extends TimerTask {

@Override
public void run() {
System.out.println("dddd");

}

}
這樣,就可以在1秒鍾之後開始執行mytask,每兩秒鍾執行一次。
當然,timer的功能也可以通過自己構造線程,然後在線程中用sleep來模擬停止一段時間,然後再執行某個動作。
其實,看一下timertask的源碼就立即可以知道,timertask就是實現了runnable介面的。也就是說,通過timer來間隔一段時間執行一個操作,也是通過一個線程來做到的。

③ timer控制項用法

Timer控制項
Timer控制項主要會用到2個屬性一個是Enabled和Interval
Enabled主要是控制當前Timer控制項是否可用
timer1.Enabled=false;不可用
timer1.Enabled=true;可用
timer1.Interval=1000;主要是設置timer2_Tick事件的時間,單位為毫秒
例一:到9:00提示去上廁所:(
把timer2.Interval=60000;//1分鍾
private void timer2_Tick(object sender, System.EventArgs e){
string cesuotime=DateTime.Now.DateTime.Now.ToShortTimeString();//得到現在的時間
if(cesuotime.equles("9:00")){
timer1.Enabled=false;
MessageBox.show("該去上廁所了");
timer1.Enabled=true;//如果不先把enabled設置成false對話框會一直彈下去
}
}
例二:每2小時提示用戶看電腦時間已經很久了,需要休息了
把timer2.Interval=7200000;//7200秒
private void timer2_Tick(object sender, System.EventArgs e){
timer1.Enabled=false;
MessageBox.show("需要休息了,開機已經2小時了");
timer1.Enabled=true;//如果不先把enabled設置成false對話框會一直彈下去
}
}

④ C# timer如何使用

privatevoidtimer1_Tick(objectsender,EventArgse)
{
if(textBox1.Text=="")
textBox1.Text="HelloWorld";
else
textBox1.Text="";
}

privatevoidbutton1_Click(objectsender,EventArgse)
{
if(!timer1.Enabled)
{
timer1.Start();
timer1.Enabled=true;
}
else
{
timer1.Enabled=false;
timer1.Stop();
}

⑤ timer的用法

public Form1()
{
InitializeComponent();
}
這句不用理會
this.timer1.Interval = 1000;
設置timer1對象的事件觸發間隔。
this.timer1.Tick += new System.EventHandler(this.timer1_Tick);
為timer1對象綁定事件,該處應用委託的方式,委託的定義網路一下就知道了。

private void timer1_Tick(object sender, EventArgs e)
{
if (time > -1)
{
time--;
this.label1.Text = time.ToString();
//this.label1.Refresh();
}
else
{
timer1.Stop();

}

}
上述委託方法的實現,原意讓label1現實倒計時,由10遞減,沒見個1秒刷新顯示。

⑥ NET中的三種Timer的區別和用法

在.NET1.1裡面,第3個System.Timers.Timer,也是可以拖拽使用,而.NET2.0開始取消了,只能手動編寫代碼。而後2個沒有限制制。操作方法如下:

1、首先使用RadTileList控制項,TileRows="3" 三行,SelectionMode="Multiple" 多選。

⑦ c#如何使用timer

可以在窗體添加一個timer控制項,或者自定義timer也可以
例如
Timer procTimer = new Timer();
然後注冊timer事件
procTimer .Tick += new EventHandler(procTimer _Tick);
定義timer函數
void procTimer _Tick(object sender, EventArgs e)
{
。。。。。。
}

設置timer計數間隔
procTimer.Interval = 100;//默認100毫秒
啟動timer
.procTimer .Start();

⑧ 新手問下timer怎麼調用

C#中Timer組件用法 Timer組件是也是一個WinForm組件了,和其他的WinForm組件的最大區別是:Timer組件是不可見的,而其他大部分的組件都是都是可見的,可以設計的。Timer組件也被封裝在名稱空間System.Windows.Forms中,其主要作用是當Timer組件啟動後,每隔一個固定時間段,觸發相同的事件。Timer組件在程序設計中是一個比較常用的組件,雖然屬性、事件都很少,但在有些地方使用它會產生意想不到的效果。

其實要使得程序的窗體飄動起來,其實思路是比較簡單的。首先是當載入窗體的時候,給窗體設定一個顯示的初始位置。然後通過在窗體中定義的二個Timer組件,其中一個叫Timer1,其作用是控制窗體從左往右飄動(當然如果你願意,你也可以改為從上往下飄動,或者其他的飄動方式。),另外一個Timer2是控制窗體從右往左飄動(同樣你也可以改為其他飄動方式)。當然這二個Timer組件不能同時啟動,在本文的程序中,是先設定Timer1組件啟動的,當此Timer1啟動後,每隔0.01秒,都會在觸發的事件中給窗體的左上角的橫坐標都加上"1",這時我們看到的結果是窗體從左往右不斷移動,當移動到一定的位置後,Timer1停止。Timer2啟動,每隔0.01秒,在觸發定義的事件中給窗體的左上角的橫坐標都減去"1",這時我們看到的結果是窗體從右往左不斷移動。當移動到一定位置後,Timer1啟動,Timer2停止,如此反覆,這樣窗體也就飄動起來了。要實現上述思路,必須解決好以下問題。

(1).如何設定窗體的初始位置:

設定窗體的初始位置,是在事件Form1_Load()中進行的。此事件是當窗體載入的時候觸發的。Form有一個DesktopLocation屬性,這個屬性是設定窗體的左上角的二維位置。在程序中是通過Point結構變數來設定此屬性的值,具體如下:

//設定窗體起初飄動的位置,位置為屏幕的坐標的(0,240)
private void Form1_Load ( object sender , System.EventArgs e )
{
Point p = new Point ( 0 , 240 ) ;
this.DesktopLocation = p ;
}

(2). 如何實現窗體從左往右飄動:

設定Timer1的Interval值為"10",就是當Timer1啟動後,每隔0.01秒觸發的事件是Timer1_Tick(),在這個事件中編寫給窗體左上角的橫坐標不斷加"1"的代碼,就可以了,具體如下:

private void timer1_Tick(object sender, System.EventArgs e)
{
{ //窗體的左上角橫坐標隨著timer1不斷加一
Point p = new Point ( this.DesktopLocation.X + 1 , this.DesktopLocation.Y ) ;
this.DesktopLocation = p ;
if ( p.X == 550 )
{
timer1.Enabled = false ;
timer2.Enabled = true ;
}
}

(3). 如何實現窗體從右往左飄動:

代碼設計和從左往右飄動差不多,主要的區別是減"1"而不是加"1"了,具體如下:

//當窗體左上角位置的橫坐標為-150時,timer2停止,timer1啟動
private void timer2_Tick(object sender, System.EventArgs e)
{ file://窗體的左上角橫坐標隨著timer2不斷減一
Point p = new Point ( this.DesktopLocation.X - 1 , this.DesktopLocation.Y ) ;
this.DesktopLocation = p ;
if ( p.X == - 150 )
{
timer1.Enabled = true ;
timer2.Enabled = false ;
}
}
三. 用Visual C#編寫窗體飄動程序的源代碼:

通過上面的介紹,不難寫出窗體飄動的程序源代碼。如下:

using System ;
using System.Drawing ;
using System.Collections ;
using System.ComponentModel ;
using System.Windows.Forms ;
using System.Data ;
namespace floatingForm
{
public class Form1 : Form
{
private Timer timer1 ;
private Timer timer2 ;
private Label label1 ;
private Button button1 ;
private System.ComponentModel.IContainer components ;
public Form1 ( )
{
file://初始化窗體中的各個組件
InitializeComponent ( ) ;
}
file://清除在程序中使用過的資源
protected override void Dispose ( bool disposing )
{
if ( disposing )
{
if ( components != null )
{
components.Dispose ( ) ;
}
}
base.Dispose( disposing ) ;
}
private void InitializeComponent ( )
{
this.components = new System.ComponentModel.Container ( ) ;
this.timer1 = new Timer ( this.components ) ;
this.timer2 = new Timer ( this.components ) ;
this.label1 = new Label ( ) ;
this.button1 = new Button ( ) ;
this.SuspendLayout ( ) ;

this.timer1.Enabled = true ;
this.timer1.Interval = 10 ;
this.timer1.Tick += new System.EventHandler ( this.timer1_Tick ) ;

this.timer2.Enabled = false ;
this.timer2.Interval = 10 ;
this.timer2.Tick += new System.EventHandler ( this.timer2_Tick ) ;

this.button1.Font = new Font ( "宋體" , 10 ) ;
this.button1.Location = new Point ( 1 , 8 ) ;
this.button1.Name = "button1" ;
this.button1.Size = new Size ( 80 , 25 ) ;
this.button1.TabIndex = 0 ;
this.button1.Text = "停止飄動" ;
this.button1.Click += new System.EventHandler ( this.button1_Click ) ;

this.label1.Font = new Font ( "宋體" , 22F , FontStyle.Bold , GraphicsUnit.Point , ( ( System.Byte ) ( 0 ) ) ) ;
this.label1.Location = new Point ( 8 , 38 ) ;
this.label1.Name = "label1" ;
this.label1.Size = new Size ( 344 , 40 ) ;
this.label1.TabIndex = 1 ;
this.label1.Text = "用Visual C#做的飄動的窗體!" ;

this.AutoScaleBaseSize = new Size ( 5 , 13 ) ;
this.ClientSize = new Size ( 352 , 70 ) ;
this.Controls.Add (this.label1 ) ;
this.Controls.Add (this.button1 ) ;
this.Name = "Form1" ;
this.Text = "用Visual C#做的飄動的窗體!";
this.Load += new System.EventHandler ( this.Form1_Load ) ;
this.ResumeLayout ( false ) ;
}
static void Main ( )
{
Application.Run ( new Form1 ( ) ) ;
}
file://設定窗體起初飄動的位置
private void Form1_Load ( object sender , System.EventArgs e )
{
Point p = new Point ( 0 , 240 ) ;
this.DesktopLocation = p ;
}
file://當窗體左上角位置的橫坐標為550時,timer1停止,timer2啟動
private void timer1_Tick(object sender, System.EventArgs e)
{
file://窗體的左上角橫坐標隨著timer1不斷加一
Point p = new Point ( this.DesktopLocation.X + 1 , this.DesktopLocation.Y ) ;
this.DesktopLocation = p ;
if ( p.X == 550 )
{
timer1.Enabled = false ;
timer2.Enabled = true ;
}
}
file://當窗體左上角位置的橫坐標為-150時,timer2停止,timer1啟動
private void timer2_Tick(object sender, System.EventArgs e)
{ file://窗體的左上角橫坐標隨著timer2不斷減一
Point p = new Point ( this.DesktopLocation.X - 1 , this.DesktopLocation.Y ) ;
this.DesktopLocation = p ;
if ( p.X == - 150 )
{
timer1.Enabled = true ;
timer2.Enabled = false ;
}
}
file://停止所有的timer
private void button1_Click(object sender, System.EventArgs e)
{
timer1.Stop ( ) ;
timer2.Stop ( ) ;
}
}
}

四. 總結:

恰到好處的使用Timer組件往往會有出其不意的效果。由於本文的主要目的是介紹Timer組件的使用方法,程序功能還不是十分強大,感興趣的讀者,可以試著按照下面的思路進行修改,看看是否可以讓窗體上下飄動,讓窗體不定規則的飄動。當然如果你更有興趣,也可以把窗體的邊框和最大化、最小化等按鈕給隱去,放一個好看的圖片充滿整個窗體,再讓他飄動起來,這樣效果就更令人驚訝了。

⑨ vb中timer怎麼用

簡單的說Timer是一個時間的控制項,不能說Timer的值等於2秒時,因為Timer相當於時間,只能說當Timer跳動兩秒時觸發某事件。

例如:

1、在窗體上添加一個timer控制項,默認名稱為Timer1。

2、將Timer1的Interval屬性設置為1000,即每隔1000毫秒(1秒)觸發一次。

3、在Timer1的Timer()事件中編寫如下代碼

PrivateSub Timer1_Timer()

Print Time

EndSub

4、運行該程序,窗體上每隔1秒就會輸出一次系統當前時間。

(9)ctimer使用方法擴展閱讀:

具體代碼如下:

首先要確定啟動控制項

Me.Timer1.Enabled = True』啟動控制項

Me.Timer1.Interval = 1000『設定跳動頻為1秒。1000=1秒

dimsTime as string=0

然後在控制項的Timer1.Tick事件下寫下面的代碼

sTime=stime+1

if sTime=2 then

'你所要觸發事件的代碼

msgbox("OK")

endif

參考資料:網路—C語言

閱讀全文

與ctimer使用方法相關的資料

熱點內容
酷派感應在哪裡設置方法 瀏覽:148
手機uc瀏覽器版本查看方法 瀏覽:284
研究中國股市的有效性問題的方法 瀏覽:658
天然氣洗澡的使用方法 瀏覽:790
工業鹽使用方法 瀏覽:143
鍛煉基礎腹肌方法視頻教程 瀏覽:201
介入方法是什麼意思 瀏覽:645
汽車阻尼器的安裝方法 瀏覽:153
論文設計並運用相關研究方法 瀏覽:558
js封裝的方法如何在頁面內調用 瀏覽:539
定量和定性研究方法的種類 瀏覽:950
腰間盤如何鍛煉方法 瀏覽:608
過河的簡單方法 瀏覽:587
傳播研究方法教材 瀏覽:282
骨科治療腱鞘炎的方法 瀏覽:597
電腦突破網路限速的方法 瀏覽:158
溶液中鋰離子濃度檢測方法 瀏覽:162
紅杉樹樹皮的食用方法 瀏覽:732
剔除離散值計算方法 瀏覽:623
seo有哪些重要的方法 瀏覽:739