开发者

synchronizing webClient download (silverlight)

开发者 https://www.devze.com 2023-01-17 23:46 出处:网络
so I have this function which gets called multiple times during my program. //global variable BitmapImage img;

so I have this function which gets called multiple times during my program.

    //global variable
    BitmapImage img;

    private void LoadImageFile(string ImageName)
    {
        WebClient ImageClient = new WebClient();
        ImageClient.DownloadStringCompleted += new DownloadStringCompletedEventHandler(ImageFileLo开发者_运维技巧aded);
        xmlClient.DownloadStringAsync(new Uri("/images/"+ImageName, UriKind.RelativeOrAbsolute));
    }

    void ImageFileLoaded(object sender, DownloadStringCompletedEventArgs e)
    {
        if (e.Error == null)
        {
            img.set = e.Result;



        }
    }

the following code uses the new value of "img" so I want it to start only after img has been assigned the new source but it seems that it runs before that happens


You want to use WebClient.OpenReadAsync() instead of WebClient.DownloadStringAsync() because you want to read a binary image, not a string.

Then when you get the stream, you call BitmapImage.SetSource() using that stream.


I would check out this blog by Jeremy Likness.

It uses corountines to help organise async requests. I have used this approach and have dealt with similar issues where I want actions to occur after several async tasks.

0

精彩评论

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

关注公众号