当前位置:编程学习 > C#/ASP.NET >>

C# 窗体上 Label控件 实时刷新 显示当前时间

我用了

while (true)
            {
                label.Text = DateTime.Now.Second.ToString();
            }

可是程序就停止响应  请问高手怎么办才好

--------------------编程问答-------------------- 怎么能让他不停地更新 而且我还能进行其他操作,如果叫我就 timer就不用了  --------------------编程问答-------------------- while (true)
            {
                label.Text = DateTime.Now.Second.ToString();
                Application.DoEvents();
                System.Threading.Thread.Sleep(100);
            } --------------------编程问答--------------------
引用 2 楼 caozhy 的回复:
while (true)
            {
                label.Text = DateTime.Now.Second.ToString();
                Application.DoEvents();
                System.Threading.Thread.Sleep(100);
            }


-------------------------------
正解 --------------------编程问答-------------------- 除了 2楼版主的方式还可以使用Time定时器来完成
1、定义Time定时器的轮询间隔
2、在Time定时器的事件中写给Label赋当前时间的代码 --------------------编程问答--------------------
引用 2 楼 caozhy 的回复:
while (true)
            {
                label.Text = DateTime.Now.Second.ToString();
                Application.DoEvents();
                System.Threading.Thread.Sleep(100);
  ……


可以用多线程的方法做吗  请帮忙??????!!!! --------------------编程问答-------------------- 楼主,死循环的代码是很容易出现内存溢出的 --------------------编程问答--------------------
引用 6 楼 lyq8376 的回复:
楼主,死循环的代码是很容易出现内存溢出的


比如这个不用死循环用什么 帮帮忙 --------------------编程问答--------------------
引用 7 楼 lht8005362388 的回复:
引用 6 楼 lyq8376 的回复:
楼主,死循环的代码是很容易出现内存溢出的

比如这个不用死循环用什么 帮帮忙


那你就采用4楼的timer的方法吧。思路已经提供给你了 --------------------编程问答-------------------- load 事件里面下 

this.timer1.Interval = 1000;
this.timer1.Start();

timer1_Tick事件下写

 label.Text = DateTime.Now.Second.ToString();
Application.DoEvents(); --------------------编程问答-------------------- 都是不是我想要的 我想要用 多线程的方法  但是不知道怎么做 --------------------编程问答--------------------
引用 2 楼 caozhy 的回复:
while (true)
            {
                label.Text = DateTime.Now.Second.ToString();
                Application.DoEvents();
                System.Threading.Thread.Sleep(100);
  ……


正解。 --------------------编程问答-------------------- 如果不用timer,写成后台线程,线消息更新就可以了。麻烦点,但彻底。 --------------------编程问答-------------------- 还是么有用解决 --------------------编程问答--------------------

public void StartShowTime(){
Thread threadShowTime = new Thread(new ThreadStart(() => {
label1.BeginInvoke(new MethodInvoker(() => label1.Text = DateTime.Now.ToString()))
Thread.Sleep(1000);
}));
threadShowTime.IsBackground = true;
threadShowTime.Start();
}

直接手打的 不知道 对不对 反正思路就这个样 --------------------编程问答--------------------

public void StartShowTime(){
Thread threadShowTime = new Thread(new ThreadStart(() => {
while(true){
label1.BeginInvoke(new MethodInvoker(() => label1.Text = DateTime.Now.ToString()))
Thread.Sleep(1000);
}
}));
threadShowTime.IsBackground = true;
threadShowTime.Start();
}

上面的貌似少了一个循环 --------------------编程问答-------------------- bool _isClosed = false;
void CreateThread()
{
   Thread thread = new Thread(new ThreadStart(TimeProc));
   thread.Start();
}

void TimeProc()
{
     while(!_isClosed)
     {
        label1.BeginInvoke(new MethodInvoker(() => label1.Text = DateTime.Now.ToString()))
        Thread.Sleep(1000);

     }
} --------------------编程问答-------------------- break; --------------------编程问答-------------------- 表示用js轻松搞定~
function Clock() {
    var date = new Date();
    this.year = date.getFullYear();
    this.month = date.getMonth() + 1;
    this.date = date.getDate();
    this.day = new Array("星期日", "星期一", "星期二", "星期三", "星期四", "星期五", "星期六")[date.getDay()];
    this.hour = date.getHours() < 10 ? "0" + date.getHours() : date.getHours();
    this.minute = date.getMinutes() < 10 ? "0" + date.getMinutes() : date.getMinutes();
    this.second = date.getSeconds() < 10 ? "0" + date.getSeconds() : date.getSeconds();

    this.toString = function () {
        return "现在是:" + this.year + "年" + this.month + "月" + this.date + "日 " + this.hour + ":" + this.minute + ":" + this.second + " " + this.day;
    };

    this.toSimpleDate = function () {
        return this.year + "-" + this.month + "-" + this.date;
    };

    this.toDetailDate = function () {
        return this.year + "-" + this.month + "-" + this.date + " " + this.hour + ":" + this.minute + ":" + this.second;
    };

    this.display = function (ele) {
        var clock = new Clock();
        ele.innerHTML = clock.toString();
        window.setTimeout(function () { clock.display(ele); }, 1000);
    };
}
补充:.NET技术 ,  C#
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,