PictureBox加载时间通知 [英] PictureBox load time notification

查看:71
本文介绍了PictureBox加载时间通知的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个按钮,可将URL中的图像加载到图片框.一些图像比其他图像需要更长的下载时间,因此我想向用户显示有关时间的通知.我的问题是如何将图像加载到图片框的时间超过5秒并且仍处于活动状态,以便如何将长时间事件通知用户?在Visual C#2010中的C#中使用.感谢您的帮助.

基本上,我需要显示已经在设计器中设置的初始图像,如果5秒钟后仍显示初始图像,则它应该显示一个MessageBox.我可以完成大部分代码,而这只是我在5秒钟后无法获得的部分.

I have a button that loads an image from a URL into a picturebox. Some of the images take longer to download than others so I want to display a notification to the user about time. My question is how would I get when the loading of an image into a picturebox has been active on for more than 5 seconds and is still active so I can notify the user of a long event? I am coding in C# in Visual C# 2010. Thanks for any help.

Basically I need to show the initial image which has already been set in the designer and after 5 seconds if the initial image is still showing it should show a MessageBox. I could do most of the code it''s just the if part to actually get when 5 seconds have passed that I can''t do.

推荐答案

没有这样的事情,你不需要那样的东西. UI操作为属性Image分配引用(包括此控件的无效)不会花费大量时间:
http://msdn.microsoft.com/en-us/library/system.windows.forms.picturebox.image.aspx [ ^ ].

需要时间来准备图像,这是位图本身(用作am图像的位图)的创建.您需要在单独的线程中处理所有非UI部分.您的操作将被阻止,因此您只需执行它即可.然后,您需要自己提供通知.但是如何在其他线程中通知UI?

您无法从非UI线程调用与UI相关的任何操作.相反,您需要使用System.Windows.Threading.DispatcherInvokeBeginInvoke方法(对于Forms或WPF)或System.Windows.Forms.Control(仅对于Forms).

在我过去的答案中,您将找到有关其工作原理的详细说明和代码示例:
Control.Invoke()与Control.BeginInvoke() [ ^ ],
Treeview Scanner和MD5的问题 [如何获取keydown事件在vb.net中的不同线程上操作 [启用禁用+多线程后控件事件不会触发 [ ^ ].

在您的情况下,被调用的委托应该在我刚开始提到的UI线程中简单地执行相同(快速)操作,并分配属性Image.



如果您通常不太熟悉线程,那么是时候开始了.如果不掌握本主题,您将无法开发出严肃的东西.如果您看到了我过去的一些答案,将为您提供一个很好的主意,以供您参考:
如何更改单线程功能/方法对于多线程 [ ^ ],
运行缓慢的过程,可能需要用户交互 [ ^ ].

—SA
There is no such thing, and you don''t need anything like that. The UI operation, which is assigning a reference to the property Image (which includes invalidation of this control) does not take any considerable time:
http://msdn.microsoft.com/en-us/library/system.windows.forms.picturebox.image.aspx[^].

What takes time is the preparation of the image, which is the creation of the bitmap itself, the one used as am image. You need to do all non-UI part in a separate thread. You operation will be blocking, so you just perform it. And then you need to provide a notification by yourself. But how to notify UI in some other thread?

You cannot call anything related to UI from non-UI thread. Instead, you need to use the method Invoke or BeginInvoke of System.Windows.Threading.Dispatcher (for both Forms or WPF) or System.Windows.Forms.Control (Forms only).

You will find detailed explanation of how it works and code samples in my past answers:
Control.Invoke() vs. Control.BeginInvoke()[^],
Problem with Treeview Scanner And MD5[^].

See also more references on threading:
How to get a keydown event to operate on a different thread in vb.net[^],
Control events not firing after enable disable + multithreading[^].

In your case, the invoked delegate should simply do the same (fast) operation in UI thread I mentioned in the very beginning, assigning of the property Image.



If you are not very experienced in threading in general, it''s time to start. You cannot develop anything serious without mastering this topic. If you see some of my past answer, is will give you a good idea what to loop for:
How to change a Single Thread Function/ Method For Multi Thread[^],
Slow running Process that might require user interaction[^].

—SA


您要做的并不困难! PictueBox.LoadAsync提供您所需的一切.

您可以在此处使用PictureBox.LoadAsynch(字符串url ):说明,警告和代码示例:[ ^ ].

在LoadProgressChanged事件中,如有必要,您可以调用PictureBox.CancelAsync()终止加载过程.

注意:我很长时间没有在PictureBox上使用LoadAsync进行过测试:也许从.NET 3.0开始:不能保证.
您还可以使用非常简单的BackGroundWorker [ ^ ]线程解决方案,或探索.NET FrameWork 4.5,Visual Studio 2012中异步/等待的新功能:请参见本页标题为使用图像"的链接:[
What you want to do is not that difficult ! PictueBox.LoadAsync provides everything you need.

You can use PictureBox.LoadAsynch(string url) : explanation, warnings, and code example, here: [^] to load a picture from a local file, or a URL.

PictureBox.LoadAsynch is supported in: .NET Framework 4.5, 4, 3.5, 3.0, 2.0; and, .NET Framework Client Profile 4, 3.5 SP1.

Handling progress in loading using LoadAsynch simply requires handling the PictureBox.LoadProgressChanged Event [^] supported in .NET FrameWork 2.0~4.0.

Within the LoadProgressChanged event you could call PictureBox.CancelAsync() to terminate the loading process, if necessary.

Note: I have not tested using LoadAsync on a PictureBox in a long time: perhaps since .NET 3.0: no guarantees.

You could also use a very simple BackGroundWorker [^] threading solution, or explore the new features async/await in .NET FrameWork 4.5., Visual Studio 2012: see the links in the section on this page titled "Working with images:" [^].

You may need to use a Timer to give some feedback to the user if the image is not loaded in a certain timespan: but, using a MessageBox is not a good choice because it''s modal: it will block the main UI thread, and require user action to dismiss. If it is necessary to allow the user to cancel the load operation while it is happening, perhaps consider making some button visible that''s hidden when not loading.

There are examples of loading a PictureBox and showing progress here on CodeProject: just search :)


这篇关于PictureBox加载时间通知的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