Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Adding Multithreading to Working App with Separate Class

Status
Not open for further replies.

joshmshrf

Programmer
May 22, 2008
8
US
I have an application that I am developing that will process files based on a database table.

Currently I'm developing my solution with multiple projects in VS 2008. I have 1 project for the base class with all of the processing code in it. Then I have 2 more projects (1 for a thick-app and 1 for a service).

The base class has the ability to process multiple items (1 at a time) before finishing the routine and returning to the service or thick app. Unfortunately a plugin I'm using does not have the ability to work as multithreaded and I have to process 1 item at a time.

I'm trying to add multithreading support to the base class so that I can have the thick-app update a progress bar to let a user know how far along it is through its queue of items to process.

I've done some testing and got the multithreading to work in the thick app for updating a text box and progress bar. I'm using a call to control.Invoke to update the properties of the control successfully. The data I want to use to update it with needs to come from the separate class.

Here are my questions/hangups so far:
1. How to get the separate class to update the UI from the class itself.
2. How to use a backgroundworker in the UI to link to a backgroundworker in the separate class.
3. How to use the ReportProgress from the separate class back to the UI for the user to see.
4. How to keep my code for processing the items in the separate class as opposed to moving the code to the UI. I would prefer to only have 1 codebase.

Any assistance that can be provided would be very helpful.

Thanks,

Josh
 
You don't always need a multi-threaded client for a responsive system. If the helper classes are working in little bits, and your form is sending DoEvents() in between then you can usually fake a responsive system.

In addition you should look at binding controls, you can have a property with a percent done calculation, and tie the progress bar directly to that. Watch out for 1/0 and 1/1 though...

-Sometimes the answer to your question is the hack that works
 
.
Unfortunately a plugin I'm using does not have the ability to work as multithreaded and I have to process 1 item at a time.
a plug-in or a 3rd party control? If it is a 3rd party control then generally you should have to instantiate the class before you can use it. Because of this fact generally you can thread just about any class.

1. How to get the separate class to update the UI from the class itself.

I've done some testing and got the multithreading to work in the thick app for updating a text box and progress bar. I'm using a call to control.Invoke to update the properties of the control successfully. The data I want to use to update it with needs to come from the separate class.
I'm kind of confused from what you said it seemed like you have fixed this one.


3. How to use the ReportProgress from the separate class back to the UI for the user to see.
Separate class or separate thread? It makes a difference.

Code:
4.  How to keep my code for processing the items in the separate class as opposed to moving the code to the UI.
I may be dense today, but I'm not getting what you are asking here.

Some code on what you are doing so far could help in answering some questions. Though it is not the preferred method like Qik3Coder said you can fake it with some DoEvents. I still have to do it sometimes because of timing issues between two 3rd party controls

-I hate Microsoft!
-Forever and always forward.
-My kingdom for a edit button!
 
After doing some more research on this I found a way to do what I needed, with one exception. I could have used the DoEvents but I am using this project as a chance to learn about multithreading.

The plugin I use is an external app that I have to access through a .bat file. It then will take a file and convert it to TIF for me. It works the best out of all that I have tested and they use a printer to do th conversion. This means that I can't process multiple items at the same time.

For my UI I created a BackgroundWorker object and use that to start the processing in another thread. Before I start the processing, in the DoWork event I create another BackgroundWorker object and set it to be the existing one and pass it into my class.

From inside the class I can use the passed object's .ReportProgress function and it will trigger the ProgressChanged event in the main UI. This allows me to keep all of my core code in my class and not in the UI.

The only issue I have with this is that if I try to cancel the passed BackgroundWorker object it will cancel properly but when the RunWorkerCompleted does not detect the object as being cancelled. It just sees it as being completed. It's not that big of a deal at this point, but it would be nice to have it recognize the cancel properly.

I can provide some code if you have questions.

Thanks,

Josh
 
I don't know that much about BackgroundWorker as I just use Threads and Delegates. I thought though that the BackgroundWorker had a property that told if it had been canceled (I could be thinking of something else though). In the RunWorkerCompleted you could check that property. If not then make a global variable and set that to something before you cancel and then check it in the RunWorkerCompleted event.

-I hate Microsoft!
-Forever and always forward.
-My kingdom for a edit button!
 
The RunWorkerCompleted event has the flag for cancelled as part of the arguments that are passed in. Somehow those are not getting all the way back to the Main UI.

The simple idea of a global variable would work. Sometimes the simple just gets overlooked.

Thanks,

 
It happens. I call it code blindness. You are just looking at your code so long you just don't think about some things anymore. I do it all the time. My mind gets so worked up that I start thinking of more and more complex ways of doing things. I then come back the next day and wonder what I was thinking. :)

-I hate Microsoft!
-Forever and always forward.
-My kingdom for a edit button!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top