是否可以替代Application.DoEvents() [英] Is there any alternative to Application.DoEvents()

查看:457
本文介绍了是否可以替代Application.DoEvents()的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个Web应用程序,试图使用

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.

推荐答案

这段代码是很不好的事情.通常,可以使用此方法的情况非常罕见,并且可靠性也很受关注.在示例中,您展示了它试图用来模仿某种并行性",但是这种方法永远无法奏效.

您需要在单独的线程中执行整个操作.由于需要在线程进行过程中更新UI,因此可以使用UI线程调用.

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

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



另请参阅我对线程包装的两个回答:
如何将ref参数传递给线程 [ ^ ],
启动后更改线程(生产者)的参数 [ ^ ].



还有这篇文章:
用于线程通信和线程间调用的简单阻塞队列 [ ^ ].

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



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



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

—SA


我怀疑DoEvents()正在制造问题.能否请您检查是否在指定用于写入图像文件的路径上在服务器上具有写访问权限?
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?


这篇关于是否可以替代Application.DoEvents()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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