C#中的数字时钟程序! [英] a digital clock program in c# !

查看:102
本文介绍了C#中的数字时钟程序!的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是Amator ...指导我用c#制作数字时钟程序!

I''m Amator...guide me to make a digital clock program in c#! thanks!

推荐答案

太好了!对于初学者来说是极好的运动,几乎是一种传统!

首先,确定要使用的是System.Windows.Forms还是WPF.了解每种方法的基础知识(使用Google,但首先使用搜索参数"site:Microsoft.com".一个提示:使用线程而不是计时器,并且在线程体中也使用System.DateTime.即使使用计时器,也要始终使用这种结构的时间,永远不要依赖计时器的计时.(但避免使用计时器,通常来说,线程要容易得多,更安全且更正确;使线程主体成为一个调用System.Threading.Thread.Sleep的循环,其睡眠时间少于a第二.

还有什么?哦,调用!您无法从非UI线程调用与UI相关的任何操作.相反,您需要使用System.Windows.Threading.DispatcherInvokeBeginInvoke方法(对于Forms或WPF)或System.Windows.Forms.Control(仅对于Forms).

在我过去的答案中,您将找到有关其工作原理的详细说明和代码示例:
Control.Invoke()与Control.BeginInvoke() [ ^ ],
Treeview Scanner和MD5的问题 [如何获取keydown事件在vb.net中的不同线程上操作 [启用禁用+多线程后控件事件不会触发 [ ^ ].

但是,该初学者的工作将为您提供真正要在大多数其他项目中使用的非常重要的.NET技术的大量经验.

—SA
Great! Excellent exercise for a beginner, almost a tradition!

First, decide what you want to use, System.Windows.Forms or WPF. Learn the basics of each (use Google but at first prefer the search parameter "site:Microsoft.com". One hint: use thread, not a timer and, in the thread body also use System.DateTime. Even with timer, always take time from this structure, never rely on timer''s timing. (But avoid a timer, a thread is much easier, safer and more correct, in general; make thread body a cycle calling System.Threading.Thread.Sleep with the sleep time less then a second.

What else? Oh, invocation! You cannot call anything related to UI from non-UI thread. Instead, you need to use the method Invoke or BeginInvoke of System.Windows.Threading.Dispatcher (for both Forms or WPF) or System.Windows.Forms.Control (Forms only).

You will find detailed explanation of how it works and code samples in my past answers:
Control.Invoke() vs. Control.BeginInvoke()[^],
Problem with Treeview Scanner And MD5[^].

Same thing with timer. Better use System.Timers.Timer, and don''t use System.Windows.Forms.Timer (almost never); it''s unreliable in terms of accuracy but does not require invocation, already works in UI thread. For other timers, you need invocation on UI thread, as described above.

See also more references on threading:
How to get a keydown event to operate on a different thread in vb.net[^],
Control events not firing after enable disable + multithreading[^].

This beginner''s work nevertheless will give you a good chunk of experience in very important .NET techniques you really want to use in most of other projects.

—SA


人们应该真正学习如何使用搜索功能...
使用.NET的多用途数字时钟控制 [一个WPF数字时钟 [
People should really learn how to use a search function...
Multipurpose Digital Clock Control using .NET[^]
A WPF Digital Clock[^]

Not the best articles according to their rating, but it''s probably the best answer you''re going to get.
For any SPECIFIC problems you can come back to the quick answers section...


这篇关于C#中的数字时钟程序!的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