IMAPI2 in VB 2008 progress bar - vb.net

I start making program that will burn CD/DVDs, and everything is okay.
I found way to burn with IMAPI2 API, but now I have problem:
I can't get progress bar of that burning.
Here is code:
Dim CDD1 As New IMAPI2.MsftDiscMaster2
Dim CDD2 As New IMAPI2.MsftDiscRecorder2
Dim FSI As New IMAPI2FS.MsftFileSystemImage
Dim CDD3 As New IMAPI2.MsftDiscFormat2Data
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim Index = 0
Dim UniqueID = ""
Dim Directory
Dim Path = "C:\lll"
Dim result
Dim Stream
Label1.Text = "----- Started -----."
UniqueID = CDD1.Item(Index)
Label1.Text = Label1.Text & vbCrLf & "ID found: " & UniqueID
CDD2.InitializeDiscRecorder(UniqueID)
Label1.Text = Label1.Text & vbCrLf & "Recorder selected!"
Directory = FSI.Root
Label1.Text = Label1.Text & vbCrLf & "Directory is here: " & Directory.ToString
CDD3.Recorder = CDD2
Label1.Text = Label1.Text & vbCrLf & "Recorder 2 selected!"
CDD3.ClientName = "IMAPI2 TEST"
Label1.Text = Label1.Text & vbCrLf & "Client Name Selected!"
FSI.ChooseImageDefaults(CDD2)
Label1.Text = Label1.Text & vbCrLf & "Default selected!"
Directory.AddTree(Path, False)
Label1.Text = Label1.Text & vbCrLf & "Directory added!"
result = FSI.CreateResultImage()
Stream = result.ImageStream
Label1.Text = Label1.Text & vbCrLf & "Writing content to disc..."
If (CDD3.IsCurrentMediaSupported(CDD2) = True) Then
If (CDD3.IsRecorderSupported(CDD2) = True) Then
CDD3.Write(Stream)
Else
MsgBox("Not Suported Recorder!")
End If
Else
MsgBox("Not Suported Media!")
End If
Label1.Text = Label1.Text & vbCrLf & "----- Finished -----"
End Sub
When command
CDD3.Write(Stream)
is triggered, program freeze, and don't respond until data is burned completely.
Is there any way to stop this, to stop program freezing and enabling progress bar?
Thanks.

You need to use threading. So in your button click event handler you start off a new thread that does the actual burning and while that's going on in it's separate thread, the main thread can continue to update the GUI (including your progress bar).
See Thread.Start for a simple sample and if you want further information I'd suggest starting here: Managed Threading

Related

How to show download progress in progressbar? Visual Basic

