Cannot access text box in form from timer interrupt event?

DanielB33

Active member
Joined
Mar 29, 2013
Messages
40
Programming Experience
1-3
Below is code for a very simple procedure. I enable a timer, every 50 ms I check if a flag in another form is set indicating to update some text boxes (text boxes hold data for motor controller) I am 100% sure that the algorithm is working correctly to the point where I write to the text box. Does anybody know why I cannot write to the text boxes in my Real_Time_Display form? If I write the text boxes upon loading the form, it works fine. But when accessing from the interrupt nothing happens. ThanksPublicClassReal_Time_Display
PrivateSharedWithEvents myTimer2 AsNew System.Windows.Forms.Timer()
PrivateSharedSub TimerEventProcessor(myObject AsObject, ByVal myEventArgs AsEventArgs) Handles myTimer2.Tick
'Get data and update form here
If (GlobalVariables.Update_dataFlag = True) Then
GlobalVariables.Update_dataFlag = False
Real_Time_Display.txtTempM1.Text = Main.realTime_DisplayData(1).ToString
Real_Time_Display.txtTempM2.Text = Main.realTime_DisplayData(2).ToString
Real_Time_Display.txtAmbientTemp.Text = Main.realTime_DisplayData(3).ToString
Real_Time_Display.txtVoltage.Text = Main.realTime_DisplayData(4).ToString
Real_Time_Display.txtCurrentM1.Text = Main.realTime_DisplayData(5).ToString
Real_Time_Display.txtCurrentM2.Text = Main.realTime_DisplayData(6).ToString
EndIf
EndSub
'Form load events
PrivateSub Real_Time_Display_Load(sender AsObject, e AsEventArgs) HandlesMyBase.Load
myTimer2.Interval = 50 'Interval for count in milliseconds. We need to poll
myTimer2.Enabled = True'Start the timer
EndSub
EndClass
 
How EXACTLY are you displaying that Real_Time_Display form in the first place? I suspect that you are displaying an instance that you are creating explicitly and then updating the default instance. That would be like writing on one piece of paper and expecting the words to magically appear on another.
 
How EXACTLY are you displaying that Real_Time_Display form in the first place? I suspect that you are displaying an instance that you are creating explicitly and then updating the default instance. That would be like writing on one piece of paper and expecting the words to magically appear on another.

I am note sure what you are asking. My question was pretty straight forward wasn't it? I have a timer, but cannot access my form that IS open while in the tick event. There has to be a way to do this. What do you mean by "How EXACTLY are you displaying that Real_Time_Display form in the first place?" I simply have a form with some text boxes that I update every 500ms or so. My main form is connected to a PCB thru COM, and receives motor data at 20sps. I just need to get that data into the text boxes in real time, but I cannot access those text boxes in the form Real_Time_Display. Does this make more sense? Thanks for the reply.
 
My question was pretty straightforward wasn't it? If the user is able to see a form then you must have displayed it to them. How did you do that? Form's don't simply pop into existence on the screen. Either it's the startup form of the app or you explicitly displayed it. Which one is it and, if it's the latter, how did you do it? You would have had to call a Show or ShowDialog method so show us that code.
 
I see. I have a main form, when the user clicks an item in a drop down menu, the code SecondForm.Show()
is called. This brings form 2 up that has the text boxes for the real time display. Does this answer your question? I am not very familiar with vb.net, I typically only program in c so I am not familiar with instances, etc. Thanks.
 
Got it. For what ever reason I needed to declare my timer a different way? The code below works great.Imports System.Timers

Imports System

Imports System.Windows.Forms


Public Class Real_Time_Display

Private WithEvents xTimer As New System.Windows.Forms.Timer

Public Sub StartTimer()

xTimer.Start()
End Sub
Public Sub StopTimer()
xTimer.Stop()
End Sub
Private Sub Timer_Tick() Handles xTimer.Tick
txtTempM1.Text = Main.realTime_DisplayData(0).ToString
txtTempM2.Text = Main.realTime_DisplayData(1).ToString
txtAmbientTemp.Text = Main.realTime_DisplayData(2).ToString
txtVoltage.Text = Main.realTime_DisplayData(3).ToString
txtCurrentM1.Text = Main.realTime_DisplayData(4).ToString
txtCurrentM2.Text = Main.realTime_DisplayData(5).ToString
End Sub
'Form load events
Private Sub Real_Time_Display_Load(sender As Object, e As EventArgs) Handles MyBase.Load
'myTimer2.Interval = 10 'Interval for count in milliseconds. We need to poll

'myTimer2.Enabled = True 'Start the time

xTimer.Interval = 10


xTimer.Enabled = True

End Sub
End Class
 
Back
Top