我是这个领域的新宠.我在我的Windows窗体中使用一个计时器,它以固定的间隔从数据库中获取数据.现在我希望我的计时器重新发送同样的信息,但我需要在屏幕上显示每秒运行的秒表.有什么办法吗?代码在这里......
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;
}
Run Code Online (Sandbox Code Playgroud)
提前致谢.....
您可以使用StopWatch类来获取时间Elapsed
来自MSDN:
提供一组方法和属性,可用于准确测量经过的时间.
第1步:创建一个StopWatch变量作为类级变量.
第2步:Start()每当你想开始计数时调用方法.
第3步:对于每个timer_tick事件,您可以使用usig经过的Totalseconds stopwatch.Elapsed.Seconds.
试试这个:
Stopwatch watch = new Stopwatch();
watch.Start();
private void timer_Tick(object sender, EventArgs e)
{
label1.Text = watch.Elapsed.Seconds.ToString();
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
7802 次 |
| 最近记录: |