im so confused because this code didn't work. It downloaded the file successfully but don't report the progress to the ProgressBar. I already started Timer1 using Timer1.Start() before BackgroundWorker2.RunWorkerAsync() .
Dim size As Double
Private Sub BackgroundWorker2_DoWork(sender As Object, e As System.ComponentModel.DoWorkEventArgs) Handles BackgroundWorker2.DoWork
Try
Dim G As Integer = 150
Dim Increase As Boolean = True
Do Until Clicked = True
If Increase = True Then
If Not G = 255 Then
G += 1
Threading.Thread.Sleep(10)
Else
Increase = False
End If
Else
If Not G = 150 Then
G -= 1
Threading.Thread.Sleep(10)
Else
Increase = True
End If
End If
Label6.ForeColor = Color.FromArgb(0, G, 0)
Loop
Label6.Cursor = Cursors.Default
Label6.Text = "Initializing"
Label6.ForeColor = Color.Lime
MessageBox.Show("Description :" & Environment.NewLine & Description & Environment.NewLine & Environment.NewLine & "Total Size: " & Environment.NewLine & TotalSize & Environment.NewLine & Environment.NewLine & "Download Link (Global): " & Environment.NewLine & DownlaodLink, "BIOS Update Information", MessageBoxButtons.OK, MessageBoxIcon.Information)
'WebBrowser1.Navigate(DownlaodLink)
'BackgroundWorker1.RunWorkerAsync()
ProgressBar1.Visible = True
size = TotalSize.Replace(" MBytes", "")
Me.Refresh()
Dim wc As New WebClient
wc.DownloadFileAsync(New Uri(DownlaodLink), My.Computer.FileSystem.SpecialDirectories.Desktop & "\A55BM-E BIOS " & LatestVersion.ToString.Replace(" ", "") & ".zip")
Catch ex As Exception
MsgBox(ex.Message)
End Try
And the code to show me the progress of my download
Dim cursize As Double
Dim finsize As Double
Private Sub Timer1_Tick(sender As Object, e As EventArgs) Handles Timer1.Tick
If System.IO.File.Exists(My.Computer.FileSystem.SpecialDirectories.Desktop & "\A55BM-E BIOS " & LatestVersion.ToString.Replace(" ", "") & ".zip") Then
cursize = My.Computer.FileSystem.GetFileInfo(My.Computer.FileSystem.SpecialDirectories.Desktop & "\A55BM-E BIOS " & LatestVersion.ToString.Replace(" ", "") & ".zip").Length
finsize = cursize / size * 100
If Not ProgressBar1.Value = ProgressBar1.Maximum Then
ProgressBar1.Value = finsize
ProgressBar1.Refresh()
Else
ProgressBar1.Value = finsize
ProgressBar1.Refresh()
Timer1.Stop()
MsgBox("Finished Downloading")
End If
End If
End Sub
I can't figure out how to make this work. Can someone help me?
Finally! I made it work but didn't go with the BackgroundWorker. The code below is what I've used to make this thing work. And it's so efficient and easy to use too.
Public WithEvents downloader As WebClient
Public Sub DownloadStart()
Label6.Cursor = Cursors.Default
Label6.Text = "Initializing"
Label6.ForeColor = Color.Lime
MessageBox.Show("Description :" & Environment.NewLine & Description & Environment.NewLine & Environment.NewLine & "Total Size: " & Environment.NewLine & TotalSize & Environment.NewLine & Environment.NewLine & "Download Link (Global): " & Environment.NewLine & DownlaodLink, "BIOS Update Information", MessageBoxButtons.OK, MessageBoxIcon.Information)
ProgressBar1.Visible = True
downloader = New WebClient
downloader.DownloadFileAsync(New Uri(DownlaodLink), My.Computer.FileSystem.SpecialDirectories.Desktop & "\A55BM-E BIOS " & LatestVersion.ToString.Replace(" ", "") & ".zip")
End Sub
Private Sub downloader_DownloadProgressChanged(sender As Object, e As DownloadProgressChangedEventArgs) Handles downloader.DownloadProgressChanged
ProgressBar1.Value = e.ProgressPercentage
End Sub
Thanks everyone for helping me!

Form is sent to back after function runs

