an architectural question here.
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).
At the minute i’ve considered the following (new to most)
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.
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.
parallel.foreach – don’t think this would be suitable with the web services element.
All the stuff i’ve tried so far, either locks the UI or gets cross thread exceptions.
I guess i’m looking for something that allows me to do this
createUsers(ref List<users> _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++
}
}
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.
Thanks, Pete.
If your create users is run in a separate thread. Have your form, have an updatestatusbar
Now you can call updatestatusbar from anywhere, thread or otherwise and it sorts itself out.