My Second BackgroundWorker won't work - vb.net

I have created 2 BackgroundWorkers and my second BackgroundWorker seem to not work at all, i have placed a messagebox indicator under "Private Sub BackgroundWorker2_DoWork" and it was triggered but the codess under it were not. Here is the complete code of my BackgroundWorker that is having problems. Is there something that is causing this?
i really need 2 BackgroundWorkers for my program as it is processing tons of files which makes the application hang-up.
Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
Try
If BackgroundWorker2.IsBusy <> True Then
BackgroundWorker2.RunWorkerAsync()
End If
Catch ex As Exception
MessageBox.Show(ex.Message)
End Try
End Sub
Private Sub BackgroundWorker2_DoWork(sender As System.Object, e As System.ComponentModel.DoWorkEventArgs) Handles BackgroundWorker2.DoWork
Dim worker1 As System.ComponentModel.BackgroundWorker = CType(sender, System.ComponentModel.BackgroundWorker)
Try
MessageBox.Show("the program was able to open me")
'this message box above was able to display but the codes below were not processed
Dim Stream As System.IO.FileStream
Dim Index As Integer = 0
Dim openFileDialog1 As New OpenFileDialog()
openFileDialog1.InitialDirectory = "D:\work\base tremble"
openFileDialog1.Filter = "txt files (*.txt)|*.txt"
openFileDialog1.FilterIndex = 2
openFileDialog1.RestoreDirectory = True
If openFileDialog1.ShowDialog() = System.Windows.Forms.DialogResult.OK Then
Try
'This line opens the file the user selected and sets the stream object
Stream = openFileDialog1.OpenFile()
If (Stream IsNot Nothing) Then
'create the reader here and use the stream you got from the file open dialog
Dim sReader As New System.IO.StreamReader(Stream)
Do While sReader.Peek >= 0
ReDim Preserve eArray(Index)
eArray(Index) = sReader.ReadLine
RichTextBox3.Text = eArray(Index)
Index += 1
worker1.ReportProgress(Index)
'Delay(2)
Loop
Label1.Text = "0/" & eArray.Length & ""
End If
Catch Ex As Exception
MessageBox.Show(Ex.Message)
Finally
If (Stream IsNot Nothing) Then
Stream.Close()
End If
End Try
End If
Catch ex As Exception
MessageBox.Show(ex.Message)
End Try
End Sub
Private Sub BackgroundWorker2_ProgressChanged(sender As System.Object, e As System.ComponentModel.ProgressChangedEventArgs) Handles BackgroundWorker2.ProgressChanged
Try
'Label1.Text = e.ProgressPercentage.ToString()
Me.ProgressBar2.Value = e.ProgressPercentage
Catch ex As Exception
MessageBox.Show(ex.Message)
End Try
End Sub
Private Sub BackgroundWorker2_Completed(sender As System.Object, e As System.ComponentModel.RunWorkerCompletedEventArgs) Handles BackgroundWorker2.RunWorkerCompleted
Try
Catch ex As Exception
MessageBox.Show(ex.Message)
End Try
End Sub

i have sperated OpenFileDialog into a different button and i processed the retreiving storing of data into the array in the backgroundworker and it works now.
Thanks for the headsup about OpenFileDialog not allowed in backgroundworker it gave me a hint.

Related

How to delete a file used by another Process

