일시 중지 기능이있는 ac # winforms 응용 프로그램에서 30 분부터 카운트 다운 타이머를 만든 다음 일시 중지가 발생한 위치에서 다시 시작하려고했습니다. System.Windows.Forms.Timer의 특정 일시 중지 기능이 없다는 것을 알고 있기 때문에이를 달성하기 위해 여러 가지 솔루션을 시도했습니다. 인터넷을 샅샅이 뒤졌지만 내 시나리오에 적용 할 수있는 것을 찾을 수 없었습니다. 내가 시도한 모든 것은 타이머가 30 분에서 다시 시작되거나 멈추지 않고 있었던 곳에서 계속됩니다. 이것은 나를 미치게 만든다. 누구든지이 작업을 수행하는 다른 방법을 돕거나 제안 할 수 있습니까? 이것은 나의 첫 번째 게시물이므로 오류가 발생하면 사과드립니다. 아래 코드. 문제를 일으키는 코드를 주석 처리했습니다. 구문 론적으로나 논리적 으로든 정확하지 않다는 것을 알고 있습니다.
public partial class FormWithTimer : Form
{
System.Windows.Forms.Timer timerx = new System.Windows.Forms.Timer();
DateTime startTime = DateTime.Now;
DateTime stopTime = DateTime.Now;
public FormWithTimer()
{
InitializeComponent();
TimerLabel.Text = "30:00";
}
private void Form1_Load(object sender, EventArgs e)
{
}
void timer_Tick(object sender, EventArgs e)
{
}
public void StartButton_Click(object sender, EventArgs e)
{
startTime = DateTime.Now;
BeginTextBox.Text = startTime.ToString();
TimerLabel.Visible = true;
timerx.Tick += (obj, args) =>
TimerLabel.Text = (TimeSpan.FromMinutes(30) - (DateTime.Now - startTime)).ToString("mm\\:ss");
timerx.Enabled = true;
}
public void PauseButton_Click(object sender, EventArgs e)
{
if (PauseButton.Text == "Pause")
{
timerx.Stop();
PauseButton.Text = "Start";
stopTime = DateTime.Now;
}
else
{
PauseButton.Text = "Pause";
timerx.Start();
TimerLabel.Visible = true;
//timerx.Tick += (obj, args) =>
// TimerLabel.Text = (TimeSpan.FromMinutes(30) - (DateTime.Now - (startTime - stopTime))).ToString("mm\\:ss");
timerx.Enabled = true;
}
}
}
나는 당신이 타이머가 일시 중지되었을 때의 위치에서 계속되도록 노력하고 있다고 생각합니다.
이를 위해 다음과 같이 사용 stopTime
하는 것이 좋습니다 .
"일시 중지-일시 중지 해제"-루틴 :
public void PauseButton_Click(object sender, EventArgs e)
{
if (PauseButton.Text == "Pause")
{
timerx.Stop();
PauseButton.Text = "Start";
stopTime = DateTime.Now;
}
else
{
PauseButton.Text = "Pause";
startTime += (DateTime.Now - stopTime);
timerx.Start();
TimerLabel.Visible = true;
timerx.Enabled = true;
}
}
이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.
침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제
몇 마디 만하겠습니다