开发者

Silverlight cross thread ui update issue

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));
        }
    }
}

I'm trying to create a file upload that is capable of large and multiple file uploads. I'm doing this using a web service. I have made a two way binding in my UI and now I want to upload the files. I am doing this using this code

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.U开发者_如何学JAVAploadService.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 can't update the UI, or cross thread operation. I tried to use a Dispatcher, but I wasn't successful because I didn't know where to insert it.


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