是否可以替代Application.DoEvents() [英] Is there any alternative to Application.DoEvents()
问题描述
我有一个Web应用程序,试图使用 ^ ].不知何故,我无法使其在服务器上正常运行,而无法在本地计算机上正常运行.不确定它,但我认为Application.DoEvents()行给我带来麻烦,甚至不确定它的作用,但我不知道如果将其删除,不会获得预期的结果.
有没有其他替代Application.DoEvents()或代码中的任何修改可以解决我的问题.
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.Dispatcher
的Invoke
或BeginInvoke
方法(对于Forms或WPF)或System.Windows.Forms.Control
(仅对于Forms).
在我过去的答案中,您将找到有关其工作原理的详细说明和代码示例:
Control.Invoke()与Control.BeginInvoke() [ ^ ],
Treeview扫描仪和MD5的问题 [如何获取keydown事件在vb.net中的不同线程上操作 [启用禁用+多线程后控件事件不会触发 [ ^ ].
另请参阅我对线程包装的两个回答:
如何将ref参数传递给线程 [ ^ ],
启动后更改线程(生产者)的参数 [ ^ ].
还有这篇文章:
用于线程通信和线程间调用的简单阻塞队列 [ ^ ].
—SA
Of courseDoEvents
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 methodInvoke
orBeginInvoke
ofSystem.Windows.Threading.Dispatcher
(for both Forms or WPF) orSystem.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屋!