简体   繁体   English

第3方dll中的死锁

[英]Deadlock in 3rd Party dll

I'm using (and referencing) two 3rd party dlls (a.dll, and b.dll) in two of my C# applications. 我在两个C#应用程序中使用(并引用)两个第三方dll(a.dll和b.dll)。 I'm getting a repeatable problem where both applications hang while making a call to a function in the 3rd party library. 我遇到一个可重复的问题,在调用第三方库中的函数时,两个应用程序都挂起。

I tried to make a copy of a.dll and b.dll (a2.dll, and b2.dll) and use that in the second application, but it turns out that a.dll references b.dll, anb b.dll references a.dll and this does not load properly. 我试图制作a.dll和b.dll(a2.dll和b2.dll)的副本,并在第二个应用程序中使用它,但事实证明a.dll引用了b.dll,anb b.dll引用了a.dll,并且无法正确加载。

I suspect that there is a deadlock but in the 3rd party library. 我怀疑在第3方库中存在僵局。 I can't use locking to prevent this. 我不能使用锁定来防止这种情况。 Each application enforces locking to make sure that that app only has one thread accessing the library at a time, but I can't lock across both programs. 每个应用程序都强制执行锁定,以确保该应用程序一次只能有一个线程访问该库,但是我无法在两个程序之间进行锁定。

So, my question is how can I solve this problem? 那么,我的问题是如何解决这个问题?

Can I tell the OS (Windows XP) that I don't want the dll to be shared? 我可以告诉OS(Windows XP)我不想共享dll吗?

Thanks, Joe 谢谢乔

You can restrict access so only a single program at a time by using a named mutex. 您可以使用命名的互斥锁来限制访问,因此一次只能访问一个程序。 Named mutexes are restrictable to an entire operating system, so can be used to prevent access from multiple processes. 命名的互斥锁只能在整个操作系统中使用,因此可以用来防止来自多个进程的访问。

See the Mutex class for details. 有关详细信息,请参见Mutex类

1) One way 1)一种方式

  • Create a server application, which exports/implements the 3rd-party API, and makes the API available to other/remote processes (eg by using dot net remoting, or a web service, to expose the API over the network). 创建一个服务器应用程序,该服务器应用程序导出/实现第三方API,并使该API可用于其他/远程进程(例如,通过使用点网远程处理或Web服务在网络上公开API)。
  • Within the server application, implement its API by delegating to the third party DLLs 在服务器应用程序中,通过委派给第三方DLL来实现其API
  • Start only one instance of the server application: there's therefore only one instance of the 3rd-party DLLs being used 仅启动服务器应用程序的一个实例:因此,仅使用了一个第三方DLL的实例
  • Have your existing applications use the API exported by your service, instead of using local instances of the 3rd-party DLLs 让您现有的应用程序使用您的服务导出的API,而不是使用第三方DLL的本地实例
  • Within your service, implement locks (which you can because it's now within a single process). 在您的服务中,实现锁(您可以执行锁定,因为它现在在单个进程中)。

2) Another way: use a kind of lock that's visible accross more than one process (eg the Mutex class). 2)另一种方法:使用一种在多个进程(例如Mutex类)中可见的Mutex

3) Ask the vendors of the third-party DLLs: are you supposed to be able to run only one instance of this DLL at a time? 3)询问第三方DLL的供应商:您一次只能运行此DLL的一个实例吗? If you are allowed to run more than one instance, what must you do to avoid deadlocks across processes? 如果允许您运行多个实例,则该怎么做才能避免跨进程死锁?

I once had a similar problem with a discontinued 3rd party product. 我曾经在停产的第三方产品上遇到过类似的问题。

There I used a disassembler and a hex editor with an integrated assembler to fix the underlying bug, but that was more or less luck because the cause was something trivial which could be derived by looking at the disassembly. 在那里,我使用了一个反汇编程序和一个带有集成汇编程序的十六进制编辑器来修复潜在的错误,但这多少有些幸运,因为原因很简单,可以通过查看反汇编来得出。

Depending on the actual cause that might be an option for you. 根据实际原因,可能会为您选择。

如何创建另一个线程-负责访问该dll ...这样整个应用程序不会冻结

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM