Threading doesn't complete the task before ending the thread loop - vb.net

So I did something similar a while ago, but this is essentially a username checker for a (specific) website, they can load in usernames via text file and it will put it into a list box, now I have the start button and it's meant to check each username. Before, however, it froze the program when they checked, but it worked. I tried making it "threaded" so it didn't freeze.
The problem now is that it doesn't check them all, and finishes instantly.
CODE:
Private Sub Button6_Click(sender As Object, e As EventArgs) Handles Button6.Click
Button6.Enabled = False
Dim goodUsers As New SaveFileDialog()
goodUsers.Filter = "TXT file (*.txt)|*.txt"
Dim flag As Boolean
Dim Incomplete As Integer = 0
Dim Taken As Integer = 0
Dim sb As New StringBuilder
If goodUsers.ShowDialog() = DialogResult.OK Then
Dim checkerMT As New Thread(
Sub()
For Each i As String In UsernameList.Items
WebRequest.Create("http://yatobooter.cf/other/checkusr.php?username=" + i.ToString)
Dim cResult As String = New System.Net.WebClient().DownloadString("http://yatobooter.cf/other/checkusr.php?username=" + i.ToString).ToString
If cResult = "taken" Then
flag = False
ElseIf cResult = "nottaken" Then
flag = True
End If
If flag = True Then
sb.Append(i & vbNewLine)
Else
Incomplete = Incomplete + 1
Taken = UsernameList.Items.Count - Incomplete
End If
Next
End Sub
)
checkerMT.Start()
Try
File.WriteAllText(goodUsers.FileName, sb.ToString)
Catch ex As Exception
Exit Sub
End Try
End If
MessageBox.Show("Checking available usernames, complete!", "NameSniper Pro")
Button6.Enabled = True
End Sub

You cannot access UI elements (UsernameList.Items) from a thread other than the UI. Instead add a background worker to your form to handle the basic threading stuff (progress reporting, finish reporting, exception handling). Pass into this an object that contains the settings your job needs to do its work without interacting with the ui.
Private Sub Button6_Click(sender As Object, e As EventArgs) Handles Button6.Click
Button6.Enabled = False
Dim goodUsers As New SaveFileDialog()
goodUsers.Filter = "TXT file (*.txt)|*.txt"
If goodUsers.ShowDialog() = DialogResult.OK Then
'Note: You'll need to add the filenames
BackgroundWorker1.RunWorkerAsync(New State() With {.Names = {}, .FileName = goodUsers.FileName})
End If
End Sub
Class State
Public Names As List(Of String)
Public StringBuilder As New System.Text.StringBuilder
Public Incomplete As Integer
Public Taken As Integer
Public FileName As String
End Class
Private Sub BackgroundWorker1_DoWork(sender As System.Object, e As System.ComponentModel.DoWorkEventArgs) Handles BackgroundWorker1.DoWork
Dim state = CType(e.Argument, State)
For Each i As String In state.Names
Using cli = New System.Net.WebClient()
Dim cResult = cli.DownloadString("http://yatobooter.cf/other/checkusr.php?username=" + i.ToString).ToString
If cResult = "nottaken" Then
state.StringBuilder.Append(i & vbNewLine)
Else
state.Incomplete = state.Incomplete + 1
state.Taken = state.Names.Count - state.Incomplete
End If
End Using
Next
IO.File.WriteAllText(state.FileName, state.StringBuilder.ToString)
End Sub
Private Sub BackgroundWorker1_RunWorkerCompleted(sender As Object, e As System.ComponentModel.RunWorkerCompletedEventArgs) Handles BackgroundWorker1.RunWorkerCompleted
If e.Error IsNot Nothing Then
MessageBox.Show(e.Error.ToString(), "Error")
Else
MessageBox.Show("Checking available usernames, complete!", "NameSniper Pro")
End If
Button6.Enabled = True
End Sub

