I'm writing a batch publishing program in VB.NET using VS2008. My program is a Windows form.
The program spawns a process called "xtop". The process converts one CAD format to another. The conversion process demands heavy CPU cycles.
The user will add "jobs" to the batch. The batch program will send one job to the publisher at a time. Each job is to be published using a worker thread in the background to make sure the UI (Windows form) doesn't get locked up. Its important that users can add jobs to the batch 24/7 while jobs are publishing in the background.
I've never written a multi-threaded app in VB.NET. I'm having some difficulty formulating a strategy.
I was planning to call the background worker thread using:
Dim pubThread As Thread = New Thread(AddressOf Me.backgroundPublishing)
pubThread.Start()
Then in a private sub called backgroundPublishing() I was going to write this:
Using p As New Process
With p.StartInfo
.FileName = proe
.Arguments = cmd
.UseShellExecute = False
.CreateNoWindow = True
.RedirectStandardInput = True
.RedirectStandardOutput = True
End With
p.Start()
p.EnableRaisingEvents = True
Debug.WriteLine(cmd)
p.WaitForExit()
p.Close()
End Using
The problem is that the main thread controls the batching. It basically loops through all the jobs that have been added to the batch (a list box).
I was planning to "freeze" the loop with a nested while loop that "watches" my "xtop" process until the background worker process is done. This will prevent multiple jobs from being sent to the worker thread (and multiple "xtop" processes from spawning).
While Process.GetProcessesByName("xtop").Length > 0
'xtop process running
End While
Thus, once the first job is run (and the corresponding "xtop" process ends in the worker thread) program control resumes with and the next job is sent to the worker thread.
There is one major problem though. The While loop "feezes" the UI (Windows form) so that new jobs can't be added. Which, of course, defeats the purpose of sending the worker process to its own thread.
Can someone please explain a good way to program something like this? I'd simply like to make the UI accessible at all times while running CPU intensive CAD conversions in the background. Thank you!