Refreshing every minutes - vb.net

In a form, there is a label. It shows whether web service is connected or not at every minutes. How to code to repeat this process? Will I use the thread or timer? Please share me.

You will need a timer object in order to run the code every X minutes. Using a separate thread to check the web service only needs to be done if it will take a while to check and you want your form to remain responsive during this time.
Using a timer is very easy:
Private WithEvents timer As New System.Timers.Timer
Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
'Set interval to 1 minute
timer.Interval = 60000
'Synchronize with current form, or else an error will occur when trying to
'update the UI from within the elapsed event
timer.SynchronizingObject = Me
End Sub
Private Sub timer_Elapsed(ByVal sender As Object, ByVal e As System.Timers.ElapsedEventArgs) Handles timer.Elapsed
'Add code here to check if the web service is updated and update label
End Sub

Related

Timer based clocks losing seconds. VB Community 2015

My question deals with Timer activated clocks. Upon research the most similar situation is found here:
Two timers with two different intervals C# Form
Situation: I have an application being used for a game. In this game, there are many instances where a user must be able to create clocks and timers that need to be accurate within a second and if clocks, follow UTC Time.
Due to the need for variables to be used across forms, they are declared in a module.
Module Global_Variables
' String Variables
' frmMain
Public strMDbPath As String
' Date & Time Variables
'frmMain
Public dtLocalTime As DateTime
Public dtEveTime As DateTime
End Module
"frmMain" the main form, uses two clocks. Clock 1 is local time and clock 2 is UTC time.
Public Class frmMain
Private Sub Page_Load(sender As Object, e As EventArgs)
'Start Eve and Local Time Clocks
tmrMainTimes.Interval = 1000
tmrMainTimes.Start()
End Sub
Private Sub mnuSecuredItemEnableAccess_Click(sender As Object, e As EventArgs) Handles mnuSecuredItemEnableAccess.Click
'Show Enable Access Form (frmFirstAccess)
Dim frmFirstAccess As New frmFirstAccess()
frmFirstAccess.ShowDialog()
End Sub
Private Sub tmrMainTimes_Tick(sender As Object, e As EventArgs) Handles tmrMainTimes.Tick
''Start Local Time
'toolstripItemLocalTime.Text = Format(Now, "HH:mm:ss")
''Start Eve (UTC) Time
'toolstripItemEveTime.Text = Format(Now.ToUniversalTime, "HH:mm:ss")
dtLocalTime = DateTime.Now.ToString("HH:mm:ss")
toolstripItemLocalTime.Text = dtLocalTime
dtEveTime = DateTime.UtcNow.ToString("HH:mm:ss")
toolstripItemEveTime.Text = dtLocalTime
End Sub
Private Sub TimersToolStripMenuItem_Click(sender As Object, e As EventArgs) Handles TimersToolStripMenuItem.Click
Dim frmTimers As New frmTimers()
frmTimers.ShowDialog()
End Sub
End Class
This works great!
The problem lies in the tools section where I am trying to give the user to initiate new timers. When I try to run a separate clock on this form, I initiate a different timer and execute the code in the same way, but the clock seems to lose a second compared to the other clocks.
Public Class frmTimers
Private Sub frmTimers_Load(sender As Object, e As EventArgs) Handles MyBase.Load
' Start Clock Timer for Timers
TimersClocksStart.Interval = 1000
TimersClocksStart.Start()
End Sub
Private Sub TimersClocksStart_Tick(sender As Object, e As EventArgs) Handles TimersClocksStart.Tick
txtCurrRollEveClock.Text = dtEveTime
End Sub
End Class
How do I 1) fix this lag of one second between clocks and timers on multiple forms? 2) prevent it from happening again?
At most, in the application there will ever need to be twenty-five clocks/timers in five forms. At least there will need to be about ten in three forms

VB: Repeat the function evey x minutes?

I'm working with visual basic express 2010 to create a very simple application.
I know this is basic stuff but i need to know how to repeat the same function every X minute while the application is being left open.
This is all my code:
Public Class Form1
Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
My.Computer.Network.DownloadFile(
"http://google.co.uk/images/someimage.png", "C:/Documents and Settings/All Users/Desktop/someimage.png")
End Sub
End Class
could someone please advise on this issue?
EDIT:
This is my entire code now:
Public Class Form1
Private Sub Timer1_Tick(sender As System.Object, e As System.EventArgs) Handles Timer1.Tick
My.Computer.Network.DownloadFile(
"http://new.tse.ir/en/json/MarketWatch/enMarketWatch_1.xls", "C:/temp/enMarketWatch_1.xls", "", "", False, 60, True)
End Sub
End Class
in the properties panel of the timer, I set the Enabled to true and Interval to 60000.
when i run this code, I get file downloaded but 1 second later, the file gets deleted automatically and an error pops up in the visual basic saying the operation has timed out
I tried to change the directory and still happening.
any advise would be appreciated.
Add a timer to your form in the graphical designer.
Double click the timer to generate its tick event handler code in the code window.
Move the code you want to repeat into a sub
Private Sub DownloadFile()
My.Computer.Network.DownloadFile("http://google.co.uk/images/someimage.png", "C:/Documents and Settings/All Users/Desktop/someimage.png")
End Sub
Add the command below into your timer tick event handler
DownloadFile()
Change your form.load event to
Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
DownloadFile()
Timer1.Interval = x ' where x is the number of minutes*60000 because there are 60000 ticks in a minute
Timer1.Enabled = True
End Sub
The reason I've put your code into a separate sub is so that it is easily reusable in both the form.load handler and the timer.tick handler without having to write it again, and if in the future you need to change, for example the file path, you only need to remember to change it once.
Also I should add that, in the form.load handler I have included the DownloadFile method because, when the timer is enabled, it won't generate a tick until the interval has elapsed. Not at the beginning when the timer is enabled.
Also - as Plutonix suggested in comments below - If it is possible that the file to be downloaded will take longer to download than the length of the timer interval you should disable the timer in the DownloadFile sub and enable it again at the end of the sub. Like so :-
Private Sub DownloadFile()
Timer1.Enabled = False
My.Computer.Network.DownloadFile("http://google.co.uk/images/someimage.png", "C:/Documents and Settings/All Users/Desktop/someimage.png")
Timer1.Enabled = True
End Sub