The SaveFileDialog can be used as "Using directive"
Why do you create a webrequest on the one hand, and on the other hand use a webclient? That does not make sense at all.
Instead of your strange if condition, write:
flag = (cResult.Equals("nottaken"))
All code you want to run after the actions you are currently running in your thread have to be in your thread as well, because it is async.
You have to invoke if you use user controls within a thread
and so on..
Please turn Option Strict On and Option Infer Off
There are lots of other things you could do better.
Please remove the webrequest and webclient combination yourself, that does not make sense at all.
Take a look at this, I cleared it a little bit:
Private Sub Button6_Click(sender As Object, e As EventArgs) Handles Button6.Click
Dim Incomplete As Integer = 0
Dim Taken As Integer = 0
Dim sb As New StringBuilder
Using goodUsers As SaveFileDialog = new SaveFileDialog()
goodUsers.Filter = "TXT file (*.txt)|*.txt"
If not goodUsers.ShowDialog() = DialogResult.OK Then Exit Sub
Dim checkerMT As New Thread(
Sub()
Me.invoke(sub()
Button6.Enabled = False
For Each i As String In UsernameList.Items
WebRequest.Create("http://yatobooter.cf/other/checkusr.php?username=" + i.ToString)
Dim cResult As String = New System.Net.WebClient().DownloadString("http://yatobooter.cf/other/checkusr.php?username=" + i.ToString).ToString
If (cResult.toLower().Equals("nottaken")) Then
sb.Append(String.Concat(i , Environment.NewLine)
Else
Incomplete += 1
Taken = UsernameList.Items.Count - Incomplete
End If
Next
File.WriteAllText(goodUsers.FileName, sb.ToString)
Button6.Enabled = True
MessageBox.Show("Checking available usernames, complete!", "NameSniper Pro")
End Sub)
End Sub
)
checkerMT.IsBackground = True;
checkerMT.Start()
End Sub

Related

How to show records added to datagridview in real time

A co-worker needs to search our network and her File Explorer search does not work well. I threw this app together quickly to allow her to search and it works well. The results are written to a datagridview, but the results are not shown until the search is complete.
I would like the datagridview to show records as they are added and allow her to cancel the search if she wants.
Using a backgroundworker, I tried to refresh the grid, but as soon as it finds a match, the code stops running. There are no errors, it just stops running.
So how can I get the grid to update as it continues to search?
Public dtResults As DataTable
Dim myDataSet As New DataSet
Dim myDataRow As DataRow
Dim colType As DataColumn
Dim colResult As DataColumn
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
dtResults = New DataTable()
colType = New DataColumn("Type", Type.GetType("System.String"))
colResult = New DataColumn("Search Result", Type.GetType("System.String"))
dtResults.Columns.Add(colType)
dtResults.Columns.Add(colResult)
DataGridView1.DataSource = dtResults
DataGridView1.Columns(1).AutoSizeMode = DataGridViewAutoSizeColumnMode.Fill
End Sub
Private Sub btnSearch_Click(sender As Object, e As EventArgs) Handles btnSearch.Click
btnSearch.Enabled = False
sbStatusBar.Text = "Searching..."
dtResults.Clear()
BackgroundWorker1.RunWorkerAsync()
End Sub
Private Sub BackgroundWorker1_DoWork(sender As Object, e As System.ComponentModel.DoWorkEventArgs) Handles BackgroundWorker1.DoWork
LoopSubFolders(txtSearchLocation.Text)
End Sub
Public Sub LoopSubFolders(sLocation As String)
Dim di = New DirectoryInfo(sLocation)
Dim mySearchterm As String = LCase(txtSearchTerm.Text)
Dim fiArr As FileInfo() = di.GetFiles()
Dim sSearchTarget As String
sbStatusBar.Text = "Searching " & sLocation
'Search File names in
If cbFileNames.Checked = True Then
For Each myFile In fiArr
sSearchTarget = LCase(myFile.Name)
If sSearchTarget.Contains(mySearchterm) Then
myDataRow = dtResults.NewRow()
myDataRow(dtResults.Columns(0)) = "File"
myDataRow(dtResults.Columns(1)) = Path.Combine(sLocation, myFile.Name)
dtResults.Rows.Add(myDataRow)
End If
Next
End If
For Each d In Directory.GetDirectories(sLocation)
If cbFolderNames.Checked = True Then
sSearchTarget = LCase(d)
If sSearchTarget.Contains(mySearchterm) Then
myDataRow = dtResults.NewRow()
myDataRow(dtResults.Columns(0)) = "Folder"
myDataRow(dtResults.Columns(1)) = d
dtResults.Rows.Add(myDataRow)
End If
End If
LoopSubFolders(d)
Next
End Sub
Private Sub BackgroundWorker1_RunWorkerCompleted(sender As Object, e As System.ComponentModel.RunWorkerCompletedEventArgs) Handles BackgroundWorker1.RunWorkerCompleted
btnSearch.Enabled = True
sbStatusBar.Text = "Complete"
DataGridView1.DataSource = Nothing
DataGridView1.DataSource = dtResults
DataGridView1.Columns(1).AutoSizeMode = DataGridViewAutoSizeColumnMode.Fill
End Sub
Here's an example of how you might do it using the suggested ReportProgress method and ProgressChanged event:
Private table As New DataTable
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
'Configure table here.
DataGridView1.DataSource = table
End Sub
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
'Setup UI here.
'Note that you MUST pass in the TextBox data as you MUST NOT touch the UI directly on the secondary thread.
BackgroundWorker1.RunWorkerAsync({TextBox1.Text, TextBox2.Text})
End Sub
Private Sub BackgroundWorker1_DoWork(sender As Object, e As DoWorkEventArgs) Handles BackgroundWorker1.DoWork
'Get the data passed in and separate it.
Dim arguments = DirectCast(e.Argument, String())
Dim folderPath = arguments(0)
Dim searchTerm = arguments(1)
SearchFileSystem(folderPath, searchTerm)
End Sub
Private Sub SearchFileSystem(folderPath As String, searchTerm As String)
For Each filePath In Directory.GetFiles(folderPath)
If filePath.IndexOf(searchTerm, StringComparison.InvariantCultureIgnoreCase) <> -1 Then
'Update the UI on the UI thread.
BackgroundWorker1.ReportProgress(0, {"File", filePath})
End If
Next
For Each subfolderPath In Directory.GetDirectories(folderPath)
If subfolderPath.IndexOf(searchTerm, StringComparison.InvariantCultureIgnoreCase) <> -1 Then
'Update the UI on the UI thread.
BackgroundWorker1.ReportProgress(0, {"Folder", subfolderPath})
End If
SearchFileSystem(subfolderPath, searchTerm)
Next
End Sub
Private Sub BackgroundWorker1_ProgressChanged(sender As Object, e As ProgressChangedEventArgs) Handles BackgroundWorker1.ProgressChanged
'Get the data passed out and separate it.
Dim data = DirectCast(e.UserState, String())
'Update the UI.
table.Rows.Add(data)
End Sub
Note that you should NEVER touch the UI directly in the DoWork event handler or a method called from it. ONLY touch the UI on the UI thread. That means that the text in your TextBoxes must be extracted BEFORE calling RunWorkerAsync. You can eithewr pass the Strings in as arguments or you can assign them to fields and access them from there on any thread. Don't EVER access a member of a control on other than the UI thread. Some times it will work, sometimes it will appear to work but not do as intended and sometimes it will crash your app. So that you don't have to remember which specific scenarios cause which result, avoid such scenario altogether.
I haven't tested this code so I'm not sure but you may have to call Refresh on the grid or the form after adding the new row to the DataTable.
Variables
Well, let's start from the top with some class level variables:
'Notice the enabled properties.
Private WithEvents BackgroundWorker1 As New BackgroundWorker With {.WorkerReportsProgress = True, .WorkerSupportsCancellation = True}
'To monitor the cancellation, set by the Cancel Button.
Private bgwCancel As Boolean = False
'The DGV source.
Private dtResults As New DataTable
'The start directory.
Private startDir As String
'The search keyword.
Private searchWord As String
'Whether to search the sub directories, from a check box for example.
Private includeSubDirectories As Boolean = True
'Whether to search the files, from another check box.
Private includeFiles As Boolean = True
The Constructor
Prepare your DGV and whatever else you need here.
Sub New()
dtResults.Columns.Add(New DataColumn("Type", Type.GetType("System.String")))
dtResults.Columns.Add(New DataColumn("Search Result", Type.GetType("System.String")))
DataGridView1.DataSource = dtResults
DataGridView1.Columns(1).AutoSizeMode = DataGridViewAutoSizeColumnMode.Fill
'Make sure you add the image column after binding the data source.
Dim imgCol As New DataGridViewImageColumn(False)
With imgCol
.Image = Nothing
.Name = "imgCol"
.HeaderText = ""
.Width = 50
.DefaultCellStyle.NullValue = Nothing
End With
DataGridView1.Columns.Insert(0, imgCol)
End Sub
Iterator
Now, let's write the search routine. I'd do that through an Iterator function:
Private Iterator Function IterateFolders(startDir As String, includeFiles As Boolean, includeSubDir As Boolean) As IEnumerable(Of String)
For Each dirName In IO.Directory.EnumerateDirectories(startDir)
Yield dirName
If includeFiles Then
For Each fileName In IO.Directory.EnumerateFiles(startDir)
Yield fileName
Next
End If
If includeSubDir Then
For Each subDir In IterateFolders(dirName, includeFiles, includeSubDir)
Yield subDir
Next
End If
Next
End Function
The Main Thread Updater
A routine called by the worker's thread to update the DataTable and any control that belongs to the main thread:
Private Sub AddSearchResult(path As String)
If InvokeRequired Then
Invoke(Sub() AddSearchResult(path))
Else
dtResults.Rows.Add(If(IO.File.Exists(path), "File", "Folder"), path)
sbStatusBar.Text = $"Searching {path}"
End If
End Sub
Start
In the click event of the start button, do the necessary validations, assign the values to their variables, and start the back ground worker:
If String.IsNullOrEmpty(txtSearchKeyword.Text) Then Return
If String.IsNullOrEmpty(txtSearchLocation.Text) Then Return
bgwCancel = False
dtResults.Rows.Clear()
startDir = txtSearchLocation.Text
searchWord = txtSearchKeyword.Text.ToLower
includeSubDirectories = chkIncludeSubDirs.Checked
includeFiles = chkFiles.Checked
btnSearch.Enabled = False
sbStatusBar.Text = "Searching..."
BackgroundWorker1.RunWorkerAsync()
Cancel
To cancel the search, in the click event of the cancel button I presume, True the bgwCancel variable:
bgwCancel = True
The BackgroundWorker - DoWork
Private Sub BackgroundWorker1_DoWork(sender As Object, e As DoWorkEventArgs) Handles BackgroundWorker1.DoWork
For Each item As String In IterateFolders(startDir, includeFiles, includeSubDirectories)
If bgwCancel Then
BackgroundWorker1.CancelAsync()
Return
End If
If item.ToLower.Contains(searchWord) Then
AddSearchResult(item)
End If
Threading.Thread.Sleep(100)
Next
End Sub
Note that, Its good practice to give a lengthy routine a BREATH through the Sleep(ms) method of that thread.
The BackgroundWorker - ProgressChanged
I don't think you need it here.
The BackgroundWorker - RunWorkerCompleted
Private Sub BackgroundWorker1_RunWorkerCompleted(sender As Object, e As RunWorkerCompletedEventArgs) Handles BackgroundWorker1.RunWorkerCompleted
If bgwCancel Then
sbStatusBar.Text = "Canceled!"
MessageBox.Show("Canceled by you!")
ElseIf e.Error IsNot Nothing Then
sbStatusBar.Text = "Error!"
MessageBox.Show(e.Error.Message)
Else
sbStatusBar.Text = "Complete"
'YOU DO NOT NEED TO DO THIS. Remove the following
'DataGridView1.DataSource = Nothing
'DataGridView1.DataSource = dtResults
'DataGridView1.Columns(1).AutoSizeMode = DataGridViewAutoSizeColumnMode.Fill
End If
btnSearch.Enabled = True
End Sub
The Image Column
Handle the RowsAdded event of the DGV as follow:
Private Sub DataGridView1_RowsAdded(sender As Object, e As DataGridViewRowsAddedEventArgs) Handles DataGridView1.RowsAdded
If DataGridView1.Columns.Count < 3 Then Return
'if you want to get rid of the default x image.
If e.RowIndex = 0 Then
DataGridView1.Rows(e.RowIndex).Cells("imgCol").Value = Nothing
End If
Dim path As String = DataGridView1.Rows(e.RowIndex).Cells(2).Value?.ToString
If Not String.IsNullOrEmpty(path) Then
If IO.File.Exists(path) Then
DataGridView1.Rows(e.RowIndex).Cells("imgCol").Value = Icon.ExtractAssociatedIcon(path).ToBitmap
Else
DataGridView1.Rows(e.RowIndex).Cells("imgCol").Value = My.Resources.Folder
End If
End If
End Sub
Where the My.Resources.Folder is an icon file of your choice for the folder entries.
Good luck.

Getting System.IO.IOException

I am working with the driving knowledge test program which consist of the multiple choice questions. I am having System.IO.IOException while trying to save the questions in the database file. Few hours earlier the same code was working fine but now it started to show the issue. The error is also mentioning that it cannot access the file and the file is being used by the another process. I also have added the picture to show the error that i faced
AddQuestions.vb
Imports System.IO
Public Class Questions
' Holds question number of question currently being updating or deleting.
Dim currentlyEditingDeleteingQueNo As Integer
Private Sub QuestionForm_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
TXTQuestionNo.Enabled = False
TXTQuestionNo.Text = gloTestQuestions.Count + 1
Fill_Grid()
End Sub
' Insert a new question in array list.
Private Sub BTNAdd_Click(sender As System.Object, e As System.EventArgs) Handles BTNAdd.Click
Dim question As Trivia_Data
question.intQuestionNo = TXTQuestionNo.Text
question.StrQuestion = TXTQuestion.Text
question.StrAnswer1 = TXTAnswer1.Text
question.StrAnswer2 = TXTAnswer2.Text
question.StrAnswer3 = TXTAnswer3.Text
question.StrAnswer4 = TXTAnswer4.Text
question.intCorrectAnswer = Integer.Parse(TXTAnswer5.Text)
gloTestQuestions.Add(question)
Fill_Grid()
MessageBox.Show("Question inserted successfully....")
ClearText()
End Sub
' Clears all text boxes for next question and displays possible question for it.
Private Sub ClearText()
TXTAnswer1.Clear()
TXTAnswer2.Clear()
TXTAnswer3.Clear()
TXTAnswer4.Clear()
TXTAnswer5.Clear()
TXTQuestion.Clear()
TXTQuestionNo.Clear()
TXTQuestionNo.Text = gloTestQuestions.Count + 1
End Sub
' Fills data grid view controls with available questions in a system.
Private Sub Fill_Grid()
Dim dt As DataTable
dt = New DataTable()
dt.Columns.Add("Question Number")
dt.Columns.Add("Question")
dt.Columns.Add("Answer1")
dt.Columns.Add("Answer2")
dt.Columns.Add("Answer3")
dt.Columns.Add("Answer4")
dt.Columns.Add("CorrectAnswer")
Dim question As Trivia_Data
For j = 0 To gloTestQuestions.Count - 1
dt.Rows.Add()
question = CType(gloTestQuestions(j), Trivia_Data)
dt.Rows(j)("Question Number") = question.intQuestionNo.ToString()
dt.Rows(j)("Question") = question.StrQuestion
dt.Rows(j)("Answer1") = question.StrAnswer1
dt.Rows(j)("Answer2") = question.StrAnswer2
dt.Rows(j)("Answer3") = question.StrAnswer3
dt.Rows(j)("Answer4") = question.StrAnswer4
dt.Rows(j)("CorrectAnswer") = question.intCorrectAnswer
Next
'DataGridView1.DataSource = dt
End Sub
' Searchs a question by user supplied question number using binary search technique.
Private Sub BTNFind_Click(sender As System.Object, e As System.EventArgs) Handles BTNFind.Click
Dim totalAddedQuestion As Integer = gloTestQuestions.Count
Dim questionNo = InputBox("Enter the question number that you want to search:")
If questionNo > gloTestQuestions.Count Or questionNo = 0 Then
MessageBox.Show("Question doesn't exist..........")
ClearText()
Exit Sub
Else
Dim compare As New ArrayListIntegerComparere
gloTestQuestions.Sort(compare)
Fill_Grid()
Dim max As Integer
Dim min As Integer
Dim mid As Integer
min = 0
max = totalAddedQuestion - 1
While (max >= min)
mid = (min + max) / 2
Dim question As Trivia_Data = CType(gloTestQuestions(mid), Trivia_Data)
If question.intQuestionNo < questionNo Then
min = mid + 1
ElseIf question.intQuestionNo > questionNo Then
max = mid - 1
Else
TXTAnswer1.Text = question.StrAnswer1
TXTAnswer2.Text = question.StrAnswer2
TXTAnswer3.Text = question.StrAnswer3
TXTAnswer4.Text = question.StrAnswer4
TXTAnswer5.Text = question.intCorrectAnswer
TXTQuestion.Text = question.StrQuestion
TXTQuestionNo.Text = question.intQuestionNo
currentlyEditingDeleteingQueNo = mid
MessageBox.Show("Question found............")
BTNUpdate.Enabled = True
BTNDelete.Enabled = True
Exit Sub
End If
End While
End If
MessageBox.Show("Question doesn't exist.................")
ClearText()
End Sub
' Saves questions in binary file and closes the file after writing operations.
Private Sub BTNSave_Click(sender As System.Object, e As System.EventArgs) Handles BTNSave.Click
Dim file As New FileStream(Directory.GetCurrentDirectory() & "\Question.dat", FileMode.Create, FileAccess.Write)
Dim fileWrite As BinaryWriter = New BinaryWriter(file)
Dim question As Trivia_Data
For i = 0 To gloTestQuestions.Count - 1
question = CType(gloTestQuestions(i), Trivia_Data)
fileWrite.Write(Convert.ToInt16(question.intQuestionNo))
fileWrite.Write(question.StrQuestion)
fileWrite.Write(question.StrAnswer1)
fileWrite.Write(question.StrAnswer2)
fileWrite.Write(question.StrAnswer3)
fileWrite.Write(question.StrAnswer4)
fileWrite.Write(Convert.ToInt16(question.intCorrectAnswer))
Next
fileWrite.Close()
file.Close()
MessageBox.Show("Questions saved in file successfully..........")
End Sub
' Updates exisitng question as per newly provided values.
Private Sub BTNUpdate_Click(sender As System.Object, e As System.EventArgs) Handles BTNUpdate.Click
Dim question As Trivia_Data = CType(gloTestQuestions(currentlyEditingDeleteingQueNo), Trivia_Data)
question.StrAnswer1 = TXTAnswer1.Text
question.StrAnswer2 = TXTAnswer2.Text
question.StrAnswer3 = TXTAnswer3.Text
question.StrAnswer4 = TXTAnswer4.Text
question.intCorrectAnswer = Integer.Parse(TXTAnswer5.Text)
question.StrQuestion = TXTQuestion.Text
gloTestQuestions(currentlyEditingDeleteingQueNo) = question
MessageBox.Show("Question updated successfully...................")
BTNUpdate.Enabled = False
BTNDelete.Enabled = False
ClearText()
Fill_Grid()
End Sub
' Deletes a current question from the arraylist.
Private Sub BTNDelete_Click(sender As System.Object, e As System.EventArgs) Handles BTNDelete.Click
gloTestQuestions.RemoveAt(currentlyEditingDeleteingQueNo)
Fill_Grid()
ClearText()
MessageBox.Show("Question removed successfully.....")
BTNDelete.Enabled = False
BTNUpdate.Enabled = False
End Sub
' Closes the Question form
Private Sub BTNClose_Click(sender As System.Object, e As System.EventArgs) Handles BTNClose.Click
Me.Close()
End Sub
End Class
Did you encounter any exceptions during testing which may have caused the file not being correctly closed (file.close()) by your own application?
Do you have the file (Question.dat) open in another program?
Does an instance of your application still run and keeps the file open?
When dealing with this sort of operations it is better to rewrite your code something like this :
EDIT VB .NET (pseudo code)
Try
Using file As New File
' your code...
End Using
Catch exception As Exception
' log and/or show to user...
Finally
' your code...
file.Close()
End Try
Original C# code
try{
// If the file class implements IDisposable
using(File file = new file()){
// your code...
...
}
}
catch(Exception ex){
// log and/or show to user
}
finally{
file.close();
}
It is hard to say what process/application has the file in use or locked so for now I should say, close and reopen Visual Studio or even worse restart Windows.

For Loop works not working, or giving error. Works when conditions are reversed

I don't know if its the late hour, but I am working on the following array For Loop:
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim pboxes() As PictureBox = {picMainImage, picImage2, picImage3, picImage4}
For i As Integer = 0 To pboxes.Count - 1
If pboxes(i).Image Is My.Resources.list Then
pboxes(i).Image = Nothing
End If
Next
End Sub
The loop is supposed to check if any of the picture boxes in the array have an Image called List stored on the Resources folder in them. If it does, set the image to Nothing. However, I run it and nothing happens., no errors, nothing.
So I reversed my For Loop as follows to see what happens:
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim pboxes() As PictureBox = {picMainImage, picImage2, picImage3, picImage4}
For i As Integer = 0 To pboxes.Count - 1
If pboxes(i).Image Is Nothing Then
pboxes(i).Image = My.Resources.list
End If
Next
End Sub
This works but is not what I want, I want the opposite.
What am I doing wrong here?
One option is if you set the pictures in the box programatically, set My.Resources.list to be referenced by a global variable, ie Public pbList = My.Resources.list
Then, when you set the picture initially, use that variable, so: picMainImage.Image = pbList
Finally, in your If statement, you should then be able to check If pboxes(i) is pbList Then...
Once it becomes a variable, it seems to become static and therefore wherever you use it, it will always be the same.
EDIT: some actual code that I used a few months back:
In the module (outside sub)
Public pbimage As System.Drawing.Image = My.Resources.placeholder
Then in the Sub
If imgpath <> "" Then
Me.lblImg.ImageLocation = imgpath
Else
Me.lblImg.ImageLocation = Nothing
Me.lblImg.Image = pbimage
End If
and then this is what I use for all pictures without issues (its a function that I have run when you click on an image - if its the placeholder then you can browse for an image and save it to a data folder, otherwise it does nothing)
Private Sub changeImg(sender As Object, e As MouseEventArgs) Handles {ALL YOUR IMAGES}.Click
If TypeOf sender Is PictureBox Then
If DirectCast(sender, PictureBox).Image Is pbimage Then
Dim ofd As New OpenFileDialog
ofd.Title = "Please select image"
ofd.Filter = "Image Files|*.jpg"
If ofd.ShowDialog() = Windows.Forms.DialogResult.OK Then
Dim rn As New Random
Dim r As Long = rn.Next(111111, 999999)
Dim newfilename As String = My.Settings.dataPath & r.ToString & Format(Now, "ddmmyy") & ".jpg"
Try
FileCopy(ofd.FileName, newfilename)
DirectCast(sender, PictureBox).ImageLocation = newfilename
Catch ex As Exception
MessageBox.Show("Check permissions to the Data folder", "Permissions error")
End Try
End If
End If
End If
End Sub
Images cannot be compared that way because the image is copied into memory and will always be different even if the pixels match. Compare the pixels directly to find out if the image is the same.
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim pboxes() As PictureBox = {picMainImage, picImage2, picImage3, picImage4}
For i As Integer = 0 To pboxes.Count - 1
If IsSameImage(pboxes(i).Image, My.Resources.list) = True Then
pboxes(i).Image = Nothing
End If
Next
End Sub
Public Function IsSameImage(ByVal oBitmap1 As Bitmap, ByVal oBitmap2 As Bitmap) As Boolean
For x = 0 To oBitmap1.Width - 1
For y = 0 To oBitmap2.Height - 1
If Not oBitmap1.GetPixel(x, y) = oBitmap2.GetPixel(x, y) Then
Return False
End If
Next
Next
Return True
End Function

Trying to change a timer once a button is pressed

I have a check going every 60 seconds, if the alarm is true it will open form2 play a song etc.
but the button on form2 should set the waittimer = 180000 which should make the next check occur half an half an hour later.
I'm not used to using timers, what am I doing wrong?
Public Class Form1
Public Property waittimer As Integer
Public Property playsong As Boolean = True
Private WithEvents Tmr As New Timer
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
If (Process.GetProcessesByName(Process.GetCurrentProcess.ProcessName).Length > 1) Then
Application.Exit()
End If
Tmr.Enabled = False
Tmr.Interval = TimeSpan.FromSeconds(60).TotalMilliseconds
BackgroundWorker1.RunWorkerAsync()
End Sub
Private Sub NotifyIcon1_MouseDoubleClick(sender As Object, e As MouseEventArgs) Handles NotifyIcon1.MouseDoubleClick
playsong = False
Try
' Create a request for the URL.
Dim request As WebRequest = _
WebRequest.Create("http://example.com/example.aspx")
' If required by the server, set the credentials.
request.Credentials = CredentialCache.DefaultCredentials
' Get the response.
Dim response As WebResponse = request.GetResponse()
' Display the status.
Console.WriteLine(CType(response, HttpWebResponse).StatusDescription)
' Get the stream containing content returned by the server.
Dim dataStream As Stream = response.GetResponseStream()
' Open the stream using a StreamReader for easy access.
Dim reader As New StreamReader(dataStream)
' Read the content.
Dim responseFromServer As String = reader.ReadToEnd()
' Display the content.
Console.WriteLine(responseFromServer)
' Clean up the streams and the response.
reader.Close()
response.Close()
Dim responseArray() As String
responseArray = Split(responseFromServer, "|")
Dim D As New Data
D.maxcalls = responseArray(0)
D.cph = responseArray(1)
D.mht = responseArray(2)
D.alarm = responseArray(3)
Form2.startsong(D)
Catch ex As Exception
End Try
playsong = True
End Sub
Private Sub ExitToolStripMenuItem_Click(sender As Object, e As EventArgs) Handles ExitToolStripMenuItem.Click
Application.Exit()
End
End Sub
Private Sub BackgroundWorker1_DoWork(sender As Object, e As System.ComponentModel.DoWorkEventArgs) Handles BackgroundWorker1.DoWork
Try
If waittimer = 180000 Then
Tmr.Interval = waittimer
Tmr.Start()
waittimer = 60000
Else
waittimer = 60000
End If
' Create a request for the URL.
Dim request As WebRequest = _
WebRequest.Create("http://example.com/example.aspx")
' If required by the server, set the credentials.
request.Credentials = CredentialCache.DefaultCredentials
' Get the response.
Dim response As WebResponse = request.GetResponse()
' Display the status.
Console.WriteLine(CType(response, HttpWebResponse).StatusDescription)
' Get the stream containing content returned by the server.
Dim dataStream As Stream = response.GetResponseStream()
' Open the stream using a StreamReader for easy access.
Dim reader As New StreamReader(dataStream)
' Read the content.
Dim responseFromServer As String = reader.ReadToEnd()
' Display the content.
Console.WriteLine(responseFromServer)
' Clean up the streams and the response.
reader.Close()
response.Close()
Dim responseArray() As String
responseArray = Split(responseFromServer, "|")
Dim D As New Data
D.maxcalls = responseArray(0)
D.cph = responseArray(1)
D.mht = responseArray(2)
D.alarm = responseArray(3)
D.forcealarm = False
e.Result = D
Catch ex As Exception
Dim D As New Data
D.maxcalls = 404
D.cph = 404
D.mht = 404
D.alarm = True
D.forcealarm = True
e.Result = D
End Try
Tmr.Interval = waittimer
End Sub
Private Sub BackgroundWorker1_RunWorkerCompleted(sender As Object, e As System.ComponentModel.RunWorkerCompletedEventArgs) Handles BackgroundWorker1.RunWorkerCompleted
If Not IsNothing(e.Result) Then
Dim strtime As String
Dim englandtime = System.TimeZoneInfo.ConvertTime(Now, TimeZoneInfo.FindSystemTimeZoneById("UTC"))
strtime = DatePart(Microsoft.VisualBasic.DateInterval.Hour, englandtime) & DatePart(Microsoft.VisualBasic.DateInterval.Minute, englandtime)
Dim D As Data = DirectCast(e.Result, Data)
If D.forcealarm = True Then
Form2.startsong(D)
End If
If D.alarm Then
If strtime < 730 And strtime > 0 Then
Else
Form2.startsong(D)
End If
End If
End If
Tmr.Start()
End Sub
Private Sub Tmr_Tick(sender As Object, e As System.EventArgs) Handles Tmr.Tick
Tmr.Stop()
BackgroundWorker1.RunWorkerAsync()
End Sub
End Class
Public Class Data
Public Property maxcalls As Integer
Public Property cph As Integer
Public Property mht As Integer
Public Property alarm As Boolean
Public Property forcealarm As Boolean
End Class
Form2
Public Class Form2
Public audio As New AudioFile(My.Application.Info.DirectoryPath & "\Help.mp3")
Public Sub startsong(ByVal D As Data)
Me.Show()
MHTvalue.Text = D.mht.ToString
Maxcallsvalue.Text = D.maxcalls
CPHvalue.Text = D.cph
If Form1.playsong = True Then
audio.Play()
End If
End Sub
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
' Me.Close()
audio.Stop()
Form1.waittimer = 180000
End Sub
Private Sub Form2_Closing(sender As Object, e As EventArgs) Handles MyBase.Load
audio.Stop()
Form1.waittimer = 180000
End Sub
Private Sub Form2_Minimize(sender As Object, e As EventArgs) Handles MyBase.Load
audio.Stop()
Form1.waittimer = 180000
End Sub
Thats probably because your timer on the original form1 instance never get re configured by your code. You are instead setting a timer on a new copy of form 1 that is not running (showing).
You need to get the running instance of form1 that you originally opened. A technique I use for something like this, is to tag my form I will be looking for, and then stepping through the form instances currently running, getting my form via its tag, setting that found form equal to the object I am working with, and THEN use the properties/methods of that form.
Set your form1's tag to "myTag".
Then on form 2:
For Each f As Form In My.Application.OpenForms
If Not f.InvokeRequired Then
'Can access the form directly.
If f.Tag = "myTag" Then
'this is my form
Dim fcast As New form1
fcast = f
fcast.<---- use your methods / properties...
End If
End If
Next
Just change:
Public Property waittimer As Integer
To:
Public Property waittimer As Integer
Get
Return Tmr.Interval
End Get
Set(value As Integer)
If value > 0 Then
Tmr.Interval = value
End If
End Set
End Property
You were changing the waittimer value, but not the actual Interval() of the Timer as well. This links the two together.

Iteration of a text file in Visual Basic

Ok, so I'm making a program that will read from a text file, convert each line into an array, the. Send each of those lines 1 per tick. This is in 2010 Visual Basic.
Closest I've gotten is sending all at once, I worked on it over night and am slowly destroying it.
Ideally, I want Button 1 click to populate the array from the file at LocationTB then start the timer. The timer should send a line at a time on the GapTB interval.
Public Class Form1
Public TextLine As String
Public MyFileName As String
Public MyNewLine(1000) As String
Private Property z As Integer
Private Property objReader As Object
Public Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
If RadioButton2.Checked = True Then
Dim Textline As String = ""
Dim FILE_NAME As String = LocationTB.Text
Dim objReader As New System.IO.StreamReader(FILE_NAME)
MyFileName = LocationTB.Text
FileOpen(1, MyFileName, OpenMode.Input, OpenAccess.Read, OpenShare.Shared)
z = 0
Do Until EOF(1)
MyNewLine(z) = LineInput(1)
z = z + 1
Loop
FileClose(1)
End If
Timer1.Interval = GapTB.Text
Timer1.Enabled = True
End Sub
Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
If RadioButton1.Checked = True Then
AppActivate(Hook.Text)
SendKeys.Send(SimpleTB.Text)
SendKeys.Send((Chr(13)))
ElseIf RadioButton2.Checked = True Then
For Each
TextLine = TextLine & objReader.ReadLine
AppActivate(Hook.Text)
SendKeys.Send(TextLine)
SendKeys.Send((Chr(13)))
Next
Else
MsgBox("File Does Not Exist")
End If
End Sub
Is this the kind of thing you're looking for?
It'll write the contents of a file (in this instance "C:\mark.txt") to the output window in Visual Studio.
Public Class Form1
Private myTimer As Timer
Private lines As String()
Private currentLine As Integer
Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
lines = IO.File.ReadAllLines("C:\mark.txt")
currentLine = 0
myTimer = New Timer()
AddHandler myTimer.Tick, AddressOf myTimer_Tick
myTimer.Interval = 1000
myTimer.Enabled = True
End Sub
Private Sub myTimer_Tick(sender As Object, e As EventArgs)
If currentLine < lines.Count Then
Dim lineToSend As String = lines(currentLine)
Debug.Print(lineToSend)
currentLine += 1
Else
myTimer.Enabled = False
End If
End Sub
End Class
Caveat
The above code isn't scalable. If you're SURE the file will always be small then it will do (that said, they're never always small).
To make this scalable you'd need to hold the file open, and read each line as you need it, not load the entire file contents at once.
I know how to do it to a application I started myself: Here's the code to make Calculator work:
Dim ProcID As Integer
' Start the Calculator application, and store the process id.
ProcID = Shell("CALC.EXE", AppWinStyle.NormalFocus)
' Activate the Calculator application.
AppActivate(ProcID)
' Send the keystrokes to the Calculator application.
My.Computer.Keyboard.SendKeys("22", True)
My.Computer.Keyboard.SendKeys("*", True)
My.Computer.Keyboard.SendKeys("44", True)
My.Computer.Keyboard.SendKeys("=", True)
' The result is 22 * 44 = 968.
AppActivate should know the exact title of the form to activate. If not known, iterate the Processes to locate yours. If the title is correctly specified it should work