我想寫一個像 OBS Studio 這樣的程式,我想在我的程式中添加一個向上計數器,這意味著當我點擊“開始流”時,它應該從 00:00:00 到 00:00:01 然后到 00:00 啟動向上計數器:02 等等,這樣我就有了“直播時間”,我已經流了多長時間。使用“結束流”按鈕,計時器應該停止并重置。我怎樣才能做到這一點?
我嘗試了不同的東西,但沒有想法。
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace StreamManagement
{
public partial class MainFormProgram : Form
{
public static bool chat_on = true;
TimeSpan testTime = DateTime.Now.TimeOfDay;
public MainFormProgram()
{
InitializeComponent();
}
private void chatOnBtn_Click(object sender, EventArgs e)
{
OnLbl.Text = "ON";
OnLbl.ForeColor = Color.ForestGreen;
chatOnBtn.BackColor = Color.LightGray;
chatOffBtn.BackColor = Color.Gray;
}
private void chatOffBtn_Click(object sender, EventArgs e)
{
OnLbl.Text = "OFF";
OnLbl.ForeColor = Color.DarkRed;
chatOffBtn.BackColor = Color.LightGray;
chatOnBtn.BackColor = Color.Gray;
}
private void Form1_Load(object sender, EventArgs e)
{
OnLbl.Text = "ON";
OnLbl.ForeColor = Color.ForestGreen;
EmoteonlyOnOff.Text = "OFF";
EmoteonlyOnOff.ForeColor = Color.DarkRed;
SubchatOnOff.Text = "OFF";
SubchatOnOff.ForeColor = Color.DarkRed;
}
private void EmoteOnlyOnBtn_Click(object sender, EventArgs e)
{
EmoteonlyOnOff.Text = "ON";
EmoteonlyOnOff.ForeColor = Color.ForestGreen;
EmoteOnlyOnBtn.BackColor = Color.FromArgb(112, 147, 252);
EmoteOnlyOffBtn.BackColor = Color.FromArgb(50, 100, 150);
}
private void EmoteOnlyOffBtn_Click(object sender, EventArgs e)
{
EmoteonlyOnOff.Text = "OFF";
EmoteonlyOnOff.ForeColor = Color.DarkRed;
EmoteOnlyOffBtn.BackColor = Color.FromArgb(112, 147, 252);
EmoteOnlyOnBtn.BackColor = Color.FromArgb(50, 100, 150);
}
private void SubChatOnBtn_Click(object sender, EventArgs e)
{
SubchatOnOff.Text = "ON";
SubchatOnOff.ForeColor = Color.ForestGreen;
SubChatOnBtn.BackColor = Color.FromArgb(255, 94, 255);
SubChatOffBtn.BackColor = Color.FromArgb(192, 0, 192);
}
private void SubChatOffBtn_Click(object sender, EventArgs e)
{
SubchatOnOff.Text = "OFF";
SubchatOnOff.ForeColor = Color.DarkRed;
SubChatOffBtn.BackColor = Color.FromArgb(255, 94, 255);
SubChatOnBtn.BackColor = Color.FromArgb(192, 0, 192);
}
private void startstreamBtn_Click(object sender, EventArgs e)
{
timerXD.Start();
}
private void timerXD_Tick(object sender, EventArgs e)
{
}
private void timerLbl_Click(object sender, EventArgs e)
{
}
}
}
uj5u.com熱心網友回復:
為此,您可以使用命名空間中的Stopwatch類System.Diagnostics。
Stopwatch在您的Form班級中創建一個欄位:
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
Stopwatch stopwatch = new Stopwatch(); // <-- this one
}
創建一個Label(lblTime例如),我們將Button在您的表單中設定計數時間和一個,它將開始Stopwatch(btnStart例如):

可選您也可以添加其它Buttons的操作Stopwatch:

