Click here to Skip to main content
15,870,297 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
hi I just wrote a code that imports data of a specific cell from excel file.
now i want to do 2 works to my code here they are:
1.i want the code to be repeated every N minutes. I tried to use the code that i mention here but The app just give me this Error:
Cross-thread operation not valid: Control 'textbox1' accessed from a thread other than the thread it was created on.

2.I want the app that after 10 times of repeating check the output if its the same as the first output stops the loop and show a message box.

I appreciate your helps!

What I have tried:

Public Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim minutes As Double = 0.5
Dim t As New System.Timers.Timer(60000 * minutes)
AddHandler t.Elapsed, AddressOf t_Elapsed
t.Start()
end sub
Private Sub t_Elapsed(sender As Object, e As System.Timers.ElapsedEventArgs)
'here i write my main code to be repeated
end sub
Posted
Updated 9-Oct-19 2:58am
Comments
F-ES Sitecore 9-Oct-19 4:54am    
Google the error message you're getting, it's a very frequently asked question and easily solvable.

Cross threading errors occur when you try to access controls from a thread on which they were not created - the UI thread. The Timer.Elapsed event does not occur on teh main thread, so it needs to be invoked using the Control.Invoke Method (System.Windows.Forms) | Microsoft Docs[^]
 
Share this answer
 
Comments
sepehrtavangar 9-Oct-19 9:33am    
thanks a lot but as Im very new in writing Codes i couldn't Understand what has been written on the site that you have been given
OriginalGriff 9-Oct-19 10:01am    
You are dealing with complicated stuff - if you can't understand the documentation yet, then you are trying to run before you can walk, and need to back up and study some of the basics in more detail first.
Public Class Form1
    Private Ncount As Integer = 0
    Private Nminute As Integer = 4
    Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        Timer1.Interval = nminute * 60000
        Timer1.Start()
    End Sub

    Private Sub Timer1_Tick(sender As Object, e As EventArgs) Handles Timer1.Tick
        'write your code
        Ncount = Ncount + 1
        If Ncount > 10 Then
            Timer1.Stop()
            'write your code to check
        End If
    End Sub
End Class
 
Share this answer
 
Comments
sepehrtavangar 9-Oct-19 9:29am    
thanks,but
this code does nothing i mean nothing will happen.
kgmmurugesh 9-Oct-19 10:01am    
Timer1.Tick event fires every 4 minutes. Reduce the timer1.inverval then check.
sepehrtavangar 9-Oct-19 10:38am    
hi i changed the Nminute first value and also the n count in if but the code just ran for once.
but by changing the line : Timer1.stop()
to the end of my base code the problem solved and after n tries the times closed thank u so much
kgmmurugesh 10-Oct-19 1:13am    
OK.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900