使用 VB.NET 的秒表循环 [英] StopWatch Loop using VB.NET

查看:51
本文介绍了使用 VB.NET 的秒表循环的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想用 VB.NET 用这个界面创建一个简单的计时器

I want to create a simple timer with this interface using VB.NET

我想按下 Button1 并开始在文本框中计算秒数.

I want to press Button1 and to start counting seconds in the textbox.

我不想使用计时器组件,因为它不提供高分辨率.

因此,我决定使用秒表类,因为它具有符合规范的高分辨率.

So, I decided to use a stopWatch Class due to its high resolution according to specifications.

但根据我下面的 VB.NET 代码,在我看来,整个dotnet 冒险"是不可能的.那是因为当我按下 Button1 时,整个表单会冻结,我无法按下 Button2 来停止计时器.

But according to my VB.NET code below it seems to me that the whole "dotnet adventure" is impossible. That is because when I press Button1 the whole form it freezes and I cannot press Button2 to stop the timer.

我的代码有问题吗?我应该怎么做才能拥有上述功能?

Is there anything wrong with my code? What should I do to have the functionality described above?

提前致谢!

 
Public Class Form1

结束课程

推荐答案

在 WinForms 中,有一个 UI 线程执行消息循环.基本上,每个事件都被添加到消息队列中,一个又一个事件被处理.当 Button1 被点击时,Button1_Click 方法被执行并且在它完成之前不会处理其他事件.由于您的设计需要处理 Button2.Click 以终止 Button1.Click 中的循环,因此它永远不会终止.

In WinForms, there is one UI thread executing the message loop. Basically, every event is added to the message queue, which is processed, one event after another. When Button1 is clicked, the Button1_Click method is executed and no other event will be processed until it finishes. Since your design requires Button2.Click to be processed in order to terminate the loop in Button1.Click, it will never terminate.

要正确实现您想要的,您必须在 Button1.Click 上启动秒表,并将 UI 更新逻辑放入计时器的 Tick 事件中你放在表格上.

To correctly implement what you want, you'd have to start the stopwatch on Button1.Click and put the UI update logic into the Tick event of a timer which you place on the form.

这篇关于使用 VB.NET 的秒表循环的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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