1
public delegate void FileEventHandler(string file); public event FileEventHandler fileEvent; public void getAllFiles(string path) { foreach (string item in Directory.GetDirectories(path)) { try { getAllFiles(item); } catch (Exception) { } } foreach (string str in Directory.GetFiles(path, "*.pcap")) { // process my file and if this file format OK raised event to UI and add the file to my listbox FileChecker fileChecker = new FileChecker(); string result = fileChecker.checkFIle(str); if (result != null) fileEvent(result); } } private void btnAddDirInput_Click(object sender, EventArgs e) { ThreadStart ts = delegate { getAllFiles(pathToSearch); }; Thread thread = new Thread(ts); thread.IsBackground = true; thread.Start(); } 

I want to wait until thread has finish its job and then update my UI

15
  • 10
    Where's your thread? Commented Aug 29, 2013 at 14:07
  • 2
    stackoverflow.com/questions/1584062/… Check this out Commented Aug 29, 2013 at 14:10
  • I am calling getAllFiles() via another thread from the mail UI Commented Aug 29, 2013 at 14:13
  • It's hard to tell exactly what you need unless we see more information. You definitely need to lock something or add a mutex, we just can't tell what. Commented Aug 29, 2013 at 14:15
  • 1
    Can't you use a BackGroundWorker? They were made for this purpose. Commented Aug 29, 2013 at 14:16

2 Answers 2

5

You can use the Task Parallel Library, rather than explicit tasks, along with the asynchrony language features to do this very easily:

private async void btnAddDirInput_Click(object sender, EventArgs e) { await Task.Run(() => getAllFiles(pathToSearch)); lable1.Text = "all done!"; } 
Sign up to request clarification or add additional context in comments.

5 Comments

If this runs in a separate thread, the Text attribute assignment will throw a runtime exception.
@JonathanHenson Look at the method. This is the button click event handler. It runs in the UI thread.
Sorry didn't notice that. Await will not block the UI thread either?
@JonathanHenson No, it will not. It will schedule the remainder of the method as a continuation of the awaited task and then return control back to the caller.
+1 for realising the requirement is not necessarily to wait, but to simply update the UI once the task has completed, and providing a succint solution.
3

Why not use Tasks?

await Task.Run(() => getAllFiles(pathToSearch)); 

Your method will be run on a separate thread, freeing your main thread to keep the UI responsive. As soon as the task completes, the control will return to your UI thread.

Edit: Don't forget to mark your button_click method as async void

6 Comments

just curious, is it possible to update a progress bar this way?
@Koryu You would use the Progress class to update a progress bar.
Do you mean running a task that periodically updates a progress bar? You can, but I don't think tasks were designed for this purpose.
@dcastro The TPL does indeed have a mechanism for handling it. It is the Progress class, as I said before. That's the intended mechanism for solving that problem with this style of programming.
Thanks @Servy, I wasn't aware of the Progress class. If anyone else is interested in this, msdn.microsoft.com/en-us/library/hh193692.aspx and blogs.msdn.com/b/dotnet/archive/2012/06/06/… will help.
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.