using?System;
創(chuàng)新互聯(lián)是一家專注于成都網(wǎng)站設(shè)計(jì)、網(wǎng)站制作與策劃設(shè)計(jì),二道江網(wǎng)站建設(shè)哪家好?創(chuàng)新互聯(lián)做網(wǎng)站,專注于網(wǎng)站建設(shè)10余年,網(wǎng)設(shè)計(jì)領(lǐng)域的專業(yè)建站公司;建站業(yè)務(wù)涵蓋:二道江等地區(qū)。二道江做網(wǎng)站價(jià)格咨詢:18980820575
using?System.Windows.Forms;
namespace?WindowsFormsApplication1
{
public?partial?class?Form1?:?Form
{
Timer?tm?=?new?Timer();//實(shí)例化?timeer?
static?int?timeS?=?0;?//設(shè)置靜態(tài)變量記錄秒數(shù)
TimeSpan?ts?=?new?TimeSpan();?//實(shí)例化?TimeSpan?
public?Form1()
{
InitializeComponent();
}
private?void?button1_Click(object?sender,?EventArgs?e)
{
tm.Interval?=?1000;?//設(shè)置?timeer?1000毫秒執(zhí)行一次
tm.Tick?+=?new?EventHandler(timeer_Tick);?//設(shè)置?timeer?運(yùn)行事件
tm.Start();?//?啟用?timeer
}
private?void?timeer_Tick(object?sender,?EventArgs?e)
{
timeS?+=?1;?//秒數(shù)?+1?
ts?=?new?TimeSpan(0,?0,?timeS);?
label1.Text?=?ts.Hours?+?":"?+?ts.Minutes?+?":"?+?ts.Seconds;
}
private?void?button2_Click(object?sender,?EventArgs?e)
{
tm.Stop();
timeS?=?0;
}
}
}
就是只允許啟動(dòng)單一個(gè)應(yīng)用程序的實(shí)例, 試圖啟動(dòng)第二次時(shí)不會(huì)產(chǎn)生新的實(shí)例,并激活已經(jīng)啟動(dòng)的實(shí)例.
該實(shí)例有很多實(shí)現(xiàn)方法,下面介紹一種不同的給你:
首先要在窗體中載入以下控件:progressbar(進(jìn)度條),label(用來(lái)實(shí)時(shí)顯示文本提示),兩個(gè)timer(分別控制進(jìn)度條進(jìn)度的增減),然后用以下代碼就能實(shí)現(xiàn):
Public Class Form1
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Timer1.Enabled = True
Timer1.Interval = 500
Timer2.Interval = 500
ProgressBar1.Value = 0
ProgressBar1.Maximum = 100
ProgressBar1.Minimum = 0
End Sub
'上述過(guò)程是設(shè)置控件的初始值,也可以在屬性面板設(shè)置
Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
ProgressBar1.Value += 1
Label1.Text = "當(dāng)前完成" ProgressBar1.Value "%"
If (ProgressBar1.Value = 100) Then
Timer1.Enabled = False
Timer2.Enabled = True
End If
End Sub
'timer1用來(lái)控制進(jìn)度的增加
Private Sub Timer2_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer2.Tick
ProgressBar1.Value -= 1
Label1.Text = "當(dāng)前完成" ProgressBar1.Value "%"
If (ProgressBar1.Value = 0) Then
Timer1.Enabled = True
Timer2.Enabled = False
End If
End Sub
'time2用來(lái)控制進(jìn)度的減少
End Class