VB.net Webclient.DownloadFileAsync Not Downloading - vb.net

I am trying to make a download manager for my program. But when I run this code it gives me the message box say "Download Started" but that is it. I do not get any file downloaded or progress bar change? Does anyone know why?
Public Class frmDownloader
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
WebClient1.DownloadFileAsync(New Uri("https://s3.amazonaws.com/MinecraftDownload/launcher/Minecraft_Server.exe"), "C:\hi.exe")
MsgBox("download started")
End Sub
Private Sub WebClient1_DownloadProgressChanged(ByVal sender As Object, ByVal e As System.Net.DownloadProgressChangedEventArgs) Handles WebClient1.DownloadProgressChanged
ProgressBar1.Value = e.ProgressPercentage
MsgBox("Download Progress Changed")
End Sub
End Class

The DownloadProgressChanged method is invoked on a different thread than the one that started the download. Inside this callback you seem to be manipulating some GUI element: ProgressBar1.Value. You should never manipulate GUI elements on different threads than the one on which they were created or you might get an exception. Depending on the type of application you are working on there are different ways to marshal calls on the GUI thread. For example in WinForms you should use the Control.BeginInvoke method. In WPF and Silverlight the equivalent is the Dispatcher.BeginInvoke.

Related

there is a loading cursor in visual studio build program, will not stop

I made a small program ( in visual studio 2013) that simply displays a label in Form1 and a message when closing it. However, when I open it, it has the loading cursor that never stops. Can anyone help me please?
Public Class Form1
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
End Sub
Private Sub PictureBox1_Click(sender As Object, e As EventArgs)
End Sub
Private Sub Form_FormClosing(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosingEventArgs) Handles Me.FormClosing
Dim response As MsgBoxResult
response = MsgBox("Skype must be restarted.", MsgBoxStyle.Exclamation + MsgBoxStyle.OkOnly, "Confirm")
If response = MsgBoxResult.Ok Then
Me.Dispose()
End If
End Sub
End Class
Cause of Error?
Look, I can't clearly say what the error is, but I find, the two main things because of which you are getting the error are:
There is a background task in your computer who is generating this, or, your Visual Studio's Environment is taking too long to load it's features.
The second error may be that, your cursor is set to loading.
Steps to Overcome
You need to go to your form's properties and set the cursor property to default or whatever you want. This can do your work, if you are having the second problem.
You can set different cursor property for different controls.
I hope this helps!

Threading issue in Vb.net

I create vb.net application and i add one windows form,called frmScan.
i put two textboxes and two labels. Then i write the following
thread with delegate event.
Private Delegate Sub DoInitializedDelegate()
Public motdet As New Thread(AddressOf MotionDetection)
Private Sub MotionDetection()
'Do motion detection Work
'It is never ending Loop until form unload.
End Sub
Then I start it in my form load event.
Private Sub frmScan_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
motdet.Start()
End Sub
So my problem started when i load the form.
i can see the form but it is like loading something.
i thought my motion detecting thread is never ending loop until form unload.
i cannot type anything inside two text box i mentions in above.
how should i do ?
Use Application.DoEvents() before calling your long running thread.
Refer example here http://msdn.microsoft.com/en-us/library/aa446540.aspx

using GoogleEarth plugin from VB.NET through FC.GEPluginCtrls

