S007
S007

Reputation: 65

WPF asynchronous databinding

I have a WPF MVVM prism application to upload files. I filled a datagrid with a list of files from a directory and got an upload button to upload the selected files from the datagrid.

For this I pass the grid selecteditems to the command parameter of the button. I successfully implemented this in the synchronous way and now want to extend this functionality in asynchronous way by using delegates.

This is my asynchronous function call:

asyncUpload.BeginInvoke(selectedFiles, out tt, new AsyncCallback(test), null);

Here the selected files are the selecteditems from the datagrid passed through the command parameter of the upload button. The problem is that while executing the first thread for uploading, I'm selecting another set of files from the datagrid to upload that will change the first thread's selected files.

How can I solve this?

Upvotes: 0

Views: 943

Answers (1)

ChrisNel52
ChrisNel52

Reputation: 15173

Don't pass the actual SelectedItems list into your BeginInvoke() method.

Instead, pass a copy of the SelectedItems list.

Array[] array = new Array[dataGrid.SelectedItems.Count];

dataGrid.SelectedItems.CopyTo(array, 0);

asyncUpload.BeginInvoke(array.ToList(), out tt, new AsyncCallback(test), null); 

Upvotes: 1

Related Questions