Creating listbox job/task queue

  • Thread starter Thread starter kimiraikkonen
  • Start date Start date
K

kimiraikkonen

Hello,
For example i have listbox with 10 items.

how can i create a job queue (automated)? I used "for each" but after
processing 5 items, it stopped, why?

Thanks.
 
Hello,
For example i have listbox with 10 items.

how can i create a job queue (automated)? I used "for each" but after
processing 5 items, it stopped, why?

Thanks.

Without you showing us any code it's very difficult to diagnose the
problem. My first guess is that you are removing the items from the
listbox in the foreach loop, which will modify the enumerator and
cause you to only process every-other item. But like I said, without
seeing your code it's difficult to say for sure.

Thanks,

Seth Rowe
 
Hi Seth,
In fact, i have listbox [too many topic i' opened recently :-) ] , and
the selected items of listbox is a parameter of my external process, i
want to create a "batch" processing program which process TILL the end
of listbox index NON-STOP/no user-prompt. Classic batch process of
programs on the earth :)

Dim psInfo As New System.Diagnostics.ProcessStartInfo("external.exe",
listbox1.selecteditem)
psInfo.WindowStyle =
System.Diagnostics.ProcessWindowStyle.Normal
psInfo.WorkingDirectory = Application.StartupPath
psInfo.WindowStyle = ProcessWindowStyle.Normal
Dim myProcess As Process =
System.Diagnostics.Process.Start(psInfo)
myProcess.WaitForExit()
If myProcess.HasExited = True Then
Try
Me.ListBox1.SelectedIndex =
Me.ListBox1.SelectedIndex + 1
System.Diagnostics.Process.Start("lame.exe", "
Catch
MsgBox("All files have been converted",
MsgBoxStyle.Information, "Done!")
End Try


But as you see after "try" , the program process the next item then it
stops as normal, but i want to do a batch process TILL the end of
listbox's ALL items. (all the listbox items, one by one).

I hope you understood.

Very thanks...
 
Hi Seth,
In fact, i have listbox [too many topic i' opened recently :-) ] , and
the selected items of listbox is a parameter of my external process, i
want to create a "batch" processing program which process TILL the end
of listbox index NON-STOP/no user-prompt. Classic batch process of
programs on the earth :)

Dim psInfo As New System.Diagnostics.ProcessStartInfo("external.exe",
listbox1.selecteditem)
psInfo.WindowStyle =
System.Diagnostics.ProcessWindowStyle.Normal
psInfo.WorkingDirectory = Application.StartupPath
psInfo.WindowStyle = ProcessWindowStyle.Normal
Dim myProcess As Process =
System.Diagnostics.Process.Start(psInfo)
myProcess.WaitForExit()
If myProcess.HasExited = True Then
Try
Me.ListBox1.SelectedIndex =
Me.ListBox1.SelectedIndex + 1
System.Diagnostics.Process.Start("lame.exe", "
Catch
MsgBox("All files have been converted",
MsgBoxStyle.Information, "Done!")
End Try

But as you see after "try" , the program process the next item then it
stops as normal, but i want to do a batch process TILL the end of
listbox's ALL items. (all the listbox items, one by one).

I hope you understood.

Very thanks...

How about this?

Just add this to the code-behind for Form1 in a new Windows
Application and run the application.

/////////////
Imports System.Diagnostics

Public Class Form1

Private listBox As ListBox
Private button As Button

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load
'// Create our listbox
listBox = New ListBox()
Me.Controls.Add(listBox)
listBox.Location = New Point(3, 3)

'// Add some values to the listbox
listBox.Items.Add("www.microsoft.com")
listBox.Items.Add("www.google.com")
listBox.Items.Add("www.yahoo.com")
listBox.Items.Add("www.newegg.com")

'// Create a button to start the batch
button = New Button()
button.Text = "Start Processes"

Me.Controls.Add(button)
button.Location = New Point(12, 105)
button.Size = New Size(100, 23)

AddHandler button.Click, AddressOf button_Click
End Sub

Private Sub button_Click(ByVal sender As Object, ByVal e As
EventArgs)
Dim index As Integer = listBox.SelectedIndex

'// Don't process anything if the user didn't select an item
If index < 0 Then Return

Try
'// Prevent a new item from being selected
'// or the user starting the process again
listBox.Enabled = False
button.Enabled = False

While listBox.Items.Count > index
Try
Dim startInfo As New ProcessStartInfo("ping.exe",
listBox.Items(index).ToString())
startInfo.WindowStyle = ProcessWindowStyle.Normal
startInfo.WorkingDirectory =
Application.StartupPath

Dim p As Process = Process.Start(startInfo)

While Not p.HasExited
'// Do some lazy, fake multithreading
Application.DoEvents()
End While

'Process.Start("lame.exe")
Finally
listBox.Items.Remove(listBox.Items(index))
End Try
End While

MessageBox.Show("All files have been converted", "Done!",
MessageBoxButtons.OK, MessageBoxIcon.Information)
Finally
'// Re-enable the ListBox and Button
listBox.Enabled = True
button.Enabled = True
End Try
End Sub

End Class
/////////////

It starts processing items at the SelectedIndex and removes them after
it's finished.

By the way, in your code you are misusing the "Catch" statement. Catch
is used to handle an exception, so in essence what you are doing is
telling the user that everything is finished whenever you have an
error.

Thanks,

Seth Rowe
 
By the way, in your code you are misusing the "Catch" statement. Catch
is used to handle an exception, so in essence what you are doing is
telling the user that everything is finished whenever you have an
error.

Thanks,

Seth Rowe

Yes, but i know "catch" is used to handle exceptions, but i used
"catch" as informational because the error occurs due to finish of
listbox index. So calling it as "error" annoys user for nothing, the
reason of finishing of index.

For the codes which all the guys have posted, after i try i'll inform.

Very thanks for sharing codes, experiences...
 
but i used "catch" as informational because the error occurs due to
finish of listbox index.

ACK!!!!

Don't do that! Exceptions are very expensive to throw and catch and
should be avoided whenever possible. Instead you should be inspecting
the index value before trying to increment the SelectedIndex of the
ListBox. This way you know if an error would occur, and can prevent it
from happening.

Thanks,

Seth Rowe
 
ACK!!!!

Don't do that! Exceptions are very expensive to throw and catch and
should be avoided whenever possible. Instead you should be inspecting
the index value before trying to increment the SelectedIndex of the
ListBox. This way you know if an error would occur, and can prevent it
from happening.

Thanks,

Seth Rowe

Seth, OK thanks for waning me, maybe you could put an informational
code block than indicates the program has reached the end of listbox
instead of "catch".

Thanks

kimiraikkonen
 
This is a IMO a very bad idea. Generally you use a for each loop that
enumerates all elements or a value that gives the last index that should be
used (generally a property called "Count"," Length" etc... depending on what
you are using) so you are generally doing something like :

For i=0 To Elements.Count-1
' Do something with Elements(i)
Next

I would suggest to read at least once the language specification so that you
can familiarize yourself with language constructs. For example loops are at
:
http://msdn2.microsoft.com/en-us/library/aa711983(VS.71).aspx
 
Seth, OK thanks for waning me, maybe you could put an informational
code block than indicates the program has reached the end of listbox
instead of "catch".

Thanks

kimiraikkonen

If you look at the code I gave you, it already does this. As soon as
the while loop is finished (when the index exceeds or equals the
number of objects) it will display the same messagebox that you had in
the Catch block of your code.

Thanks,

Seth Rowe
 
kimiraikkonen,

Have you tried using a For Next or a For Each loop?

Hi Kerry,

Could you define sample code for "for each loop"?


Seth, i'll try the code you've given.

Thanks both.
 
Back
Top