|
-
Jan 4th, 2010, 12:58 AM
#1
Thread Starter
Member
Progress Bar Marquee
Hello all,
I have a problem (actually I am using VB 2008, but the code is the same, so should not be a problem), here is the situation:
First, I have to download one file
Second, I have to do another job (which I cannot calculate the progress of)
Third, I have to download another file
For downloading files I know how to obtain the progress of the download, so I have a progress bar to display it, however when it's doing the other job, since I cannot calculate it's progress, I'd like the progress bar to switch to Marquee style and back when the third job starts. However I never saw any trace of the Marquee becoming active, here is my code:
Code:
If StartDownload(txtURL.Text, GetTempFolder() & "\BTV" & GetVideoName(txtURL.Text) & ".htm") = True Then
System.Threading.Thread.Sleep(1000)
prgCurFile.Style = ProgressBarStyle.Marquee
prgCurFile.Refresh()
Application.DoEvents()
After that code, I continue downloading the next file, please help!
Thanks in advance for any possible answers!
-
Jan 4th, 2010, 03:21 AM
#2
Re: Progress Bar Marquee
I'm not sure I understand your code completely. Is StartDownload a synchronous operation that returns when the file has finished downloading? If so, is it returning false? If StartDownload is beginning an asynchronous operation and returning immediately, then could it be that it your code is setting the style to marquee, after which it is being reset immediately to a regular progress bar from your other thread?
-
Jan 4th, 2010, 04:20 AM
#3
Thread Starter
Member
Re: Progress Bar Marquee
The StartDownload method starts downloading the first file and returns control when it's finished downloading, at which point the progress bar should turn to Marquee. As you can see I tried putting prgCurFile.refresh and application.doevents, but it still doesn't work!
-
Jan 4th, 2010, 06:47 AM
#4
Re: Progress Bar Marquee
Alright, have you checked to make sure that code block is being executed? What is the code being executed right after that block? What is the code executed in the StartDownload method?
-
Jan 4th, 2010, 08:04 AM
#5
Thread Starter
Member
Re: Progress Bar Marquee
The code in StartDownload starts downloading the specified file, it uses a class I downloaded which downloads files showing progress. Anyhow here is the code in StartDownload:
Code:
Try
DLMDownl = New WebFileDownloader
StartDownload = DLMDownl.DownloadFileWithProgress(URL, Dest)
Catch ex As Exception
MsgBox("There was an error while downloading the file " & URL & "! The error message is: " & ex.Message & "! Please make sure the URL you specified is valid!", MsgBoxStyle.Critical, "Download Error!")
Shell(Application.StartupPath & "\Delay.exe " & Application.ExecutablePath, AppWinStyle.NormalFocus)
Me.Close()
End Try
The block of code is being executed all right since it starts downloading a 7 MB .flv video and the video gets fully downloaded (I have been able to watch it fully). I have had similar issues when trying to show in a text box a log of operations being done but the text would not appear as it was added, but it would all appear together when everything was done! I have thought this might be the problem, but normally using prgCurFile.Refresh() should fix it no?
-
Jan 4th, 2010, 09:11 AM
#6
Re: Progress Bar Marquee
This is not completely related to your post, but your functions MsgBox and Shell are legacy and should not be used. Take a look at MessageBox and Process.
Other than that, what is the StartDownoad variable in your try block? Is it a boolean, an IAsyncResult?
-
Jan 4th, 2010, 11:37 AM
#7
Thread Starter
Member
Re: Progress Bar Marquee
StartDownload returns a boolean indicating whether the download was successful or not, in case it was not then the whole operation will be aborted!
Also, how come shouldn't I use the MsgBox and Shell functions? It's good that you informed me of that!
-
Jan 4th, 2010, 01:36 PM
#8
Re: Progress Bar Marquee
They are legacy functions, supported for backward compatibility but not recommended. Use framework classes and methods whenever possible 
Oh I see. I forgot in VB that you can actually assign a function a value within itself. Very confusing, normally I always declare variables within my function to store values.
I am pretty sure I know what your problem is. It isn't the code that you've shown, it's teh code that you haven't shown. Your Application.DoEvents() does indeed give the UI a a chance to set the progress bar style to Marquee, but i'm guessing that the code you execute directly afterwards is another synchronous download, i.e. you download directly without declaring background workers etc.
The thing about synchronous operations is that they block the calling thread until they are finished. In this case, you are blocking the UI thread. Blocking the UI thread means that it cannot refresh your form. The Marquee progress bar is just an animation, which means that the UI is redrawing the bar over and over again. Or wants to. But it can't because it is waiting for your download to complete. So it never gets refreshed and it looks like it was never set to marquee.
Downloading, complex operations and other such time-consuming code should be handled on another thread. I would suggest that you take a look at the BackgroundWorker.
-
Jan 4th, 2010, 11:50 PM
#9
Thread Starter
Member
Re: Progress Bar Marquee
Well, I will try declaring a background worker and see if it solves my problem, however in case you need it for further examining, here is the full code of that procedure (where I set the progress bar to marquee and back)
Code:
Private Sub btnDownload_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnDownload.Click
Try
btnPaste.Enabled = False
txtURL.Enabled = False
btnDownload.Enabled = False
If StartDownload(txtURL.Text, GetTempFolder() & "\BTV" & GetVideoName(txtURL.Text) & ".htm") = True Then
System.Threading.Thread.Sleep(1000)
prgCurFile.Style = ProgressBarStyle.Marquee
prgCurFile.Refresh()
Application.DoEvents()
Dim UrlF1 As String
UrlF1 = GetURLFromFile(GetTempFolder() & "\BTV" & GetVideoName(txtURL.Text) & ".htm")
If UrlF1 <> "" Then
prgCurFile.Style = ProgressBarStyle.Continuous
prgCurFile.Value = 0
If StartDownload(UrlF1, GetTempFolder() & "\Video" & GetVideoName(txtURL.Text) & ".flv") Then
System.Threading.Thread.Sleep(1000)
Shell(Application.StartupPath & "\ffmpeg.exe -i " & """" & GetTempFolder() & "\Video" & GetVideoName(txtURL.Text) & ".flv" & """" & " " & """" & GetTempFolder() & "\Video" & GetVideoName(txtURL.Text) & ".mpg" & """", AppWinStyle.NormalFocus, True, -1)
SFD.ShowDialog()
Dim UserDefDest As String
UserDefDest = SFD.FileName
My.Computer.FileSystem.MoveFile(GetTempFolder() & "\Video" & GetVideoName(txtURL.Text) & ".flv", UserDefDest & ".flv", FileIO.UIOption.AllDialogs, FileIO.UICancelOption.ThrowException)
My.Computer.FileSystem.MoveFile(GetTempFolder() & "\Video" & GetVideoName(txtURL.Text) & ".mpg", UserDefDest & ".mpg", FileIO.UIOption.AllDialogs, FileIO.UICancelOption.ThrowException)
My.Computer.FileSystem.DeleteDirectory(GetTempFolder, FileIO.DeleteDirectoryOption.DeleteAllContents)
If MsgBox("The video has been downloaded with success!" & vbCrLf & "Would you like to download more videos?", MsgBoxStyle.Information + MsgBoxStyle.YesNo, "Download Complete") = MsgBoxResult.Yes Then
Shell(Application.StartupPath & "\Delay.exe " & Application.ExecutablePath, AppWinStyle.NormalFocus)
End If
Me.Close()
Else
MsgBox("Unable to download video! Please make sure that the URL you entered is valid and that it belongs to a Blip TV video (YouTube is not supported)!", MsgBoxStyle.Critical, "Download Error!")
Shell(Application.StartupPath & "\Delay.exe " & Application.ExecutablePath, AppWinStyle.NormalFocus)
Me.Close()
End If
Else
MsgBox("Unable to find video download URL! Please make sure that the URL you entered is valid and that it belongs to a Blip TV video (YouTube is not supported)!", MsgBoxStyle.Critical, "Download Error!")
Shell(Application.StartupPath & "\Delay.exe " & Application.ExecutablePath, AppWinStyle.NormalFocus)
Me.Close()
End If
Else
MsgBox("Unable to download video web page! Please make sure that the URL you entered is valid!", MsgBoxStyle.Critical, "Download Error!")
Shell(Application.StartupPath & "\Delay.exe " & Application.ExecutablePath, AppWinStyle.NormalFocus)
Me.Close()
End If
Catch ex As Exception
MsgBox("There was an error while downloading the video! The error message is: " & ex.Message & "! Please make sure the URL you specified is valid!", MsgBoxStyle.Critical, "Download Error!")
Shell(Application.StartupPath & "\Delay.exe " & Application.ExecutablePath, AppWinStyle.NormalFocus)
Me.Close()
End Try
End Sub
As you can see there are a lot of Shell and MsgBox functions which I will also correct.
-
Jan 5th, 2010, 04:55 AM
#10
Re: Progress Bar Marquee
What I see is that there is no reason for you to be using a marquee in the first place. How long does it take for your GetURLFromFile function to run?
-
Jan 5th, 2010, 06:09 AM
#11
Thread Starter
Member
Re: Progress Bar Marquee
Actually you are kind of right, the reason why I wanted it was to make the program look better and make it clear to the user that the program is not just doing nothing. But if I think of it better actually now I also think it doesn't really matter. The GetURLFromFile function takes just a few seconds as it reads the first downloaded file and looks for a specific url which will be downloaded soon after. Thanks a lot for the help though, and sorry for having bothered.
-
Jan 5th, 2010, 06:30 AM
#12
Re: Progress Bar Marquee
No problem, anything you learn can be used for other projects. That's what makes this forum so great happy coding! If you consider this matter resolved, then by all means mark it as such using the thread tools at the top of the page.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|