I'm really looking for a simple way to build VB.NET apps that use the GEplugin. So I have found this project that seems to do the dirty job I need: http://code.google.com/p/winforms-geplugin-control-library/
Well, all the code posted there around works on C#, but I need to have it on VB.NET. So I have tried this:
created a new 32-bit solution from VB.NET 2010 Express (I simply added
<PlatformTarget>x86</PlatformTarget >
inside the .vbproj file)
added a reference to FC.GEPluginCtrls.dll
inserted a GeWebBrowser control on the form
at the top of the code, added
Imports FC.GEPluginCtrls
then, in the form Load event, put this code:
InitializeComponent()
GeWebBrowser1.LoadEmbeddedPlugin()
Do
Loop Until GeWebBrowser1.PluginIsReady = False
GeWebBrowser1.CreateInstance(ImageryBase.Earth)
that, I think, would be equivalent to
http://code.google.com/p/winforms-geplugin-control-library/wiki/CreateInstance
So, the project compiles and doesn't get errors, but the GeWebBrowser control remains empty.
I actually wrote the library you are using. You are not listening for the PluginReady event. http://code.google.com/p/winforms-geplugin-control-library/wiki/PluginReady
To use it with VB simply convert the basic examples to VB -
http://code.google.com/p/winforms-geplugin-control-library/wiki/ExampleForm
Also, a loop polling PluginIsReady is totally unnecessary as the PluginReady event is asynchronous.
To show the earth all you would need is the following.
Private Sub Form1_Load( ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
GeWebBrowser1.LoadEmbeddedPlugin()
End Sub
To use the plugin when it has initialzesd use the PluginReady event. Something like.
Option Strict Off
Public Class Form1
Private Dim _ge as Object = Nothing
Private Sub GeWebBrowser1_PluginReady( ByVal sender As System.Object, ByVal e As FC.GEPluginCtrls.GEEventArgs) Handles GeWebBrowser1.PluginReady
_ge = e.ApiObject ' reference to the Google Earth Plugin object
MessageBox.Show(_ge.getApiVersion()) ' _ge is the plugin -use it just as in the javascript api...
End Sub
Private Sub Form1_Load( ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
GeWebBrowser1.LoadEmbeddedPlugin() ' load the plugin
End Sub
End Class

BackgroundWorker causing error when not Startup project

I have a project in VSTO/VB using a BackgroundWorker that works fine. It is a form that calls for a web page of information. The web page can take a while, so I have the form calling with the BackgroundWorker.
I then have an Excel Addin project that has added the BackgroundWorker project. When I call up the form from the Excel Addin project and use the BackgroundWorker to request the web page, it grabs the web page ok. But the work done upon completion, during the BackgroundWorker1_RunWorkerCompleted method, is resulting in an error message:
"Cross-thread operation not valid: Control 'TabPage2' accessed from a thread other than the thread it was created on."
Why does the BackgroundWorker project not work when called from the Excel Addin project?
I note that when I set the BackgroundWorker project as the "Startup Project" there is no error generated. Its something to do with calling this BackgroundWorker project from the Excel Addin project.
Edit: Could be that I am calling RunWorkerAsync() from a non UI thread?
I have an Excel Addin project with a Ribbon class. The Ribbon1.vb has a button click method that creates an instance of the second project, from which the backgroundworker will be called:
Private Sub Btn_Click(ByVal sender As System.Object, ByVal e As Microsoft.Office.Tools.Ribbon.RibbonControlEventArgs) Handles Btn.Click
Dim MySecondProject As SecondProject.Form1 = New SecondProject.Form1()
MySecondProject.Show()
End Sub
MySecondProject is then calling the BackgroundWorker from within its own button click method as:
BackgroundWorker1.RunWorkerAsync()
Then, after it completes, the backgroundworker is trying to update a label in MySecondProject:
Private Sub BackgroundWorker1_RunWorkerCompleted(ByVal sender As System.Object, ByVal e As System.ComponentModel.RunWorkerCompletedEventArgs) Handles BackgroundWorker1.RunWorkerCompleted
SuccessLabel.Text = "Success!"
End Sub
When MySecondProject was the Startup Project, the backgroundworker kept track of the correct thread and updated the label successfully upon completion. With the Excel Addin as the Startup Project, and MySecondProject instantiated during runtime, the backgroundworker seems to lose track of the correct thread. Should I manually be inserting Invoke or BeginInvoke somewhere to help the backgroundworker keep track of the correct thread to update?
So, it turns out that a Backgroundworker launched from a Form launched from a Ribbon cannot update a control at the end of processing. I am not sure why it works from a Form launched as a Startup project while it does not work from a form launched from the Ribbon, but there it is -- turns out that you need to deal with the Backgroundworker losing track of the UI thread.
Using a MethodInvoker works, as in the following snippet:
Imports System.Windows.Forms
Public Class Form1
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
BackgroundWorker1.RunWorkerAsync()
End Sub
Private Sub BackgroundWorker1_DoWork(ByVal sender As System.Object, ByVal e As System.ComponentModel.DoWorkEventArgs) Handles BackgroundWorker1.DoWork
[do nothing]
End Sub
Private myString As String = "This is my string"
Private Sub BackgroundWorker1_RunWorkerCompleted(ByVal sender As System.Object, ByVal e As System.ComponentModel.RunWorkerCompletedEventArgs) Handles BackgroundWorker1.RunWorkerCompleted
If Label1.InvokeRequired Then
Dim mi As MethodInvoker = AddressOf UpdateFormText
Label1.BeginInvoke(mi)
Else
Label1.Text = myString
End If
End Sub
Private Sub UpdateFormText()
Label1.Text = myString + " (BeginInvoked)"
End Sub
End Class
A better answer in VS2010 would use an inline MethodInvoker instead of the second function:
Me.Invoke(CType(Sub() Me.Label1.Text = "This is my string", MethodInvoker))

Windows form closing automatically when parent window gets focus

VB Windows form Application.. I am developing an application of which part of the program is around configuration settings allowing the user to enter configuration items. When the menubar item for configuration menu is clicked on the main form the menu opens. This is fine but the mainform should not become active again until the configuration menu has closed. This does not happen right now and the mainform simply comes to the foreground and the configuration form goes to the background... I realize that coding an event on the Child form to handle this would not work because the child window loses control and the main form gains control.. I thought of coding a function as follows on the main form but it does not seem logical because i would have to add to it for everyform and do checking to make sure the child is actually open before trying to close it..
Private Sub Form1_GotFocus(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.GotFocus
MailSettingsWindow.Close()
RentalSettingsWindow.Close()
End Sub
I did away with the above sub routine and used the below code as per the recomendation of using showdialog which works just as i was looking for.
Private Sub MailingAndEmailSettingsToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MailingAndEmailSettingsToolStripMenuItem.Click
Dim MailConfig As New MailSettingsWindow()
MailSettingsWindow.Showdialog()
End Sub
I did away with the above sub routine and used the below code as per the recommendation of using ShowDialog which works just as I was looking for.
My code is as follows:
Private Sub MailingAndEmailSettingsMenuItem_Click(ByVal sender As Object, ByVal e As EventArgs) Handles MailingAndEmailSettingsMenuItem.Click
Dim MailConfig As New MailSettingsWindow()
MailSettingsWindow.ShowDialog()
End Sub