Public Class frmMainCetegory
Private Sub frmMainCetegory_Load(sender As Object, e As EventArgs) Handles MyBase.Load
'TODO: This line of code loads data into the 'PosDatabaseDataSet.tblItemMainCategory' table. You can move, or remove it, as needed.
Me.TblItemMainCategoryTableAdapter.Fill(Me.PosDatabaseDataSet.tblItemMainCategory)
Me.CenterToParent()
Me.btnAddNew.Select()
End Sub
Private Sub btnAddNew_Click(sender As Object, e As EventArgs) Handles btnAddNew.Click
Try
Me.TblItemMainCategoryBindingSource.AddNew()
Me.txtMainCategory.Select()
Catch ex As Exception
MsgBox(ex.Message)
End Try
End Sub
Private Sub btnSave_Click(sender As Object, e As EventArgs) Handles btnSave.Click
Try
If Me.txtMainCategory.Text = "" Then
MsgBox("Please Enter Main Category Name!", vbInformation)
Exit Sub
End If
Me.imgMainCategoryImage.Image.Save(Application.StartupPath & "\Images\" & Me.txtMainCategory.Text & ".jpeg", System.Drawing.Imaging.ImageFormat.Jpeg)
Me.TblItemMainCategoryBindingSource.EndEdit()
Me.TblItemMainCategoryTableAdapter.Update(Me.PosDatabaseDataSet.tblItemMainCategory)
Me.TblItemMainCategoryTableAdapter.Fill(Me.PosDatabaseDataSet.tblItemMainCategory)
Me.btnAddNew.Select()
Catch ex As Exception
MsgBox(ex.Message)
End Try
End Sub
Private Sub btnDelete_Click(sender As Object, e As EventArgs) Handles btnDelete.Click
Try
Dim MainCatName As String
MainCatName = Me.TblItemMainCategoryDataGridView.CurrentRow.Cells("MainCategoryName").Value
If Me.TblItemMainCategoryDataGridView.Rows.Count - 1 > 0 Then
'check before delting, if this main category is used in sub category then It can not be deleted.
Dim I As Integer
I = frmSubCategories.TblItemSubCategoryTableAdapter.qryCountMainCategoryUnderSubCategory(MainCatName)
If I > 0 Then
MsgBox(MainCatName & " is used under Sub Category(s), Please Delete Sub Category(s) First!", vbOK)
Exit Sub
End If
End If
Me.TblItemMainCategoryBindingSource.RemoveCurrent()
Me.TblItemMainCategoryTableAdapter.Update(Me.PosDatabaseDataSet.tblItemMainCategory)
Me.TblItemMainCategoryTableAdapter.Fill(Me.PosDatabaseDataSet.tblItemMainCategory)
'Delete the Main Category Image if this exists
If System.IO.File.Exists(Application.StartupPath & "\Images\" & MainCatName & ".JPEG") Then
System.IO.File.Delete(Application.StartupPath & "\Images\" & MainCatName & ".JPEG")
MsgBox("Image Deleted Successfull")
End If
Catch ex As Exception
MsgBox(ex.Message)
End Try
End Sub
Private Sub btnBrowse_Click(sender As Object, e As EventArgs) Handles btnBrowse.Click
Try
Me.OpenFileDialog1.Filter = "Bitmaps(*.JPG, *.PNG, *.JPEG, *.BMP, *.TIF, *.GIF, *.TIFF)|"
Me.OpenFileDialog1.FileName = ""
If OpenFileDialog1.ShowDialog(Me) = DialogResult.OK Then
Dim img As String = Me.OpenFileDialog1.FileName
Me.imgMainCategoryImage.Image = System.Drawing.Bitmap.FromFile(img)
Me.btnSave.Select()
Else
End If
Catch ex As Exception
MsgBox(ex.Message)
End Try
End Sub
Private Sub TblItemMainCategoryDataGridView_SelectionChanged(sender As Object, e As EventArgs) Handles TblItemMainCategoryDataGridView.SelectionChanged
Try
Me.imgMainCategoryImage.Image = DirectCast(Image.FromFile(Application.StartupPath & "\Images\" & Me.TblItemMainCategoryDataGridView.CurrentRow.Cells("MainCategoryName").Value & ".JPEG").Clone(), Image)
' Me.imgMainCategoryImage.Image = System.Drawing.Bitmap.FromFile(Application.StartupPath & "\Images\" & Me.TblItemMainCategoryDataGridView.CurrentRow.Cells("MainCategoryName").Value & ".JPEG")
Catch ex As Exception
Me.imgMainCategoryImage.Image = My.Resources.imgEmpty 'if image path not found then select empty image
End Try
End Sub
Private Sub btnClose_Click(sender As Object, e As EventArgs) Handles btnClose.Click
Me.Close()
End Sub
End Class
Above code give me an error message:
image is used in another process, can't be deleted
when I delete the image file. The image is assigned to a PictureBox with selection event of the DataGridView. Since I have deleted the DataGridView row, as shown in the above code, it changes the image in the PictureBox.
Here I am unable to delete the image file related to the removed row.
Please guide and let me know if you need any further details.

Deletion of image from folder that is referenced to picture box

sorry i have to rewrite this question as i did not get any update on my same question posted previously.
Below is the code where I'm inserting some data into Ms Access database in visual studio using vb. Net.
When i insert the data i also browse the image for that item and save into the folder that is located under project directory for universal access.
There is a datagridview which displays the data which was inserted to access databas alog with the image in picture box. Whenever the the datagrid view selection is change it display the picure into picture box.
I am having a trouble when i want to delete the data along with image located in project folder as it show the error that 'file used by another process, cant be deleted'
Since i have referenced that image to my picture box, it can not be deleted directly. So here I'm looking for a solution which can display the clone of image in picture box instead of real object.
Below is my code
Public Class frmMainCetegory
Private Sub frmMainCetegory_Load(sender As Object, e As EventArgs) Handles MyBase.Load
'TODO: This line of code loads data into the 'PosDatabaseDataSet.tblItemMainCategory' table. You can move, or remove it, as needed.
Me.TblItemMainCategoryTableAdapter.Fill(Me.PosDatabaseDataSet.tblItemMainCategory)
Me.CenterToParent()
Me.btnAddNew.Select()
End Sub
Private Sub btnAddNew_Click(sender As Object, e As EventArgs) Handles btnAddNew.Click
Try
Me.TblItemMainCategoryBindingSource.AddNew()
Me.txtMainCategory.Select()
Catch ex As Exception
MsgBox(ex.Message)
End Try
End Sub
Private Sub btnSave_Click(sender As Object, e As EventArgs) Handles btnSave.Click
Try
If Me.txtMainCategory.Text = "" Then
MsgBox("Please Enter Main Category Name!", vbInformation)
Exit Sub
End If
Me.imgMainCategoryImage.Image.Save(Application.StartupPath & "\Images\" & Me.txtMainCategory.Text & ".jpeg", System.Drawing.Imaging.ImageFormat.Jpeg)
Me.TblItemMainCategoryBindingSource.EndEdit()
Me.TblItemMainCategoryTableAdapter.Update(Me.PosDatabaseDataSet.tblItemMainCategory)
Me.TblItemMainCategoryTableAdapter.Fill(Me.PosDatabaseDataSet.tblItemMainCategory)
Me.btnAddNew.Select()
Catch ex As Exception
MsgBox(ex.Message)
End Try
End Sub
Private Sub btnDelete_Click(sender As Object, e As EventArgs) Handles btnDelete.Click
Try
Dim MainCatName As String
MainCatName = Me.TblItemMainCategoryDataGridView.CurrentRow.Cells("MainCategoryName").Value
If Me.TblItemMainCategoryDataGridView.Rows.Count - 1 > 0 Then
'check before delting, if this main category is used in sub category then It can not be deleted.
Dim I As Integer
I = frmSubCategories.TblItemSubCategoryTableAdapter.qryCountMainCategoryUnderSubCategory(MainCatName)
If I > 0 Then
MsgBox(MainCatName & " is used under Sub Category(s), Please Delete Sub Category(s) First!", vbOK)
Exit Sub
End If
End If
Me.TblItemMainCategoryBindingSource.RemoveCurrent()
Me.TblItemMainCategoryTableAdapter.Update(Me.PosDatabaseDataSet.tblItemMainCategory)
Me.TblItemMainCategoryTableAdapter.Fill(Me.PosDatabaseDataSet.tblItemMainCategory)
'Delete the Main Category Image if this exists
If System.IO.File.Exists(Application.StartupPath & "\Images\" & MainCatName & ".JPEG") Then
System.IO.File.Delete(Application.StartupPath & "\Images\" & MainCatName & ".JPEG")
MsgBox("Image Deleted Successfull")
End If
Catch ex As Exception
MsgBox(ex.Message)
End Try
End Sub
Private Sub btnBrowse_Click(sender As Object, e As EventArgs) Handles btnBrowse.Click
Try
Me.OpenFileDialog1.Filter = "Bitmaps(*.JPG, *.PNG, *.JPEG, *.BMP, *.TIF, *.GIF, *.TIFF)|"
Me.OpenFileDialog1.FileName = ""
If OpenFileDialog1.ShowDialog(Me) = DialogResult.OK Then
Dim img As String = Me.OpenFileDialog1.FileName
Me.imgMainCategoryImage.Image = System.Drawing.Bitmap.FromFile(img)
Me.btnSave.Select()
Else
End If
Catch ex As Exception
MsgBox(ex.Message)
End Try
End Sub
Private Sub TblItemMainCategoryDataGridView_SelectionChanged(sender As Object, e As EventArgs) Handles TblItemMainCategoryDataGridView.SelectionChanged
Try
Me.imgMainCategoryImage.Image = DirectCast(Image.FromFile(Application.StartupPath & "\Images\" & Me.TblItemMainCategoryDataGridView.CurrentRow.Cells("MainCategoryName").Value & ".JPEG").Clone(), Image)
' Me.imgMainCategoryImage.Image = System.Drawing.Bitmap.FromFile(Application.StartupPath & "\Images\" & Me.TblItemMainCategoryDataGridView.CurrentRow.Cells("MainCategoryName").Value & ".JPEG")
Catch ex As Exception
Me.imgMainCategoryImage.Image = My.Resources.imgEmpty 'if image path not found then select empty image
End Try
End Sub
Private Sub btnClose_Click(sender As Object, e As EventArgs) Handles btnClose.Click
Me.Close()
End Sub
Private Sub Label6_Click(sender As Object, e As EventArgs) Handles Label6.Click
frmTable.Show()
End Sub
End Class

Can't launch .exe

Trying to make a "world of warcraft" launcher.
How it should work:
When you press " Start Authserver " you get to locate the authserver.exe. the location is then saved so you don't have to do that twice.
Once pressed, it obviously should launch the authserver.exe.
What happens:
When you press " Start Authserver " it opens and instantly closes with error:
Error, couldn't open cause authserver.conf couldn't be located.
Notice:
Authserver.exe and authserver.conf is in the same folder, I'm able to launch it manually.
Mort helped me with similar problem yesterday, i hoped that the same VB code would work since that would seem obvious.
Private filePath As String = String.Empty
Private Sub PlayButton_Click(sender As System.Object, e As System.EventArgs) Handles PlayButton.Click
Try
If filePath.Length = 0 Then
Dim diagResult As DialogResult = OpenFileDialog1.ShowDialog()
If diagResult = Windows.Forms.DialogResult.OK Then
filePath = OpenFileDialog1.FileName
If filePath.ToUpper.EndsWith("WOW.EXE") Then
Process.Start(filePath)
Else
MessageBox.Show("Wrong file selected!")
filePath = String.Empty
End If
End If
Else
Process.Start(filePath)
End If
Catch ex As Exception
MessageBox.Show(String.Concat("An error occurred in the play button click:", ex.Message))
End Try
End Sub
Anyone got an idea?
In advance: Thanks.
Try something like this:
Private Sub PlayButton_Click(sender As System.Object, e As System.EventArgs) Handles PlayButton.Click
Try
OpenFileDialog1.FileName = ""
OpenFileDialog1.Filter = "World of Warcraft (WOW.EXE)|WOW.EXE"
Dim diagResult As DialogResult = OpenFileDialog1.ShowDialog
If diagResult = Windows.Forms.DialogResult.OK Then
Dim p As New Process
Dim fn As New System.IO.FileInfo(OpenFileDialog1.FileName)
p.StartInfo.WorkingDirectory = fn.DirectoryName
p.StartInfo.FileName = fn.Name
p.Start()
End If
Catch ex As Exception
MessageBox.Show(String.Concat("An error occurred in the play button click:", ex.Message))
End Try
End Sub

Drag and Drop in datagridview in vb.net

I am using two Datagridview in my code, i drag content from Me.datagridview2 and drop it on Me.datagridview1.This process is successful. But as soon as i click the cell other than the dropped content cell, the dropped content disappears. Here's my code
Private Sub DataGridView2_MouseDown(sender As Object, e As System.Windows.Forms.MouseEventArgs) Handles DataGridView2.MouseDown
Try
If Me.DataGridView2.SelectedRows.Count = 0 Then
Exit Sub
End If
Me.DataGridView2.DoDragDrop(Me.DataGridView2.SelectedRows(0), DragDropEffects.All)
Catch ex As Exception
MessageBox.Show(ex.Message, "Message", MessageBoxButtons.OK, MessageBoxIcon.Error)
End Try
End Sub
Private Sub DataGridView1_DragDrop(sender As Object, e As System.Windows.Forms.DragEventArgs) Handles DataGridView1.DragDrop
Try
Dim r As DataGridViewRow = e.Data.GetData(GetType(DataGridViewRow))
If Me.DataGridView1.SelectedRows.Count = 0 Then
Exit Sub
End If
Dim i As Integer = Me.DataGridView1.SelectedRows(0).Index
Me.DataGridView1.Rows(i).Cells(1).Value = r.Cells(0).Value
Me.DataGridView1.Rows(i).Cells(2).Value = r.Cells(1).Value
Me.DataGridView1.Rows(i).Cells(3).Value = r.Cells(2).Value
Me.DataGridView1.Rows(i).Cells(4).Value = r.Cells(3).Value
Catch ex As Exception
MessageBox.Show(ex.Message, "Message", MessageBoxButtons.OK, MessageBoxIcon.Error)
End Try
End Sub
Private Sub DataGridView1_DragEnter(sender As Object, e As System.Windows.Forms.DragEventArgs) Handles DataGridView1.DragEnter
Try
e.Effect = DragDropEffects.Copy
Catch ex As Exception
MessageBox.Show(ex.Message, "Message", MessageBoxButtons.OK, MessageBoxIcon.Error)
End Try
End Sub
Have following doubts
1. why does the dropped content disappears.
2. whenever we begin edit on datagridview, a row in automatically added below. Why it doesn't gets added when i drop content in datagridview.?
Please help me.
Actually, i just got an alternative to my own question. Here's it.
Private Sub DataGridView1_DragDrop(sender As Object, e As System.Windows.Forms.DragEventArgs) Handles DataGridView1.DragDrop
Try
Dim r As DataGridViewRow = e.Data.GetData(GetType(DataGridViewRow))
If Me.DataGridView1.SelectedRows.Count = 0 Then
Exit Sub
End If
Dim i As Integer = Me.DataGridView1.SelectedRows(0).Index
dragseldet.Tables(0).Rows.Add("", r.Cells(0).Value, r.Cells(1).Value, r.Cells(2).Value, r.Cells(3).Value, 0, 0)
dragseldet.AcceptChanges()
'Me.DataGridView1.Rows(i).Cells(1).Value = r.Cells(0).Value
'Me.DataGridView1.Rows(i).Cells(2).Value = r.Cells(1).Value
'Me.DataGridView1.Rows(i).Cells(3).Value = r.Cells(2).Value
'Me.DataGridView1.Rows(i).Cells(4).Value = r.Cells(3).Value
Catch ex As Exception
MessageBox.Show(ex.Message, "Message", MessageBoxButtons.OK, MessageBoxIcon.Error)
End Try
End Sub
Rather than copying the contents from r row to Me.DataGridview. all cells , i am directly adding r row to my datasource named dragsaldet . And that did the trick for me.

Error : file is being used by another process

I have a problem where it says that a file is already being used by another process
But I am pretty sure it's not being used anywhere else..
Imports System.IO
Public Class Browse
Private Sub add_Click(sender As Object, e As EventArgs) Handles add.Click
Dim Files As New OpenFileDialog
Dim Folder As New FolderBrowserDialog
Dim Filelist As String = "C:\Program Files\FTP-Sync\Files.txt"
Dim FileP As String = ""
Dim list() As String = IO.File.ReadAllLines(Filelist)
If fileb.Checked Then
Try
If System.IO.File.Exists(Filelist) = True And Files.ShowDialog = Windows.Forms.DialogResult.OK Then
FileP = Files.FileName
End If
Catch ex As Exception
MsgBox(ex.Message)
End Try
End If
If folderb.Checked Then
Try
If System.IO.File.Exists(Filelist) = True And Folder.ShowDialog = Windows.Forms.DialogResult.OK Then
FileP = Folder.SelectedPath
End If
Catch ex As Exception
MsgBox(ex.Message)
End Try
End If
pathtxt.Text = FileP
End Sub
Private Sub ok_Click(sender As Object, e As EventArgs) Handles ok.Click
Dim Filelist As String = "C:\Program Files\FTP-Sync\Files.txt"
Dim writer As StreamWriter = New StreamWriter(Filelist)
Dim FileP As String = ""
Try
If pathtxt.Text = "" Then
MsgBox("No folder or file has been choosen")
Else
writer = File.AppendText(Filelist)
writer.WriteLine(pathtxt.Text)
pathtxt.Text = FileP
writer.Close()
Me.Hide()
End If
Catch ex As Exception
MsgBox(ex.Message)
End Try
End Sub
Private Sub cancel_Click(sender As Object, e As EventArgs) Handles cancel.Click
Me.Hide()
End Sub
Private Sub Browse_Load(sender As Object, e As EventArgs) Handles MyBase.Load
End Sub
End Class
Thanks in advance!
It was because of this:
Dim writer As StreamWriter = New StreamWriter(Filelist)
i changed it to
Dim writer As StreamWriter
and it work like a charm.. dont know if it is the right way.. but it worked.