开发者

Problem with loading data asynchronously

开发者 https://www.devze.com 2023-01-14 19:58 出处:网络
Our application downloads a large amount of data on the startup. It needs most of the data for all operations. Here is a problem: some operations can be performed only after data is loaded. As it load

Our application downloads a large amount of data on the startup. It needs most of the data for all operations. Here is a problem: some operations can be performed only after data is loaded. As it loads asynchronously, most of user actions throw an exception.

I tried AutoResetEvent.WaitOne(), but it doesn't work correctly.

What can be 开发者_如何学编程done to avoid it? Thanks in advance.


This isn't a Silverlight problem, it's a universal producer/consumer problem.

Depending upon the exact environment andparticulars of your application, there are different approaches to it. If the data production is something that will happen VERY quickly and you only need to consume 1 unit of the specific data type, then in your consumer, you can poll a flag that you change once the data is produced. Or, you can provide the producer with a function/method to call once the data is populated. In this manner, you would push the data to the module that only displays what is available.

More complicated examples will involve queueing and signaling, but this should get you started.


Expose your data via a service, and also expose an indicator of whether the data is loading, loaded, couldn't be loaded etc. Your UI can then use this indicator to determine the availability of user interface elements (usually via commands).

You certainly should not be blocking your UI until the data loads - that defeats the whole purpose of you being forced to do it asynchronously in the first place. Instead, your UI should react according to the availability of the data as and when it loads.

0

精彩评论

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