Note that there are some explanatory texts on larger screens.

plurals
  1. POHow To: Best way to run a threaded loop with a UI update at the end of each?
    primarykey
    data
    text
    <p>an architectural question here.</p> <p>I have a List (selected via a listview) that needs to be sent to a webservice and created in the database. I want to be able to update one at a time then update a progress bar, listview and local List with the returned ID without locking up the uithread (although i'll have lock the listview/search controls etc they just need to be able to complete other task in different forms).</p> <p>At the minute i've considered the following (new to most)</p> <p>Background worker - the only problem here is that the progressChanged only returns an int so i'd find it difficult to return all the information I need.</p> <p>Tasks - i'm using these at the minute when all I need to return a single object and then process that information (like getting list from local database and then populating listview and that works well). I can't figure out how to spawn a thread that in turn does a loop and updates the UI Thread at the end of each pass.</p> <p>parallel.foreach - don't think this would be suitable with the web services element.</p> <p>All the stuff i've tried so far, either locks the UI or gets cross thread exceptions.</p> <p>I guess i'm looking for something that allows me to do this</p> <pre><code>createUsers(ref List&lt;users&gt; _users) { foreach(user _u in _users) { //call webservice and update - it returns an ID or null string newID = webCreate(_u); if (newID != null) { ListViewItem lvi = listview1.FindText(_u.ListID); lvi.SubItems[5].Text = newID; _u.newID = newID; } else { lvi.SubItems[5].Text = "Error"; } progressbar.value++ } } </code></pre> <p>There just seems to be so many options in .net 4.0 for threading I just can't think which one will best serve me. Any pointers would be great. </p> <p>Thanks, Pete.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload