计时器事件蜱Form_Load事件之前触发(VB.NET)事件、计时器、Form_Load、VB

2023-09-04 06:02:29 作者:深知你是梦

我有一个Windows窗体与定时器组件,默认情况下(设计)的计时器已启用,但基于发送给我禁用的Form_Load定时器等形式的论点。

I have a Windows form with Timer component, by default (design) the timer is enabled, but based on some arguments sent to the form I disable the timer on form_load.

我现在面临一个非常奇怪的情况下,Timer_Tick事件被解雇的某个时候,甚至之前在Form_Load被解雇,这个发生在最小化的20分钟例如应用程序,然后我打开应用程序,并试图打开新的形式,特别是在较慢的系统。

I'm facing a very weird scenario, the Timer_Tick event is sometime fired even before the form_load is fired, this happen with the application minimized for 20 mins for example, then I open the application and trying to open new form, especially on slow systems.

code如下:

'=============== Code of the form with Timer
Public Sub OpenForm(SomeParams)
        'Set Form Properties
        Me.Show() 'Here the event Form_Load fired
End Sub

Private Sub Form_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
  'Some Code ...
  Timer1.Enabled = False/ True 'Based True or false based on parameters
 'Code ...
End Sub

Private Sub Timer1_Tick(sender As System.Object, e As System.EventArgs) Handles Timer1.Tick
  'Code here
  'The code raise error if form load is not fired, because need info from params ...
End Sub



'=============== Code in the calling form (MainForm)
'Calling the Form
dim obj As new Form1  'I think form this line the Timer1_Tick Fired, before load
obj.OpenForm(Params)

在异常引发,我关闭处理的异常并尝试再次打开该窗体打开它的形式与Timer1被禁止。

When the exception raised, I close the handled exception and try to open the form again it open the form with Timer1 is disabled.

我知道解决的办法是小事,只是让定时器默认被禁用,则启用基于是否存在params,但我想知道为什么Timer1_Tick某个之前的OpenForm Sub和Form1_Load的子之前解雇! ?

I know the solution is trivial, just make the timer disabled by default, then enabled based on the params, but I want to know WHY the Timer1_Tick sometime is fired before OpenForm Sub and Before Form1_Load Sub !! ?

非常感谢您的时间。 萨迈赫

Many thanks for your time. Sameh

推荐答案

您声明并initalize您的计时器的形式在InitializeComponent,所谓的形式构造函数中。这立即开始你的计时器,那么你从形式构造和形式演出前退出(这引起了Form_Load事件)在规定的间隔时间过去。照片 这种情况可能会在空闲应用在磁盘上的虚拟存储器交换远的情况下被增加。在物理内存中的重装需要更多的时间。 你可以检验我的假设减少间隔值。 窗体加载事件之前,您应该获得更多的Timer_Tick事件。

You declare and initalize your timer in the InitializeComponent of the form, called inside the form constructor. This starts immediately your timer, then you exit from the form constructor and before the form shows (which raises the Form_Load event) the allotted interval passes. This situation could be increased in the case of an idle app swapped away in the virtual memory on disk. The reloading in physical memory requires more time. You could test my hypothesis decreasing the Interval value. You should get more Timer_Tick events before the form load event.