简体   繁体   English

为多个事件显示单个加载屏幕

[英]Showing a single loadingscreen for multiple events

My app downloads from several URLs at the same time. 我的应用程序同时从多个URL下载。 Non of them finishes at the same time, but i only want one loading screen letting the user see that something is going on :-) 它们不同时完成,但我只想要一个加载屏幕让用户看到正在发生的事情:-)

I've tried a "ticket" system, where i have a List<>. 我试过一个“票证”系统,我有一个List <>。 Each event that triggers a download add a ticket to the list. 触发下载的每个事件都会向列表添加票证。 When they are done they remove one. 完成后,他们删除一个。 I've then have a BackgroundWorker running every 200ms that checks this list. 然后我有一个每200ms运行一次的BackgroundWorker来检查这个列表。 If the list count is 0 and the loadingscreen (UserControl) exists (busyscreen.GetType().Name comparison to every object in this.LayoutRoot.Children) it removes the child. 如果列表计数为0且loadingscreen(UserControl)存在(busyscreen.GetType()。名称与this.LayoutRoot.Children中的每个对象进行比较),则删除子项。 If the list isnt 0 and there is no child name busyscreen it gets added. 如果列表不为0并且没有子名称busyscreen则会添加。

What's wrong with this logic? 这个逻辑出了什么问题? I cant seem to get it to work, is there a better way to do this kind of work? 我似乎无法让它工作,有没有更好的方法来做这种工作?

Ideally you want to use blocking and something like WaitHandle- you can make your code block until signalled to proceed 理想情况下,您希望使用阻塞和WaitHandle之类的东西 - 您可以使代码阻塞,直到发出信号为止

http://msdn.microsoft.com/en-us/library/system.threading.waithandle.aspx http://msdn.microsoft.com/en-us/library/system.threading.waithandle.aspx

Basically, you create an array of ManualResetEvent objects - these objects will signal to the main thread when the work is done 基本上,您创建一个ManualResetEvent对象数组 - 这些对象将在完成工作时向主线程发出信号

Then you call 然后你打电话

WaitHandle.WaitAll(arrayOfManualResetEvents) 

which will block until the reset events are signalled 这将阻止,直到重置事件发出信号

ManualResetEvent.Set()

So in pseudocode: 所以在伪代码中:

> Create each URL loader
> Create a ManualResetEvent for each loader
> Start each loader loading
> Call WaitHandle.WaitAll(arrayOfManualResets) which will block on your main thread
> In each URL LoadComplete handler call the associated ManualResetEvent.Set()
> The main thread will continue when all are signalled

Edit: As has been pointed out - WaitHandle.WaitAll doesn't work on Windows Phone. 编辑:正如已经指出的 - WaitHandle.WaitAll在Windows Phone上不起作用。 There's an alternative to it here: 这里有一个替代方案:

Alternatives for WaitHandle.WaitAll on Windows Phone? Windows Phone上WaitHandle.WaitAll的替代方案?

Which seems like it would do the job. 这似乎可以完成这项工作。 Consider using it because I always find that code that ideally should block, when used with timers can work but is more fiddly and prone to bugs 考虑使用它,因为我总是发现理想情况下应该阻止的代码,当与计时器一起使用时可以工作但是更加繁琐且易于出错

You can create Singleton object that will contain LoadCount property and loadCount field (of course do not forget about INotifyProeprtyChanged ) which you can use further as you need. 您可以创建包含LoadCount属性和loadCount字段的Singleton对象(当然不要忘记INotifyProeprtyChanged ),您可以根据需要进一步使用它。 When you start downloading from new URL do something like Interlocked.Increment(ref loadCount); 当您从新URL开始下载时,请执行Interlocked.Increment(ref loadCount); and on callback - Interlocked.Decrement(ref loadCount); 并在回调上 - Interlocked.Decrement(ref loadCount); .
Depending on LoadCount you can show "loading screen". 根据LoadCount您可以显示“加载屏幕”。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM