How do i refresh WebBrowser1 whitout flickering on VB.NET? - vb.net

I call my page every 20 seconds but when i call my page i see a flash. Is it possible to avoid the flickering when the page load?
Private Sub WebBrowser1_load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
WebBrowser1.Navigate(New Uri("http://metalrockpopradio.caramania.com/tuneefy4.php"))
Dim timer As New Timer()
timer.Interval = 20000
AddHandler timer.Tick, AddressOf WebBrowser1.Refresh
timer.Start()

You can do something like this:
Create a Form (here called Form1)
add a PictureBox (here, called PictureBox1) to it
set the PictureBox size to what you want and set its SizeMode = Zoom
paste the following code inside the Form's code partial file (F7 on the Form)
Imports System.Net
Imports System.Windows.Forms
Public Class Form1
Dim timer As System.Windows.Forms.Timer
Private client As WebClient = New WebClient()
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
RefreshImage(Nothing, Nothing)
timer = New System.Windows.Forms.Timer()
AddHandler timer.Tick, AddressOf Me.RefreshImage
timer.Interval = 5000
timer.Start()
End Sub
Protected Sub RefreshImage(sender As Object, e As EventArgs)
Dim HttpResource As String = client.DownloadString("http://metalrockpopradio.caramania.com/tuneefy4.php")
Dim ParseStart As Integer = HttpResource.IndexOf("=") + 2
HttpResource = HttpResource.Substring(ParseStart, HttpResource.LastIndexOf(ChrW(34)) - ParseStart)
If PictureBox1.Image IsNot Nothing Then PictureBox1.Image.Dispose()
PictureBox1.Load(HttpResource)
End Sub
Private Sub Form1_FormClosed(sender As Object, e As FormClosedEventArgs) Handles MyBase.FormClosed
client.Dispose()
timer.Dispose()
End Sub
End Class

Have two of them. When the new reload is done, show the new control and hide the old.
Alternatively, use AJAX or similar on your page to reload content.

Related

How To Auto Refresh Xml page in VB.NET?

Trying to auto refresh xml page with timer but i see the node but the timer does not work. I post my code if you can find the problem thanks a lot in advance.
Private Sub TextBox1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Dim dom As New Xml.XmlDocument
dom.Load("http://69.175.13.131:8050/stats")
TextBox1.Clear()
Dim monitorid As String = String.Empty
For Each node As Xml.XmlNode In dom.SelectNodes("//SHOUTCASTSERVER/SONGTITLE")
monitorid = node.InnerText
TextBox1.Text=(monitorid)
Next
Dim timer As New Timer()
timer.Interval = 20000
AddHandler timer.Tick, AddressOf TextBox1.Refresh
timer.Start()
End Sub
The Control.Refresh method is only to redraw the control, not update its contents (unless you write code in an unexpected place to do that).
If you put the code to retrieve the data in a separate method, you can make it a bit tidier and easier to see what is happening in the code. Something like:
Public Class Form1
Dim tim As Timer
Sub ShowCurrentSongTitle(sender As Object, e As EventArgs)
Dim dom As New Xml.XmlDocument
dom.Load("http://69.175.13.131:8050/stats")
Dim node As Xml.XmlNode = dom.SelectSingleNode("//SHOUTCASTSERVER/SONGTITLE")
Dim monitorid As String = node.InnerText
TextBox1.Text = monitorid
TextBox1.SelectionStart = monitorid.Length
End Sub
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
ShowCurrentSongTitle(Nothing, EventArgs.Empty)
tim = New Timer With {.Interval = 20000}
AddHandler tim.Tick, AddressOf ShowCurrentSongTitle
tim.Start()
End Sub
Private Sub Form1_FormClosing(sender As Object, e As FormClosingEventArgs) Handles MyBase.FormClosing
' tidy up...
RemoveHandler tim.Tick, AddressOf ShowCurrentSongTitle
End Sub
End Class

Auto refresh form every 30 seconds vb.net

I want to have a form opened all day but I want to auto refresh it every 30 seconds.
I am using this code:
Private Sub tempo(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
Dim timer = New Timer
timer.Interval = 30 * 1000
AddHandler timer.Tick, AddressOf Form12_Load
timer.Start()
End Sub
Private Sub Form12_Load(sender As Object, e As EventArgs) Handles MyBase.Load
...
End sub
But it isn't working. Do you know what I am doing wrong?
Thank you.
My 0.02, should not need much explanation:
Private WithEvents clock As New Timers.Timer
Private Sub Form12_Load(sender As Object, e As EventArgs) Handles MyBase.Load
With clock
.Interval = 30000
.AutoReset = True
.Enabled = True
.Start()
End With
End Sub
Private Sub clock_tick() Handles clock.Elapsed
Me.BeginInvoke(Sub()
Me.Refresh()
End Sub)
End Sub
Firsly you need to take timer. after that you select time interval. how much time you want to refresh from load page. in my case im creating one function form load in side formload function is nothing you just type your commands. want to update on load page ===**
MyBase.Update()** im using that.
'Private Sub Timer3_Tick(sender As Object, e As EventArgs) Handles Timer3.Tick
' formload()
' ref_screen()
'End Sub
Hi you have to call the form_load event from the tick-timer event:
Private Sub Form12_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Dim timer = New Timer
timer.Interval = 30 * 1000
AddHandler timer.Tick, AddressOf timer_Tick
timer.Start()
End sub
Private Sub timer_Tick(ByVal sender As Object, ByVal e As EventArgs)
Form12_Load(me,nothing)
End Sub

Closing application after X mins of inactivity in Visual Basic

I have an application that I need to close after it is inactive or not used. I have written the below code and am getting
System.InvalidOperationException in windowsBase.dll
Do you have any idea how I can get this done? I need the application not to "freeze" using some of the threading timers.
Private Sub MainWindow_Loaded(sender As Object, e As RoutedEventArgs) Handles Me.Loaded
Dim aTimer As System.Timers.Timer
aTimer = New System.Timers.Timer()
aTimer.Interval = 9000
' Hook up the Elapsed event for the timer.
AddHandler aTimer.Elapsed, AddressOf OnTimedEvent
' Start the timer
aTimer.Enabled = True
End Sub
Private Sub OnTimedEvent(source As Object, e As System.Timers.ElapsedEventArgs)
Close()
End Sub
Try :
Dim app as Application
Private Sub OnTimedEvent(source As Object, e As System.Timers.ElapsedEventArgs)
app.Exit()
End Sub
That seems to have worked for me.
Here is my full code on a blank form I made to test your issue. I get the smae exception you get trying to use Me.Close() but my Application.Exit() and app.Exit() work without a problem.
Public Class Form1
Dim app As Application
Private Sub MainWindow_Loaded(sender As Object, e As EventArgs) Handles Me.Load
Dim aTimer As System.Timers.Timer
aTimer = New System.Timers.Timer()
aTimer.Interval = 5000
' Hook up the Elapsed event for the timer.
AddHandler aTimer.Elapsed, AddressOf OnTimedEvent
' Start the timer
aTimer.Enabled = True
End Sub
Private Sub OnTimedEvent(source As Object, e As System.Timers.ElapsedEventArgs)
'Environment.Exit(0) - closes without a problem
'Application.Exit() - closes without a problem
'closes without a problem.
app.Exit()
'Me.Close() - System.InvalidOperationException
End Sub
End Class

VB.NET Create and Close Forms after Certain Time

I could easily close form after few seconds; but when I want to close many forms one after another, same sequence as they were "created"; I could not figure it out:
The main form code is as below:
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim numberOfForms As Integer = 10
For open = 1 To numberOfForms
TestClosing()
Next
End Sub
The module code that I am trying to create then close forms after few seconds is as below:
Imports System.Timers
Module ClosingModule
Sub TestClosing()
Dim frmNew As New Form
frmNew.Show()
Dim tmr As New System.Timers.Timer()
tmr.Interval = 3000
tmr.Enabled = True
tmr.Start()
End Sub
End Module
I started a timer, but all the methods I tried to close the form is same sequence they were created; were not successful;
Help appreciated; and thanks in advance.
Add the Timer to the Forms you are creating, start it when the Form is created that way they will be closed in the same order that they were created. I also added an incremental delay to that the order of closing is more evident.
Form1
Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
Dim numberOfForms As Integer = 10
For open = 1 To numberOfForms
Dim frmNew As New Form2
frmNew.Text = open.ToString
frmNew.Show()
Next
End Sub
Form2
Public Class Form2
Dim myTimer As New Timer()
Private Sub myTimer_Tick(sender As System.Object, e As System.EventArgs)
myTimer.Stop()
Me.Close()
End Sub
Private Sub Form2_Shown(sender As Object, e As System.EventArgs) Handles Me.Shown
myTimer.Interval = 1000 * CInt(Me.Text)
AddHandler myTimer.Tick, AddressOf myTimer_Tick
myTimer.Start()
End Sub
End Class
Added code in module to do the same thing:
Imports System.Timers
Module ClosingModule
Sub TestClosing(multiplier As Integer)
Dim frmNew As New Form
frmNew.Show()
Dim tmr As New System.Timers.Timer()
AddHandler tmr.Elapsed, AddressOf Timer_Elapsed 'Add Handler to New Timer
tmr.SynchronizingObject = frmNew 'Synchronize Timer to newly created form
tmr.Interval = 1000 * multiplier
tmr.Enabled = True
tmr.Start()
End Sub
Public Sub Timer_Elapsed(sender As Object, e As ElapsedEventArgs)
Dim tmr As System.Timers.Timer = DirectCast(sender, System.Timers.Timer)
tmr.Stop() 'Stop Timer
DirectCast(tmr.SynchronizingObject, Form).Close() 'Get Form Timer was synchronized with and close it
tmr.SynchronizingObject = Nothing 'Remove Form reference from timer
RemoveHandler tmr.Elapsed, AddressOf Timer_Elapsed 'Remove Handler from Timer
End Sub

Getting web page after calling DownloadStringAsync()?

I don't know enough about VB.Net yet to use the richer HttpWebRequest class, so I figured I'd use the simpler WebClient class to download web pages asynchronously (to avoid freezing the UI).
However, how can the asynchronous event handler actually return the web page to the calling routine?
Imports System.Net
Public Class Form1
Private Shared Sub DownloadStringCallback2(ByVal sender As Object, ByVal e As DownloadStringCompletedEventArgs)
If e.Cancelled = False AndAlso e.Error Is Nothing Then
Dim textString As String = CStr(e.Result)
'HERE : How to return textString to the calling routine?
End If
End Sub
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim client As WebClient = New WebClient()
AddHandler client.DownloadStringCompleted, AddressOf DownloadStringCallback2
Dim uri As Uri = New Uri("http://www.google.com")
client.DownloadStringAsync(uri)
'HERE : how to get web page back from callback function?
End Sub
End Class
Thank you.
Edit: I added a global, shared variable and a While/DoEvents/EndWhile, but there's got to be a cleaner way to do this :-/
Public Class Form1
Shared page As String
Public Shared Sub AlertStringDownloaded(ByVal sender As Object, ByVal e As DownloadStringCompletedEventArgs)
' If the string request went as planned and wasn't cancelled:
If e.Cancelled = False AndAlso e.Error Is Nothing Then
page = CStr(e.Result)
End If
End Sub
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim wc As New WebClient
AddHandler wc.DownloadStringCompleted, AddressOf AlertStringDownloaded
page = Nothing
wc.DownloadStringAsync(New Uri("http://www.google.com"))
'Better way to wait until page has been filled?
While page Is Nothing
Application.DoEvents()
End While
RichTextBox1.Text = page
End Sub
End Class
You can set the RichTextBox1.Text directly in the completed handler if you make the handler function an instance method instead of shared.
Public Class Form1
Private Sub AlertStringDownloaded(ByVal sender As Object, ByVal e As DownloadStringCompletedEventArgs)
' If the string request went as planned and wasn't cancelled:
If e.Cancelled = False AndAlso e.Error Is Nothing Then
RichTextBox1.Text = CStr(e.Result)
End If
End Sub
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim wc As New WebClient
AddHandler wc.DownloadStringCompleted, AddressOf AlertStringDownloaded
page = Nothing
wc.DownloadStringAsync(New Uri("http://www.google.com"))
End Sub
End Class
You haven't found a clean way. Close your form while the download is taking place and behold the kaboom you'll get. You'll have to at least set the form's Enabled property to False.
Look at the BackgroundWorker class to do this cleanly.