多个线程写入WebBrowser,陷入僵局 [英] multiple threads writing to WebBrowser, getting deadlocked

查看:102
本文介绍了多个线程写入WebBrowser,陷入僵局的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有多个线程写入WebBrowser(使用一个函数,

检查InvokedRequired,如果是这样,在WebBrowser上调用自己

线程)并且它们正在陷入僵局。


当我在调用

WebBrowser.Write时使用锁{}时它们只会死锁,以确保线程安全!


有没有人有过这样的经历?


Zytan

解决方案

< blockquote> Zytan< zy ********** @ gmail.comwrote:


我有多个线程写入WebBrowser(使用一个函数

检查InvokedRequired,如果是这样,在WebBrowser上调用它自己的b $ b $线程)它们就会陷入僵局。


它们只是死锁当我在调用

WebBrowser.Write时使用lock {}以确保线程安全!


有没有人有过这样的经历?



坚持 - 你的意思是你在调用Invoke时已经锁定了吗?


你能发帖吗?一个简短但完整的程序,演示了

问题?


http://www.pobox.com/~skeet/csharp/complete.html 了解详情

什么我是说那个。


-

Jon Skeet - < sk *** @ pobox.com>
http://www.pobox.com/~skeet 博客: http://www.msmvps.com/jon.skeet

如果回复小组,请不要给我发邮件


Zytan写道:


我有多个线程写入WebBrowser (使用

检查InvokedRequired的函数,如果是,则调用它WebBrowser上的精灵

线程)他们正陷入僵局。


当我使用锁定{}时,他们只会死锁。

WebBrowser.Write确保线程安全!



使用''lock''没有意义。


WebBrowser是一个WinForms控件,只应该可以从它的

UI线程访问。所以,如果你使用Control.InvokeRequired,并正确使用

Control.Invoke或Control.BeginInvoke将你的调用封送到

UI控件,你不会需要使用''lock'',因为WebBrowser控件上触及的所有代码都是在其UI线程上序列化的。


访问Control.InvokeRequired是不需要同步 - 实际上,如果它和它的同类确实需要

同步,它很容易死锁。类似地,Control.Invoke,Control.BeginInvoke等

不需要同步 - 检查Control.InvokeRequired的文档。


- Barry


-
http://barrkel.blogspot .com /


Zytan写道:


我有多个线程写入WebBrowser(使用一个函数,

检查InvokedRequired,如果是这样,在WebBrowser上调用它自己的b $ b $线程)它们就会陷入僵局。


当我在调用

WebBrowser.Write时使用lock {}以确保线程安全时,它们只会死锁!


是否有人有这样的经验事情?



好​​吧,如果你这样做:


class Foo

{

//设置一个名为WebBrowser的WebBrowser控件的东西


public void SomeMethod()

{

lock(WebBrowser )

{

if(InvokeRequired)

{

Invoke(new SomeDelegate(SomeMethod));

}否则{

//做点什么

}

}

}

}


当从

UI线程以外的线程调用时,这将导致死锁。相反,你应该这样做:


class Foo

{

//设置一个名为WebBrowser的WebBrowser控件的东西


public void SomeMethod()

{

if(InvokeRequired)

{

Invoke(new SomeDelegate(SomeMethod));

} else {

lock(WebBrowser)

{

//做点什么

}

}

}

}

}


如果你想要更多,发布一些代码:-)


Alun Harford


I have multiple threads writing to WebBrowser (using a function that
checks InvokedRequired, and if so, invokes itself on the WebBrowser
thread) and they are getting deadlocked.

They only deadlock when I use lock { } around the call to
WebBrowser.Write to ensure thread safety!

Does any one have experience with such a thing?

Zytan

解决方案

Zytan <zy**********@gmail.comwrote:

I have multiple threads writing to WebBrowser (using a function that
checks InvokedRequired, and if so, invokes itself on the WebBrowser
thread) and they are getting deadlocked.

They only deadlock when I use lock { } around the call to
WebBrowser.Write to ensure thread safety!

Does any one have experience with such a thing?

Hang on - do you mean you''ve already got a lock when you call Invoke?

Could you post a short but complete program which demonstrates the
problem?

See http://www.pobox.com/~skeet/csharp/complete.html for details of
what I mean by that.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too


Zytan wrote:

I have multiple threads writing to WebBrowser (using a function that
checks InvokedRequired, and if so, invokes itself on the WebBrowser
thread) and they are getting deadlocked.

They only deadlock when I use lock { } around the call to
WebBrowser.Write to ensure thread safety!

Using ''lock'' doesn''t make sense.

WebBrowser is a WinForms control, and should only be accessed from its
UI thread. So, if you use Control.InvokeRequired, and properly use
Control.Invoke or Control.BeginInvoke to marshal your call over to the
UI control, you don''t need to use ''lock'', because all code that touches
the WebBrowser control is serialized on its UI thread.

Access to Control.InvokeRequired is not required to be synchronized - in
fact, it would be easy to deadlock if it and its ilk did require
synchronization. Similarly, Control.Invoke, Control.BeginInvoke etc.
don''t require synchronization - check docs for Control.InvokeRequired.

-- Barry

--
http://barrkel.blogspot.com/


Zytan wrote:

I have multiple threads writing to WebBrowser (using a function that
checks InvokedRequired, and if so, invokes itself on the WebBrowser
thread) and they are getting deadlocked.

They only deadlock when I use lock { } around the call to
WebBrowser.Write to ensure thread safety!

Does any one have experience with such a thing?

Well if you do:

class Foo
{
//Some stuff to setup a WebBrowser control called WebBrowser

public void SomeMethod()
{
lock(WebBrowser)
{
if(InvokeRequired)
{
Invoke(new SomeDelegate(SomeMethod));
} else {
//Do something
}
}
}
}

This will result in a deadlock when called from a thread other than the
UI thread. Instead, you should do:

class Foo
{
//Some stuff to setup a WebBrowser control called WebBrowser

public void SomeMethod()
{
if(InvokeRequired)
{
Invoke(new SomeDelegate(SomeMethod));
} else {
lock(WebBrowser)
{
//Do something
}
}
}
}
}

If you want more than that, post some code :-)

Alun Harford


这篇关于多个线程写入WebBrowser,陷入僵局的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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