c# - How to properly wait for an unawaitable operation to complete -
In my app, I created a class, SerializableBitmapImage, which displays a default image while another image is downloaded from the Internet It also allows to download the downloaded image as a byte array so that the image does not need to be downloaded at every app launch. I currently have a serializable bitmap image in a square "card" that is saved right after downloading the card data:
var resp = appData.API.GetCards (); If (resp! = Null) {if (AppData.CardsData == empty) AppData.CardsData = new card data (); AppData.CardsData.Update (resp); AppData.SaveCards (); FillCards (); }
However, occasional images applet in the serializablebitmap image. Downloading of Savcards () timings does not end, so images can not be saved. Therefore, I am looking for a way to save images until I get the download. Right now, I am downloading images to the creator of SerialWebbitmap image like:
image = new bitmap image (default Yuri); // Set the bitmap to default while downloading the image img = new bitmap image {CreateOptions = BitmapCreateOptions.None}; Img.ImageOpened + = (Sender, E) = & gt; // Download Successful, Set Image Download Image (Image = Sender as Bitmap Image; NotifyPropertyChanged ("Image");}; Img.UriSource = downloadUri; // Download Image
I have created a task in the serializablebitmap image to wait for the image to be completed, but I'm not sure that it works in a healthy way.
Download Public ASINCC Work Waiting Format () (While (image == zero) image. Uraa ESROS.Orgig string == "Assets / ManPage / DefaultCard.") Waiting for the job. Check again when Daley (100); // completes 100ms}
Then, appetite In the SaveCard (), I wait to download all the images before saving the card:
Tasks [] functions = New work [CardData.Count]; For (IntI = 0; I & lt; CardData.Count; i ++) Work [i] = CardData [i] .CardImage.WaitForImageDownload (); Waiting for the job. (Work);
Is this the right way to wait for an operation that is not waiting to complete? Or, is there any way to force the picture to download immediately? thanks for your help! Elliott
You are close, of course, every time you stay busy - the waiting alarm should stop:
while (image == zero) image. Urosoro.original string == "Assets / ManPage / DefaultCard." ) Waiting for the job. Daley (100); // Check again when complete at 100ms
This is not correct, this will work but you are spinning the CPU unnecessarily and you present a delay which the user considers It may be better that the situation you are waiting for is really true. Inform the image to you:
Download the public async work waiting format image () {var tcs = new TaskCompletionSource & lt; Bool & gt; (); Image.ImageOpened + = (o, e) =>; {Tcs.SetResult (true); }; Image.ImageFailed + = (O, E) =>; {Tcs.SetResult (wrong); }; // After the event the source is set // or, check whether the image is already full and only the // is attached to it if it is image. Setsource (TODO); Wait for TCS. Task; }
(Better method is waiting in the subsidiary method.)
Your use of Task. When all
is a good thing is.
Or, is there any way to force the image to download immediately?
Answering it literally: Not at all, it will violate physics (I do not know what you mean by that question ...)
Comments
Post a Comment