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.
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 as follow:
'=============== 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)
When the exception raised, I close the handled exception and try to open the form again it open the form with Timer1 is disabled.
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
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.