运行环境:Microsoft Visio Studio 2015
控件说明:1.button(一键获取当前系统时间!) 2.TextBox(显示时间)
实现的效果如下:
时间跟随系统时间滚动输出
完整源代码如下:
1 using System; 2 using System.Threading; 3 using System.Windows.Forms; 4 5 namespace EXP3_C53_GetTime_threading 6 { 7 public partial class Form1 : Form 8 { 9 public Form1() 10 { 11 InitializeComponent(); 12 } 13 bool flag = false; 14 15 public void showTime() 16 { 17 while (true) 18 { 19 DateTime dateTime = DateTime.Now; 20 textBox1.Text = "当前系统时间为:" + dateTime.ToString(); 21 Thread.Sleep(1000); 22 } 23 } 24 25 private void button1_Click(object sender, EventArgs e) 26 { 27 flag = true; 28 if (flag) 29 { 30 Thread P_thread = new Thread(new ThreadStart(showTime)); 31 P_thread.IsBackground = true; 32 P_thread.Start(); 33 } 34 } 35 36 } 37 }
注:本博客博文均为原创,转载请注明出处!
时间: 2024-10-05 22:19:18