如何在每个tick事件C#.NET中更改标签文本 [英] How do I change the the label text every tick event C# .NET

查看:87
本文介绍了如何在每个tick事件C#.NET中更改标签文本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我认为我正在努力实现这一目标......



我的代码现在做的是尝试将myLabel1更改为firstBlinkName; AND secondBlinkName;在同一时间..我想介于两者之间,我真的不知道该怎么做。



我需要创建这样的东西..



I think I am making this harder than it should be..

What my code does now is that it tries to change the myLabel1 to firstBlinkName; AND secondBlinkName; at the same time.. I would like some time in between, I really dont know how to do this.

I need to create something like this..

            var firstBlinkName = nameBlinkFirstName.Text;
            var secondBlinkName = nameBlinkSecond.Text;

Label.Text = firstBlinkName;
//Pause of 1 second
Label.Text = secondBlinkName;









但这就是它结束的方式







But this is how it ended up

private void nameBlinkInterval_Tick(object sender, EventArgs e)
{
    var firstBlinkName = nameBlinkFirstName.Text;
    var secondBlinkName = nameBlinkSecond.Text;
    myLabel1.text = firstBlinkName;
    myLabel1.text = secondBlinkName;
}





我尝试过:



我试过的一切都是他的问题



What I have tried:

Everything I've tried is int he question

推荐答案

我会通过一个单独的线程来做,这是更可靠的方式。这取决于目的。在循环的每次迭代中,可以通过实时读取来补偿一些时间上的不准确性。例如,对平滑运动的印象是好的;在其他情况下,它并不好。你甚至可以创建一个真实的时钟,但是你需要一段延迟( Thread.Sleep ),这比更新时钟视图的时间要小一些。



使用计时器,您需要更加小心。第一个想法是:你可能需要一些近乎周期性的行为。如果是这样,请确保永远不要使用计时器类型 System.Windows.Timer :它很简单,并保证在UI线程中调用事件处理程序,但其计时准确性非常糟糕。当准确性根本不重要时,它可用于执行超时。



让我们回到其他可以给你合理准确性的计时器。他们无法保证在您的UI线程中调用您的处理程序方法。你必须考虑到它。您不能从非UI线程调用与UI相关的任何内容。相反,您需要使用 Invoke System.Windows.Threading的方法。 Dispatcher (对于Forms或WPF)或 System.Windows.Forms.Control (仅限表单)。



您将在我过去的答案中找到有关其工作原理和代码示例的详细说明:

Control.Invoke()与Control.BeginInvoke()

使用Treeview扫描仪和MD5的问题



另请参阅有关线程的更多参考资料:

主要的.NET事件线程

如何让keydown事件在不同的线程上运行i n vb.net

在启用禁用+多线程后控制事件未触发



-SA
I would do it via a separate thread, which is more reliable way. It depends on purpose though. Some inaccuracy in time can be compensated be taking real time reading in each iteration of a loop. It's good, for example, to make impression of smooth motion; in other cases it is not good. You can even create a realistic clock, but you would need a delay (Thread.Sleep) somewhat smaller then the period of time for an update a clock view.

With a timer, you need to be more careful. First idea is: you probably need some near-periodic behavior. If so, make sure you never use the timer type System.Windows.Timer: it is simple and guarantees that the event handler is called in the UI thread, but its timing accuracy is prohibitively bad. It can be used for, say, implementing of timeouts when accuracy is not critical at all.

Let's come back to other timers which can give you reasonable accuracy. They cannot guarantee the call of your handler method in your UI thread. You have to take it into account. 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.

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

—SA


这篇关于如何在每个tick事件C#.NET中更改标签文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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