![]() |
|
![]() |
|
|
Thread Tools | Display Modes |
|
|
#1 |
|
Professional Programmer
Join Date: Jan 2006
Location: Ontario, Canada
Posts: 368
Rep Power: 0
![]() |
When do you guys use multi-threading in your windows applications in C#? I know how to use the different threading libraries in C#, but never find good situations to use them that a timer control can't take care of. So I was just wondering what situations you guys use multi-threading in?
__________________
I am Addicted to Linux! |
|
|
|
|
|
#2 |
|
Expert Programmer
|
Re: Threading
I've never had to use multi-threading.
__________________
|
|
|
|
|
|
#3 |
|
PFO Founder
![]() ![]() |
Re: Threading
I used multi-threading on a program that I wrote that read information for a external scale and had to do a count of the number of items on the scale. The reason I used it was so the window wasn't froze and could still close the window after they were done dumping parts on the scale. Since there was no way to know when the user would stopped putting items on the scale, so it would constantly read from the scale until the user closed the window and then the thread would be killed.
Hope that makes sense ![]()
__________________
BIG K aka Kyle Programming Forums Kyle K Online Please do not PM or email me programming questions. Post them in the forums instead. |
|
|
|
|
|
#4 |
|
Expert Programmer
Join Date: May 2005
Location: East Lansing, MI
Posts: 663
Rep Power: 4
![]() |
Re: Threading
I've used timers to reset the color of a button after some time without freezing the window so that I can still accept user input while waiting for the color to reset. I don't know if that counts as multi-threading...
|
|
|
|
|
|
#5 |
|
Professional Programmer
Join Date: Jan 2006
Location: Ontario, Canada
Posts: 368
Rep Power: 0
![]() |
Re: Threading
Big K - That makes perfect sense.
OpenLoop - That does count as multi-threading, but I was referring more to the actual Thread class in C#. I have used the timer control numerous times (can be quite handy). The reason I ask for places to use it is that I have a fairly big application that uses a lot of the different advanced features of C# and would be good to showcase to potential employers, and I wanted a good reason to use the thread class. I have come across a few good places to uses them, but it turns out there is always a better, more simple way to achieve that task without threading.
__________________
I am Addicted to Linux! |
|
|
|
|
|
#6 |
|
Troll
Join Date: Apr 2005
Location: Texas
Posts: 732
Rep Power: 4
![]() |
Re: Threading
The timer control isn't actually 'multithreading.' The OS posts a message to the message loop on the main thread at the specified interval. Only one thread actually exists. The timer going off is entirely dependent on your main thread returning to the message loop and checking for messages. (Form/control events are raised by the message loop in response to window messages. The reason the form will 'freeze up' when you put something time consuming in an event handler is because it can't check for messages until the handler returns).
The other timer classes (such as in System.Threading) actually spawn a background thread which sleeps until it is supposed to go off. This will work regardless of if your main thread is busy, but you then have to deal with the issues associated with multiple threads. Sometimes it makes sense to do more than one thing at once. Perhaps you have an algorithm that needs to take advantage of multiple cores/processors. Or perhaps you have a single processor system, but want to use the processor as effectively as you can. Just as one running program will take advantage of another running program sleeping, using multiple threads lets the OS scheduler give you time where you would otherwise not have any. Say you are reading and processing a large file. Reading from file is a time consuming operation. You don't know how busy the disk is or how far it has to seek. Your request might be at the end of a rather long queue. Even in the best case, we're talking milliseconds. A millisecond is a long time. With a single thread, while you're waiting on data from the hard drive, you can't process any data. And while you're processing data, you can't queue any read requests, so the disk may well spin idle. Say you have two threads, one which reads the file in some kind of logical chunks and posts the each chunk to a shared queue, and another which does processing chunk by chunk, grabbing each chunk from the shared queue. And perhaps you have a 3rd thread handling the user interface. Every 50th chunk, the processing thread posts a message to update a progress bar. But the rest of the interface code can be written as if the background operations don't even exist.
__________________
MD5(sig) = bcef75433db02e9ad9bf81d6f7c5c270 |
|
|
|
![]() |
| Bookmarks |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Display Modes | |
|
|
Similar Threads
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| C# Threading Resource | Dameon | C# | 4 | Jun 23rd, 2007 2:39 PM |
| Threading with Hashtable class | ssrun | Java | 1 | Mar 23rd, 2007 6:41 PM |
| .NET threading & memory | hbe02 | Other Programming Languages | 0 | Sep 28th, 2006 6:53 AM |
| Threading + Progress | tayspen | C# | 9 | Dec 16th, 2005 11:50 AM |
| help on threading | lucifer | C# | 4 | Dec 2nd, 2005 2:48 PM |