Advertisement
6_2008-2009 Miscellaneous #206106

Simple Multi Thread Example

I've had a few people come to me with how to do multi treading. I've looked around PSC and there are plenty of projects that have multi-threading but none solely for multi-threading. So I did up a simple multi-threading console application that passes data back to the parent thread via events. This is a very simple example and is designed to show how multi-threading can be used. The key to understanding what you would use multi-threading for is to keep one simple thing in mind. All UI (user interfaces) elements must be in the parent thread. You can not have anything that interacts direct with the UI in a seperate thread. For example if you are running a long business process and get an exception, then use a message box to show that exception then nothing will ever be shown to the user. It's in a background thread so the user never see's it. It can also lock the application up since the messagebox is waiting for the user to click ok. What you would do in such an example would be to pass the error message back to the parent thread and let the parent thread display the error message. This is also a good example to show how you could update a progress bar on the parent thread with the process of the thread (like how far along the process has gotten in getting or doing something). Hope this helps. Sometimes you just need to break something down to bare basics to get a good understanding of it. Though this is only one example of how to work with threads, there are many other ways.

AI

Résumé par IA: This codebase represents a historical implementation of the logic described in the metadata. Our preservation engine analyzes the structure to provide context for modern developers.

Code source
original-source
Upload
Commentaires originaux (3)
Récupéré via Wayback Machine