gpt4 book ai didi

c# - 如何在不使用计时器的情况下以 Windows 形式显示秒表

转载 作者:太空宇宙 更新时间:2023-11-03 19:09:24 24 4
gpt4 key购买 nike

我是这个领域的新人。我在我的窗口窗体中使用一个定时器,它以固定的时间间隔从数据库中获取数据。现在我希望我的计时器重新发送相同的邮件,但我需要在屏幕上显示秒表每秒运行一次。有什么办法吗?代码在这里.....

public partial class Form2 : Form
{
private Timer _timer;
int count = 0;
// The last time the timer was started
private DateTime _startTime = DateTime.MinValue;

// Time between now and when the timer was started last
private TimeSpan _currentElapsedTime = TimeSpan.Zero;

// Time between now and the first time timer was started after a reset
private TimeSpan _totalElapsedTime = TimeSpan.Zero;

// Whether or not the timer is currently running
private bool _timerRunning = false;
public Form2(String UserName)
{
InitializeComponent();
_timer = new Timer();
_timer.Interval = 1000;
_timer.Tick += new EventHandler(timer_Tick);
label4.Text = UserName;

}


private void richTextBox2_TextChanged(object sender, EventArgs e)
{

}


private void timer_Tick(object sender, EventArgs e)
{

count = count + 1;
var timeSinceStartTime = DateTime.Now - _startTime;
timeSinceStartTime = new TimeSpan(timeSinceStartTime.Hours,
timeSinceStartTime.Minutes,
timeSinceStartTime.Seconds);

// The current elapsed time is the time since the start button was
// clicked, plus the total time elapsed since the last reset
_currentElapsedTime = timeSinceStartTime + _totalElapsedTime;

// These are just two Label controls which display the current
// elapsed time and total elapsed time

label3.Text = timeSinceStartTime.ToString();
String str1, str2;
SqlDataReader rd1, rd2;
SqlConnection Con = new SqlConnection("Data Source=216.218.224.238;Database=chatapp;Uid=chatappuser;pwd=1234@ChatAppUser;MultipleActiveResultSets=true;");
// SqlConnection Con = new SqlConnection("Data Source=.\\SQLEXPRESS;Initial Catalog=Test;User ID=;Password=;Connection Timeout=600");
Con.Open();

rd1 = new SqlCommand("select top 1 Chat from chat where where email ='" + label4.Text + "'order by id Desc", Con).ExecuteReader();
rd1.Read();
str1 = rd1["Chat"].ToString();
rd1.Close();
rd2 = new SqlCommand("select top 1 UserInitial from chat where email ='" + label4.Text + "' order by id Desc", Con).ExecuteReader();
rd2.Read();
str2 = rd2["UserInitial"].ToString();
rd2.Close();
if (str1 != str2)
{
SqlDataReader rd3, rd4;
rd3 = new SqlCommand("select top 1 UserInitial from chat where email ='" + label4.Text + "'order by id desc", Con).ExecuteReader();

richTextBox1.Text = richTextBox1.Text + " <br /> " + rd3.Read();
rd3.Close();
rd4 = new SqlCommand("Update top 1 chat set Chat = UserInitial where email ='" + label4.Text + "'order by id desc", Con).ExecuteReader();
rd4.Read();
rd4.Close();
Con.Close();
}

}

private void button1_Click(object sender, EventArgs e)
{

if (!_timerRunning)
{
// Set the start time to Now
_startTime = DateTime.Now;

// Store the total elapsed time so far
_totalElapsedTime = _currentElapsedTime;

_timer.Start();
_timerRunning = true;
}
else // If the timer is already running
{
_timer.Stop();
_timerRunning = false;
}
SqlDataReader rd5;
SqlConnection Con = new SqlConnection("Data Source=216.218.224.238;Database=chatapp;Uid=chatappuser;pwd=1234@ChatAppUser;MultipleActiveResultSets=true;");
// SqlConnection Con = new SqlConnection("Data Source=.\\SQLEXPRESS;Initial Catalog=Test;User ID=;Password=;Connection Timeout=600");
Con.Open();
richTextBox1.Text = richTextBox1.Text + "<br />Me:" + richTextBox2.Text;
rd5 = new SqlCommand("Update chat set UserInitial ='" + richTextBox2.Text + "' order by id Desc where email ='" + label4.Text + "'", Con).ExecuteReader();
rd5.Read();
rd5.Close();
Con.Close();
}


private void button3_Click(object sender, EventArgs e)
{
_timer.Stop();
_timerRunning = false;

// Reset the elapsed time TimeSpan objects
_totalElapsedTime = TimeSpan.Zero;
_currentElapsedTime = TimeSpan.Zero;
label3.Text = _totalElapsedTime.ToString();
MessageBox.Show(count.ToString());
count = 0;
}

提前致谢......

最佳答案

您可以使用 StopWatch获取时间量的类 Elapsed

来自 MSDN:

Provides a set of methods and properties that you can use to accurately measure elapsed time.

第 1 步:创建一个 StopWatch 变量作为类级变量。

第 2 步:调用 Start() 方法,无论何时您想要开始计数。

第 3 步:对于每个 timer_tick 事件,您可以使用 stopwatch.Elapsed.Seconds 计算经过的总秒数。

试试这个:

Stopwatch watch = new Stopwatch();
watch.Start();
private void timer_Tick(object sender, EventArgs e)
{
label1.Text = watch.Elapsed.Seconds.ToString();
}

关于c# - 如何在不使用计时器的情况下以 Windows 形式显示秒表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22060823/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com