Click here to Skip to main content
16,018,418 members
Home / Discussions / C#
   

C#

 
GeneralRe: Why I hate C# Pin
Skippums29-Nov-07 4:13
Skippums29-Nov-07 4:13 
GeneralRe: Why I hate C# Pin
PIEBALDconsult29-Nov-07 5:00
mvePIEBALDconsult29-Nov-07 5:00 
GeneralRe: Why I hate C# Pin
Skippums29-Nov-07 5:03
Skippums29-Nov-07 5:03 
AnswerRe: Why I hate C# Pin
Bino B28-Nov-07 15:39
Bino B28-Nov-07 15:39 
AnswerRe: Why I hate C# Pin
Skippums29-Nov-07 4:10
Skippums29-Nov-07 4:10 
QuestionGetting the names of files have extension of mp3 from filesystem?? Pin
omegazafer28-Nov-07 11:57
omegazafer28-Nov-07 11:57 
AnswerRe: Getting the names of files have extension of mp3 from filesystem?? Pin
Anthony Mushrow28-Nov-07 12:10
professionalAnthony Mushrow28-Nov-07 12:10 
GeneralRe: Getting the names of files have extension of mp3 from filesystem?? Pin
Jared Bienz [MSFT]28-Nov-07 12:33
Jared Bienz [MSFT]28-Nov-07 12:33 
Actually, Application.DoEvents isn't really the best way to handle that. You're absolutely right that the application should yield to the UI thread so the application remains responsive, but Application.DoEvents forces the application message pump to run and isn't recommended.

Ideally the code above should be performed on a separate thread from the main UI thread. There are many ways to do that including creating a new thread, using the ThreadPool or asynchronous execution of a delegate (delegate.BeginInvoke / EndInvoke). But one of the coolest ways to execute code off the UI thread is the Dispatcher class. You can use the Dispatcher to execute code with various priority levels and even postpone execution until the application is idle. Note that Dispatcher is new to .Net 3.0, so if you're tied to 1.x or 2.0 then you won't be able to use it. You can read about the Dispatcher here[^].

Unfortunately, once this code is running on a separate thread you still have to get back to the UI thread to update controls. If you have a listbox, for example, and you want to add a ListboxItem for each file you find you won't be able to do it from the worker thread. You can get around that using Control.Invoke, but there's an even easier way if you're in .Net 3.0 or later. That's the BackgroundWorker[^] component, which will help you in easily setting up the background thread to execute your code and even gives you a very easy way to communicate back to the UI thread. BackgroundWorker is certainly the easiest, but if it's too restrictive for what you're trying to do make sure you check out some of those other options.

Regards,
Jared Bienz

My posts may include factual data, educated guesses, personal opinion and dry humor. They should not be treated as an official Microsoft statement.
Sites of Interest: MSDN Events | US ISV Team Blog

AnswerRe: Getting the names of files have extension of mp3 from filesystem?? Pin
Skippums28-Nov-07 12:22
Skippums28-Nov-07 12:22 
GeneralRe: Getting the names of files have extension of mp3 from filesystem?? Pin
Anthony Mushrow28-Nov-07 12:26
professionalAnthony Mushrow28-Nov-07 12:26 
AnswerRe: Getting the names of files have extension of mp3 from filesystem?? Pin
Luc Pattyn28-Nov-07 12:28
sitebuilderLuc Pattyn28-Nov-07 12:28 
GeneralRe: Getting the names of files have extension of mp3 from filesystem?? Pin
Jared Bienz [MSFT]28-Nov-07 12:36
Jared Bienz [MSFT]28-Nov-07 12:36 
GeneralRe: Getting the names of files have extension of mp3 from filesystem?? Pin
Luc Pattyn28-Nov-07 21:50
sitebuilderLuc Pattyn28-Nov-07 21:50 
AnswerRe: Getting the names of files have extension of mp3 from filesystem?? Pin
omegazafer28-Nov-07 12:41
omegazafer28-Nov-07 12:41 
QuestionIdentifier Expected Pin
murtle328-Nov-07 11:31
murtle328-Nov-07 11:31 
AnswerRe: Identifier Expected Pin
Luc Pattyn28-Nov-07 11:38
sitebuilderLuc Pattyn28-Nov-07 11:38 
GeneralRe: Identifier Expected Pin
murtle328-Nov-07 12:00
murtle328-Nov-07 12:00 
GeneralRe: Identifier Expected Pin
Luc Pattyn28-Nov-07 12:21
sitebuilderLuc Pattyn28-Nov-07 12:21 
AnswerRe: Identifier Expected Pin
Jared Bienz [MSFT]28-Nov-07 12:08
Jared Bienz [MSFT]28-Nov-07 12:08 
GeneralRe: Identifier Expected Pin
murtle328-Nov-07 12:16
murtle328-Nov-07 12:16 
GeneralRe: Identifier Expected Pin
Jared Bienz [MSFT]28-Nov-07 12:19
Jared Bienz [MSFT]28-Nov-07 12:19 
JokeRe: Identifier Expected Pin
Pete O'Hanlon28-Nov-07 22:16
mvePete O'Hanlon28-Nov-07 22:16 
GeneralRe: Identifier Expected Pin
Luc Pattyn28-Nov-07 22:40
sitebuilderLuc Pattyn28-Nov-07 22:40 
GeneralRe: Identifier Expected Pin
Pete O'Hanlon28-Nov-07 23:11
mvePete O'Hanlon28-Nov-07 23:11 
GeneralRe: Identifier Expected Pin
Anthony Mushrow28-Nov-07 12:19
professionalAnthony Mushrow28-Nov-07 12:19 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.