Visual Basic adding number loop

I am trying to make an idle game something like cookie clicker, and I am having problems with making a simple line of code that repeats every second and adds 5 to a number every second. Could anyone help me? I want this to start the loop if someone clicks the button.
you could use a timer. Enable/start the timer when the button is clicked.
See example at MSDN: Windows Form Timer
add a timer and in the button code type :
TimerName.start
and add this in the timer code :
TimerName.interval = 1000
'replace TimerName with the name of timer you just added
'this will add 5 to number you want every second , interval of timer = 1000 that means it does the code every second
NumberThatYouWant += 5
'Replace NameThatYouWant with the number name that you want to add 5 to it every second
Yeah create a timer, then set the timer.interval to 1000 to tick each second, then create a sub for timer.tick and put the number you want to be increased in there and that should work.
EG.
Private Sub Timer1_Tick() Handles Timer1.Tick
variable += 5
End Sub
You have to change the interval in the properties window (bottom right)
Hope this helps!
Edit: I didn't include Timer1.start because the other answers said that. Don't forget to use it.
Setting the Timer
First, add a timer control to your form. Set the timer's interval value to '1000' (the timer's interval is measured in milliseconds). You should also enable your timer at run-time:
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Timer1.Enabled = True
End Sub
So your code should look similar to this:
Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
Timer1.Interval = 1000
End Sub
Adding the value with the Timer
Now say the button's name is 'button1', we will now finish the code to add 5 to the button's text property every 1 second, like so:
Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
Timer1.Interval = 1000
Button1.Text += 5
End Sub
This code can also be written as "Button1.Text = Button1.Text + 5" I hope this helps clear up the ambiguity.

How to make a for loop of listbox with a pause between each

Public Class Form1
Dim Iclick, submit
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Iclick.InvokeMember("click")
With WebBrowser1.Document
For l_index As Integer = 0 To ListBox1.Items.Count - 1
Dim l_text As String = CStr(ListBox1.Items(l_index))
.All("input").InnerText = l_text
System.Threading.Thread.Sleep(5000)
Next
'.All("input").InnerText = "http://wordpress.com"
End With
submit.InvokeMember("click")
End Sub
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
WebBrowser1.Navigate("http://whatwpthemeisthat.com")
End Sub
Private Sub WebBrowser1_DocumentCompleted(ByVal sender As System.Object, ByVal e As System.Windows.Forms.WebBrowserDocumentCompletedEventArgs) Handles WebBrowser1.DocumentCompleted
Iclick = WebBrowser1.Document.GetElementById("input")
submit = WebBrowser1.Document.GetElementById("check")
End Sub
End Class
This is my code so far I have a ListBox with URLs I want to check using web browser which theme are they running (if they are wordpress) but the program seems to be bugged when I click START it is NOT responding, until the last element. It has to do something with the system.threading.thread.sleep line but I don't know what am I doing wrong? Thanks.
That's excactly what the Thread.Sleep() method is for. It freezes for 5 seconds.
Also you are replacing the input each time the For loop repeats. So it replaces, waits 5 seconds, replaces, waits 5 sec... and so on.
I guess you are trying to click the submit button for each of the elements, but that's not what you are doing. You have to hit the submit button each time the text has changed. However you really can't be sure about the loading time of the page.
I'd suggest you to place the submit-part inside the loop, then wait, then go into the next iteration. Maybe you could even try to run the website multiple times, one for each listbox item and then apply the right text to the controls in the webpage, hit the button and receive your result.
EDIT: Your best bet for the Thread.Sleep() would be creating a new thread in which you place the loop. This thread can be paused for 5 seconds, and your application will still respond. This is how you create one:
Imports System.Threading.Thread
Dim myThread as Thread
'//..........
'//Button1 is clicked ->
myThread = new Thread (AddressOf myLoop)
myThread.start()
'//..........
Private Sub myLoop ()
'// Loop goes here...
'Sleeping here will only affect the thread that runs this sub. Your form will still be available
End Sub

VB 2008 Periodic Tasks

What's the easiest way to run a periodic task in the background in VB?
For example: Update a label every second with the current time, while still allowing the form to be available.
Add a Timer component to your form
Set its Interval to the time between updates
Enable the timer (by setting its Enabled property or calling its Start method)
Handle its Tick event by doing whatever updates are necessary.
Your handler would look something like this:
Private Sub Timer1_Tick(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles Timer1.Tick
label1.Text = GetCurrentStatus()
End Sub