Receive Events from separate thread

B

Ben white

I did this before but can't remember how - please help. This is the
context:
1) .NET 1.1
2) Main form has a Task List Box, Go button, Cancel Button, Errors
List Box
3) Task list may have 1 to n tasks described
4) Tasks may be long running
5) When task launches it shows a "progress form" and closes it when
the task is complete
6) There is a Job object that performs the task.
7) When the user presses the Go button the tasks in the list box are
executed.

What I want it to do:
1) While task is running send "error objects" back to the main form to
be added to the "errors" list box
2) be able to cancel a job while it is running.

There are two problems:
1) If I launch the job as a method (Job.DoYourself), I can not cancel
it as the UI on Main is inaccessible while task finishes it but it
does receive Errors. If I launch a job as a thread the buttons are
available until I attempt to put/send an error to the list box where
it simply hangs (it appears that the thread dies). I have tried
various forms of using delegates with the same result.
2) regardless of method the Progress UI stops responding after 4 or
five minutes.

I know I am missing something small as I am fairly certain I have done
this before - I just can't seem to remember where I did it. I would
love an article that has both how and why but any help would certainly
be appreciated.
 
B

Bill McCarthy

Hi Ben,

You run the length task on another thread. I'd write it in a seperate class
and have it raise events etc, and also have a public Cancel property which
you can set. It would periodically check the cancel flag and if it is set
cancel and clean up. The event notification would give you a progress value
if applicable. In your form's code that handles the event you need to use
Me.Invoke(...) and pass the information over to a method which will be then
invoked on the Form's UI thread.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top