|
-
Mar 29th, 2010, 07:10 PM
#11
Re: Using the BackgroundWorker Component
 Originally Posted by cengineer
I have tried to adapt your code to a console application and it seems to working as far as running the background worker, and I do have the ProgressChanged and RunWorkerCompleted functions to monitor it, the process ends after one cycle of its do work process.
Code:
While Not response.Equals("3")
Try
Console.Write("Enter choice: ")
response = Console.ReadLine()
Console.WriteLine()
If response.Equals("1") Then
Console.WriteLine("Thread 1 doing work" & vbNewLine)
engine.LoadFile()
'tm.SetApartmentState(ApartmentState.STA)
'tm.Start()
response = String.Empty
ElseIf response.Equals("2") Then
Console.WriteLine("Starting a second Thread")
'ts.Start()
report()
response = String.Empty
End If
'ts.Join()
'tm.Join()
Catch ex As Exception
as you can see the commented parts was an attempt to try threads; wasnt successful.
Here is where the background worker is being called from, sub engine.loadfile()
Code:
bgw.WorkerReportsProgress = True
AddHandler bgw.DoWork, New DoWorkEventHandler(AddressOf Search)
bgw.RunWorkerAsync()
AddHandler bgw.ProgressChanged, New ProgressChangedEventHandler(AddressOf ProgressChanged)
AddHandler bgw.RunWorkerCompleted, New RunWorkerCompletedEventHandler(AddressOf RunWorkerComplete)
the actual do work process
Code:
Private Sub Search(ByVal sender As Object, ByVal e As DoWorkEventArgs)
...
BloObj.Main(connectionString, userAgent, fileName, clientName, clientProj)
Dim i As Integer
For i = 1 To 100
CType(sender, BackgroundWorker).ReportProgress(i)
Thread.Sleep(100)
Next
Console.ResetColor()
Console.Write("Search Complete, Do you wish to generate queries, y or n? ")
reply = Console.ReadLine()
If reply = "y" Then
report()
ElseIf reply = "n" Then
Console.Clear()
main()
End If
Catch ex As Exception
errormessage = ex.Message
End Try
Any ideas appreciated.
Your code doesn't really make sense to me. I don't see why you have a BGW at all. You seem to be prompting the user for data from the background thread. Usually the point of background threads is to perform time-consuming processing in the background so that the user can still interact with the application in the foreground. What's the point of having multiple threads in your case?
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|