將Click處理程式(ButtonStart_Click例如)添加到“開始”按鈕以從那里運行秒表。標記它async以防止Form在Stopwatch運行時凍結:
private async void ButtonStart_Click(object sender, EventArgs e)
{
stopwatch.Start();
while (stopwatch.IsRunning)
{
lblTime.Text = stopwatch.Elapsed.ToString("hh\\:mm\\:ss");
await Task.Delay(1000); // Delay between time updates on Form (1 sec)
}
}
現在您有了“向上計數計時器”。
評論:
- 使用
stopwatch.Reset();以停止計時器和重置測量的時間。 - 使用
stopwatch.Stop();,如果你想停止計時,但要保持meaused時間。類似于“暫停”。
完整Form代碼:
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
// "Upcount Timer"
private readonly Stopwatch stopwatch = new Stopwatch();
private async void ButtonStart_Click(object sender, EventArgs e)
{
stopwatch.Start();
// Disable START button and enable STOP and PAUSE buttons
btnStart.Enabled = !(btnPause.Enabled = btnStop.Enabled = true);
while (stopwatch.IsRunning)
{
// Update time in Label on Form
lblTime.Text = stopwatch.Elapsed.ToString("hh\\:mm\\:ss");
// Delay between next update
await Task.Delay(1000);
}
}
private void ButtonPause_Click(object sender, EventArgs e)
{
// Stop timer, but not reset measured time
stopwatch.Stop();
// Enable START button and disable STOP and PAUSE buttons
btnStart.Enabled = !(btnPause.Enabled = btnStop.Enabled = false);
}
private void ButtonStop_Click(object sender, EventArgs e)
{
// Stop timer and reset measured time
stopwatch.Reset();
// Reset time in Label on Form
lblTime.Text = "00:00:00";
// or use stopwatch.Elapsed property - after Reset() it would be 00:00:00
//lblTime.Text = stopwatch.Elapsed.ToString("hh\\:mm\\:ss");
// Enable START button and disable STOP and PAUSE buttons
btnStart.Enabled = !(btnPause.Enabled = btnStop.Enabled = false);
}
}
uj5u.com熱心網友回復:
只需將開始時間存盤在 DateTime 中,然后從當前時間中減去該時間即可獲得 TimeSpan。使用該 TimeSpan,您可以呼叫 ToString() 并指定您想要的任何格式。
就像是:
DateTime startDT;
private void Form1_Load(object sender, EventArgs e)
{
timerXD.Interval = 1000;
timerXD.Enabled = false;
}
private void startstreamBtn_Click(object sender, EventArgs e)
{
label1.Text = "00:00:00";
startDT = DateTime.Now;
timerXD.Start();
}
private void timerXD_Tick(object sender, EventArgs e)
{
String elapsed = DateTime.Now.Subtract(startDT).ToString(@"hh\:mm\:ss");
label1.Text = elapsed; // display elapsed somehow
}
uj5u.com熱心網友回復:
您可以使用一個單獨的任務來完成此操作,當您使用 CancellationToken 單擊按鈕時,該任務會取消您可以在類名正下方放置:
public CancellationTokenSource cancellationTokenSource { get; set; }
public CancellationToken cancellationToken => this.cancellationTokenSource.Token;
您的計時器任務可能如下所示:
private async Task Timer()
{
CancellationToken cancel = this.cancellationToken;
int s = 0;
TimeSpan ts = TimeSpan.FromSeconds(s);
while(!cancel.IsCancellationRequested)
{
lbltimer.Text = ts.ToString(@"hh\:mm\:ss");
await Task.Delay(1000);
s ;
ts = TimeSpan.FromSeconds(s);
}
}
現在是按鈕
把它放在你的 StartBtn 函式中
cancellationTokenSource = new();
Timer();
這在你的 StopBtn 函式中
cancellationTokenSource.Cancel();
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/315632.html
上一篇:C語言中的函式指標?
下一篇:從另一個標簽頁獲取值
