Click here to Skip to main content
15,881,709 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
I have a web application where I am trying to save webpage as image using the code provided at http://stackoverflow.com/questions/2715385/convert-webpage-to-image-from-asp-net[^]. Somehow I am not able to make it work on my server whereas its working perfectly on my local machine.Not sure about it but I think the line Application.DoEvents() is creating trouble for me , not even sure what it does but I don't get expected result if i remove it.
Is there any other alternative to Application.DoEvents() or any modification in the code which will solve my problem.
Posted

Of course DoEvents as it is used in this code is a very bad thing. Generally, the cases where this method can be used are very rare, and reliability is much concerned. In the sample you show it is attempted to use to imitate some kind of "parallelism", which never works well.

You need to do the whole operation in a separate thread. As you need to update UI while the thread is progressing, you can use UI thread invocation.

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[^].

[EDIT #1]

See also my two answers of thread wrappers:
How to pass ref parameter to the thread[^],
change paramters of thread (producer) after it started[^].

[EDIT #2]

And also this article:
Simple Blocking Queue for Thread Communication and Inter-thread Invocation[^].

—SA
 
Share this answer
 
v4
Comments
BillW33 26-Jul-12 17:17pm    
Very nice answer, +5 :)
Sergey Alexandrovich Kryukov 26-Jul-12 18:59pm    
Thank you.
--SA
Kenneth Haugland 26-Jul-12 18:13pm    
A very good answer 6!. I do however have a question related to this. If I have read you posts correctly you are essentially saing that all expencive loading should be done on a separate thread, but are there any good rule of thumb when I must do it? I mean that the final user could have problems regarding the UI thread that dont have while debugging.
Sergey Alexandrovich Kryukov 26-Jul-12 19:08pm    
Basically, you need to have a separate thread for anything which has blocking call or simply takes any time above few milliseconds for the complete processing of some atomic UI event. The ways of obtaining such thread (there are three: using its constructor (important to use a wrapper class to the thread by the reason you can find in my updated answer, after [EDIT]), thread pool and BackgroundWorker). As to the problem with UI thread, it depends how the notification is implemented. My answer above is devoted mostly to this aspect (Invoke, BeginInvoke and related issues.)

Please see my short article on thread synchronization and other posts I referenced:
http://www.codeproject.com/Tips/149540/Simple-Blocking-Queue-for-Thread-Communication-and.

(I'll add another update).

Good luck,
--SA
Dexter11 27-Jul-12 6:52am    
Thank you for your replies.
I know it would be like spoon feeding but could you help to rewrite same code with threading instead of Application.DoEvents() as i have to get it wrapped soon and i am not much aware of threading concepts.
Thanks in advance.
I doubt that DoEvents() is creating problem. Can you please check whether you have write access on the server at the path specified for writing the image file?
 
Share this answer
 
Comments
Dexter11 26-Jul-12 9:37am    
I am pretty sure that's not access problem.My page keeps on loading and finally gets timeout.
I guess the condition while (browser.ReadyState != WebBrowserReadyState.Complete) goes in indefinite loop.

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