在特定时间发送的vb.net中的计时器 [英] Timer in vb.net that is sent at certain time

查看:100
本文介绍了在特定时间发送的vb.net中的计时器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我正在尝试编写将在一定时间内发送x条消息的代码.
用户将输入他们要发送的邮件数量.[amtMES]
-以及每条消息所花费的时间. [timemessage]
-以及发送所有邮件的总时间.


我一直在尝试这样做2周.我离开了几个女士.

Hi,

I am trying to write a code that will send x amount of messages in a certain time.
the user will enter the amount of message they want to send.[amtMES]
-and the time it will take for each message. [timemessage]
-and the total time to send all the messages.


i have been trying to do this for 2 wks. i am off by couple ms .

推荐答案

看看您的(已删除)代码,我可以假设您可能正在使用System.Windows.Forms.Timer.此计时器只有一个好处:简化了使用.该计时器的问题更为严重:其精度很差,因此只能在不需要精度的情况下使用.我的意思是说,这不是对准确性的低要求,而只是在根本没有准确性的情况下:-)
(例如:闪屏.)例如,当人们使用此计时器进行简单的动画处理(非常简单的任务)时,均匀的速度看起来会变得生涩,不仅用肉眼可见,而且非常令人讨厌.

因此,您需要使用System.Threading.TimerSystem.Timers.Timer.唯一的问题-它们都需要调用才能与UI线程一起使用.

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

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

—SA
Looking at your (deleted) code, I can assume that you are probably using System.Windows.Forms.Timer. This timer has only one benefit: its use is simplified. The problem with this timer is more serious: its accuracy is that bad so it can only be used when no accuracy is required. I mean it, not low requirements to accuracy, but only when there is no accuracy at all :-)
(Example: flash screen.) For example, when people used this timer for simple animation (very simple task), uniform speed looked jerky, not just visible with unarmed eye, but just highly irritating.

So, you need to use either System.Threading.Timer or System.Timers.Timer. The only problem — they both need invocation to work with UI thread.

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[^].

Besides, you should always consider not using timers at all, but using a separate thread instead. The invocation problem will look the same.

Now matter what you use, you should understand that Windows is nothing like a real-time system, so all timing is probabilistic. However, you can have reasonable smoothness of, say, periodic operations with reasonable probability in case of stable operating conditions (for example, if the OS is not overloaded, does not run time-consuming processes/threads with unreasonably high priority, etc.).

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[^].

—SA


几毫秒? Microsoft Windows不是关键任务操作系统.如果您告诉它在某个时间做某事,它将接近,但不完全准确.实际发生的情况是您的请求在正确的时间排入队列,并在到达时完成.
A couple of milliseconds ? Microsoft Windows is not a mission critical operating system. If you tell it to do something in a certain time, it will come close, but not be exact. What actually happens is that your request gets put in a queue at the right time, and done when it gets to it.


这篇关于在特定时间发送的vb.net中的计时器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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