Click here to Skip to main content
15,914,390 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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.

[EDIT] 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.
Posted
Updated 18-Aug-13 13:13pm
v2

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.

[EDIT]

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
 
Share this answer
 
v2
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 :)
 
Share this answer
 
v3

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900