views:

304

answers:

1

Hi, I have this class:

public class UploadFile : INotifyPropertyChanged
{
    private string name;
    public string Name 
    {
        get
        {
            return name;
        }
        set
        {
            name = value;
            OnPropertyChanged("Name");
        } 
    }

    private FileInfo fileInfo;
    public FileInfo FileInfo { get; set; }

    private string length;
    public string Length 
    {
        get
        {
            return length;
        }
        set
        {
            length = value;
            OnPropertyChanged("Length");
        }  
    }

    private int percentage;
    public int Percentage 
    {
        get
        {
            return percentage;
        }
        set
        {
            percentage = value;
            OnPropertyChanged("Percentage");
        } 
    }

    public string ProgressValue
    {
        get { return string.Format("{0}%", Percentage); }
    }

    private string imageSource;
    public string ImageSource 
    {
        get
        {
            return imageSource;
        }
        set
        {
            imageSource = value;
            OnPropertyChanged("ImageSource");
        }  
    }


    #region INotifyPropertyChanged Members

    public event PropertyChangedEventHandler PropertyChanged;

    #endregion

    private void OnPropertyChanged(string property)
    {
        if (PropertyChanged != null)
        {
            PropertyChanged(this, new PropertyChangedEventArgs(property));
        }
    }
}

What im trying to do is make some multi/large fileupload using a webservice. I have made two way binding in my ui and now i want to upload the files, this is done using this method:

private void DoUpload()
    {
        foreach (UploadFile file in fileInfos)
        {
            int BUFFERSIZE = 1024;
            int offset = 0; //get from webservice, when partial file
            FileStream s = file.FileInfo.OpenRead();
            byte[] buffer = null;
            long remainingBytes = s.Length - offset;
            while (remainingBytes > 0)
            {
                if (remainingBytes < BUFFERSIZE)
                {
                    buffer = new byte[remainingBytes];
                    BUFFERSIZE = (int)remainingBytes;
                }
                else if (remainingBytes > BUFFERSIZE)
                {
                    buffer = new byte[BUFFERSIZE];
                }

                s.Read(buffer, 0, BUFFERSIZE);
                //push to webservice
                Thread.Sleep(20);
                //UploadService.Service1SoapClient client = new MultiLargeFileUpload.UploadService.Service1SoapClient();
                //client.LargeUploadCompleted +=new EventHandler<AsyncCompletedEventArgs>(client_LargeUploadCompleted);
                //client.LargeUploadAsync(1, buffer, file.Name);
                offset += BUFFERSIZE;
                int newPercentage = offset / (int)file.FileInfo.Length * 100;
                file.Percentage = newPercentage;
                remainingBytes = s.Length - offset;                    
            }
            //file.Percentage = 100;
            //file.ImageSource = "accept.png";
        }
    }

The problem is that i cant update the ui, cross thread operation, and i tried to use a Dispatcher, but i dont know where to insert it...

+3  A: 

I guess you're trying to do the updating of your UI in

           file.Percentage = newPercentage;
           remainingBytes = s.Length - offset;

You'll have to wrap this in a delegate, and do a Dispatcher.BeginInvoke on this to update your UI from another thread:

Dispatcher.BeginInvoke(()=>{
    file.Percentage=newPercentage;
    remainingBytes = s.Length - offset;
});

This way you send a message to the messagequeue on the main thread, and the main thread can execute the delegate when he has time.

Jan Jongboom