I've got the following section of code:
Public Sub SendTestEmail()
Try
Dim Mail As New MailMessage
Mail.Subject = "Test email"
Mail.To.Add(smtpTXTsendto.Text)
Mail.From = New MailAddress(smtpTXTusername.Text)
Mail.Body = "This is a test message"
Dim SMTP As New SmtpClient(smtpTXTserver.Text)
If smtpCHECKssl.Checked = True Then
SMTP.EnableSsl = True
Else
SMTP.EnableSsl = False
End If
SMTP.Credentials = New System.Net.NetworkCredential(smtpTXTusername.Text, smtpTXTpassword.Text)
SMTP.Port = smtpTXTport.Text
SMTP.Send(Mail)
MessageBox.Show("A test email has been sent." & Environment.NewLine & Environment.NewLine & "To: " & smtpTXTsendto.Text & Environment.NewLine & "From: " & smtpTXTusername.Text & "." & Environment.NewLine & Environment.NewLine & "If you did not recieve an email, please check your settings and try again.", "Test Email")
Catch ex1 As Exception
MessageBox.Show(ex1.Message)
Return
End Try
End Sub
The Sub SendTestEmail is called inside of a Background worker.
The odd issue I'm having, as that when a MessageBox appears, and I click OK the form gets sent to the back of the screen, behind all applications ...
I've tried adding a Me.focus, but it gives me issues about Cross Tread violations.
Any ideas why this is happening?
Background Worker Code:
Private Sub BGWSendTestEmail_DoWork(ByVal sender As System.Object, ByVal e As System.ComponentModel.DoWorkEventArgs) Handles BGWSendTestEmail.DoWork
SendTestEmail()
End Sub
Private Sub BGWSendTestEmail_RunWorkerCompleted(ByVal sender As System.Object, ByVal e As System.ComponentModel.RunWorkerCompletedEventArgs) Handles BGWSendTestEmail.RunWorkerCompleted
If (e.Cancelled) Then
MsgBox("Something went wrong!")
Else
GroupBoxTesting.Visible = False
Me.Enabled = True
End If
End Sub
Private Sub SMTPButtonTest_Click_1(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles SMTPButtonTest.Click
GroupBoxTesting.Visible = True
Me.Enabled = False
BGWSendTestEmail.RunWorkerAsync()
End Sub
Change BGWSendTestEmail_RunWorkerCompleted to something like this and remove exception handling from SendTestEmail
Me.Enabled = True ' Always enable form first after completed
If e.Error Is Nothing Then
If Not e.Cancelled Then
MessageBox.Show("A test email has been sent." _
& Environment.NewLine _
& Environment.NewLine _
& "To: " & smtpTXTsendto.Text _
& Environment.NewLine _
& "From: " & smtpTXTusername.Text _
& "." & Environment.NewLine _
& Environment.NewLine _
& "If you did not recieve an email, please check your settings and try again.", _
"Test Email")
GroupBoxTesting.Visible = False 'Maybe put this also into start of method?
Else
MsgBox("Something went wrong!")
End If
Else
MessageBox.Show("Email sending failed. Exception: " & e.Error.Message)
End If
If you debug this program your debugger will attach exceptions in do_work method, but if you run it without debugger then exceptions are handled at completed method.

Making checked boxes put multiline text into a label

So I have 4 check boxes and if one of them is checked, I would like to put that text into a label.
My problem is when one box is checked, it just has that text in the label but not the other checked ones. I want it to have a new line for each box that is checked to put new line text into the label.
How would I be able to have the program go through the check boxes, and if they are checked put that text into a label and for the next box checked start a new line and put that text there?
To be put under a button click event.
Quick and dirty, but if I understand what you are asking for this should work.
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Label1.Text = ""
If (CheckBox1.Checked) Then
Label1.Text &= CheckBox1.Text & vbCrLf
End If
If (CheckBox2.Checked) Then
Label1.Text &= CheckBox2.Text & vbCrLf
End If
If (CheckBox3.Checked) Then
Label1.Text &= CheckBox3.Text & vbCrLf
End If
If (CheckBox4.Checked) Then
Label1.Text &= CheckBox4.Text & vbCrLf
End If
End Sub
Put all your CheckBoxes in the same container. In this example, they are all contained within Panel1. This gets all the Text properties of the CheckBoxes which are checked, and puts the Text in Label1.Text. When nothing is selected, an Exception is thrown (Sequence contains no elements).
Try
Label1.Text = Panel1.Controls.OfType(Of CheckBox). _
Where(Function(arg) arg.Checked). _
Select(Function(arg) arg.Text). _
Aggregate(Function(aggregate, nextItem) aggregate & Environment.NewLine & nextItem)
Catch ex As Exception
Label1.Text = "Nothing selected"
End Try
Try this code,
[Note: Tested with IDE]
Private Sub Common_Cheked_Change_Handler(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles _
CheckBox1.CheckedChanged, CheckBox2.CheckedChanged, _
CheckBox3.CheckedChanged, CheckBox4.CheckedChanged
Label1.Text = String.Empty
Label1.Text = If(CheckBox1.Checked = True, CheckBox1.Text & vbCrLf, String.Empty) & _
If(CheckBox2.Checked = True, CheckBox2.Text & vbCrLf, String.Empty) & _
If(CheckBox3.Checked = True, CheckBox3.Text & vbCrLf, String.Empty) & _
If(CheckBox4.Checked = True, CheckBox4.Text, String.Empty)
End Sub

vb.net Searching for specific lines within large .log files

I'm reading from 1-5mb log files outputting to a textview and also searching for specific lines outputting to another textview. Currently it takes about a minute for just a 1mb file. Does anyone know any faster methods of searching through lines or strings other than the method I'm using?
Imports EnterpriseDT.Net.Ftp
Public Class Form1
Private Sub SettingsToolStripMenuItem_Click(sender As System.Object, e As System.EventArgs) Handles SettingsToolStripMenuItem.Click
End Sub
Private Sub Button2_Click(sender As System.Object, e As System.EventArgs) Handles Button2.Click
Dim sw As New Stopwatch
Dim FullLine As String = ""
Dim ScriptLine As String = ""
sw.Start()
Dim ll As New Queue(Of String)
Dim i As String = ""
Using TestFile As New IO.StreamReader("c:\test.txt", System.Text.Encoding.Default, False, 4096)
Using OutFile As New IO.StreamWriter("c:\SBOutFile.txt", False, System.Text.Encoding.Default, 4096)
While TestFile.EndOfStream = False
i = TestFile.ReadLine
If i.Contains(".sqf") And i.Contains("handleGear.sqf") = False Then
ScriptLine = ScriptLine & i & vbNewLine & vbNewLine
FullLine = FullLine & i & vbNewLine & vbNewLine
Else
FullLine = FullLine & i & vbNewLine & vbNewLine
End If
End While
End Using
End Using
sw.Stop()
TextBox1.Text = FullLine
TextBox2.Text = ScriptLine
RichTextBox1.AppendText(String.Format("Run_Queue took {0} Milliseconds." & Environment.NewLine, sw.ElapsedMilliseconds))
End Sub
Private Sub Button1_Click_1(sender As System.Object, e As System.EventArgs) Handles Button1.Click
Try
'connect to ftp server
Dim ftp As New FTPConnection
ftp.ServerAddress = "-"
ftp.ServerPort = "-"
ftp.UserName = "-"
ftp.Password = "-"
ftp.Connect()
ftp.ChangeWorkingDirectory("-")
ftp.TransferType = FTPTransferType.BINARY
'download a file
ftp.DownloadFile("c:\test.txt", "scripts.log")
'ftp.RenameFile("scripts.log", "scripts_test.log")
'close the connection
ftp.Close()
Catch ex As Exception
MessageBox.Show(ex.Message.ToString())
End Try
End Sub
Private Sub Button3_Click(sender As System.Object, e As System.EventArgs) Handles Button3.Click
End Sub
End Class
Given the heavy reading and concatenating you are doing as you're reading, I suspect its part of your time/performance issue. I would probably consider changing the declarations for ScriptLine and FullLine from String type to a StringBuilder, because Strings are technically immutable. That means each concatenation really turns out to be teardown of the previous object, and the creation of a new one in its place. StringBuilders are designed specifically for heavy concatenation scenarios. When the looping is finished, you can convert it back to a String.
Also, a compiled Regular Expression might search faster than String.Contains. Your regular expression string would be something like "(?!handleGear).sqf", meaning "find any sequence of zero or more characters other than "handleGear" in front of the string ".sqf".
I haven't had a chance to test that expression, so it is offered with that caveat. If I get a chance to throw together a test, I'll be glad to amend and let you know.
Good luck!
I just wanted to post what I came up with in the end. It was a very large improvement to what I had.
'Read file
Dim sw As New Stopwatch
Dim FullLine As String = ""
Dim ScriptLine As String = ""
sw.Start()
Dim ll As New Queue(Of String)
Dim i As String = ""
Dim builder As New StringBuilder
Using TestFile As New IO.StreamReader("c:\test.txt", System.Text.Encoding.Default, False, 4096)
builder.AppendLine("Started at: " & DateTime.Now.ToLongTimeString().ToString)
RichTextBox1.AppendText(Now.ToShortTimeString & " Reading Log File Started" & vbNewLine)
RichTextBox1.SelectionStart = RichTextBox1.TextLength
Dim rowCount As Integer = 0
Do Until TestFile.EndOfStream
ScriptLine = TestFile.ReadLine
ScriptLine = LCase(ScriptLine)
If InStr(ScriptLine, ".sqf") > 0 And InStr(ScriptLine, "handlegear.sqf") < 1 Then 'And InStr(ScriptLine, "createmarkerlocal.sqf") < 1 And InStr(ScriptLine, "setmarkerposlocal.sqf") < 1
builder.AppendLine(ScriptLine)
builder.AppendLine()
End If
rowCount = rowCount + 1
Loop
builder.AppendLine(Now.ToShortTimeString & "==== Searched " & rowCount & " rows" & vbNewLine)
builder.AppendLine(Now.ToShortTimeString & " Finished" & vbNewLine)
End Using
sw.Stop()
RichTextBox2.AppendText(builder.ToString & vbNewLine)
RichTextBox1.AppendText(Now.ToShortTimeString & String.Format(" Run_Queue took {0} Milliseconds." & Environment.NewLine, sw.ElapsedMilliseconds))

Backgroundworker doesn't work... VB.Net

this is my code:
Private Sub BackgroundWorker1_DoWork(ByVal sender As System.Object, ByVal e As System.ComponentModel.DoWorkEventArgs) Handles BackgroundWorker1.DoWork
For i = 0 To 1000
Dim inum As String = i & "0"
Dim request As System.Net.HttpWebRequest = System.Net.HttpWebRequest.Create("http://www.google.nl/search?q=site:" & combobox1.Text & "&hl=nl&start=" & inum)
Dim response As System.Net.HttpWebResponse = request.GetResponse
Dim sr As System.IO.StreamReader = New System.IO.StreamReader(response.GetResponseStream())
Dim sourcecode As String = sr.ReadToEnd
Dim search As String = combobox1.Text
Dim r As New System.Text.RegularExpressions.Regex("http://" & search & "/\w*")
Dim matches As MatchCollection = r.Matches(sourcecode)
For Each itemcode As Match In matches
Dim item As String = (itemcode.ToString.Split("""").GetValue(0))
Dim url As New Net.WebClient
Dim str As String = url.DownloadString("http://www.prcheck.nl/results.php?url=" & item)
If str.Contains(">0/10") Then
ListBox1.Items.Add("(0/10) " & item)
ElseIf str.Contains("1/10") Then
ListBox1.Items.Add("(1/10) " & item)
ElseIf str.Contains("2/10") Then
ListBox1.Items.Add("(2/10) " & item)
ElseIf str.Contains("3/10") Then
ListBox1.Items.Add("(3/10) " & item)
ElseIf str.Contains("4/10") Then
ListBox1.Items.Add("(4/10) " & item)
ElseIf str.Contains("5/10") Then
ListBox1.Items.Add("(5/10) " & item)
ElseIf str.Contains("6/10") Then
ListBox1.Items.Add("(6/10) " & item)
ElseIf str.Contains("7/10") Then
ListBox1.Items.Add("(7/10) " & item)
ElseIf str.Contains("8/10") Then
ListBox1.Items.Add("(8/10) " & item)
ElseIf str.Contains("9/10") Then
ListBox1.Items.Add("(9/10) " & item)
ElseIf str.Contains("10/10") Then
ListBox1.Items.Add("(10/10) " & item)
Else
ListBox1.Items.Add("(0/10) " & item)
End If
Label2.Text = ListBox1.Items.Count
Next
If Not sourcecode.Contains("<b>Volgende</b>") Then
MsgBox("")
Exit For
End If
Next
End Sub
and combobox1.text = www.google.nl ( example )
at button 1 the code is:
BackgroundWorker1.RunWorkerAsync()
and if backgroundworker is done:
Private Sub BackgroundWorker1_RunWorkerCompleted(ByVal sender As Object, ByVal e As System.ComponentModel.RunWorkerCompletedEventArgs) Handles BackgroundWorker1.RunWorkerCompleted
MsgBox("Done")
End Sub
if i click button 1, i get within a half second the message: Done
what's wrong with the code??
if i put the code inside backgroundworker1 just in button1 it works but goes really slow..
You can only update the UI from within the main application thread, in this case you're attempting to do it via a background thread that has been created by the background worker which will throw an exception as you've found.
What you'll need to do it run the code which adds to the ListBox on the main thread which you can do via BeginInvoke and a custom delegate which takes the item you want to add as a parameter, the delegate can then add the item to list box - there's an example of how to do this in the docs for BeginInvoke.
I would return a list or array of items to be added from the background worker and then fill the ListBox in the RunWorkerCompleted event handler.