互斥锁不适用于跨进程文件锁定 [英] Mutex not working for cross-process file locking

查看:177
本文介绍了互斥锁不适用于跨进程文件锁定的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

请参阅下面的已批准解决方案 - 因为正在从两个不同的用户空间访问该文件,因此需要将Mutex声明为Global。感谢Andreas的解决方案!



=========================

好​​的,我有一个可以由多个进程写入的日志文件。我正在尝试使用一个名为Mutex来保护它,但在尝试打开文件时我仍然偶尔会得到文件被另一个进程使用错误,我无法理解为什么。据我所知,我正确使用互斥锁。任何指针都将非常感激...



相关(简化)代码如下 - 这是在两个进程调用的程序集中,我已经验证了两个进程都认为他们使用相同的Mutex,只需要一个进程创建Mutex,另一个进程使用OpenExisting。



See approved solution below - because the file is being accessed from two different user spaces, the Mutex needs to be declared as Global. Thanks to Andreas for the solution!

=========================
OK, I have a log file that can be written to by multiple processes. I''m trying to protect it with a named Mutex, but I''m still occasionally getting "file is in use by another process" errors when trying to open the file and I can''t understand why. As far as I can tell, I''m using the Mutex correctly. Any pointers would be greatly appreciated...

Relevant (simplified) code is below - this is in an assembly that is called by both processes, and I have verified that both processes believe that they are using the same Mutex by having one process create the Mutex and the other use OpenExisting.

public class Log
{
  public static Mutex m_Mutex = new Mutex( false, "MyMutex" );

  public static void LogStuff( string stuff )
  {
    StreamWriter w = null;

    try
    {
      m_Mutex.WaitOne();

      w = File.AppendText( @"C:\temp.log" );
      w.WriteLine( stuff );
    }
    finally
    {
      if ( w != null )
      {
        w.Flush();
        w.Close();
        w = null;
      }

      m_Mutex.ReleaseMutex();
    }
  }
}





其他(可能)重要说明:

- 这是VS2008目前正在调试模式下进行的,我只调试主进程。



- 正好有2个进程可以访问文件,两者都在同一台机器上运行。需要2个进程与需要管理员权限的某些第三方驱动程序有关,而我们希望主应用程序以当前用户身份运行,即使他们没有管理员权限。



Other (possibly) important notes:
- This is currently happening in Debug mode from VS2008, where I am debugging only the main process.

- There are exactly 2 processes that can access the file, both of which are running on the same machine. The need for 2 processes has to do with some 3rd-party drivers that require Admin privileges, while we want the main application to run as the current user, even if they don''t have Admin rights.

推荐答案

更改您的代码如下:

1)给Mutex命名 @Global \ MyMutex

2)在(或代替)之后,在 w 上拨打 Dispose() 关闭()

为您的其他流程执行相同操作。





我会简化代码如下:

Change your code as follows:
1) Give the Mutex the name @"Global\MyMutex".
2) Call Dispose() on w after (or instead of) Close().
Do the same for your other process.


I would simplify the code as follows:
public class Log
{
  public static Mutex m_Mutex = new Mutex( false, @"Global\MyMutex" );
 
  public static void LogStuff( string stuff )
  {
    try
    {
      m_Mutex.WaitOne();
      using(var w = File.AppendText( @"C:\temp.log" ))
      {
        w.WriteLine( stuff );
      }
    }
    finally
    {
      m_Mutex.ReleaseMutex();
    }
  }
}



这个使用块调用Dispose at块的结束(因此,刷新和关闭流并释放文件系统上文件的代理对象)。

[/ EDIT]



干杯

Andi


This using block calls the Dispose at the end of the block (thus, flushing and closing the stream and releaseing the proxy object of the file on the file system).
[/EDIT]

Cheers
Andi


这篇关于互斥锁不适用于跨进程文件锁定的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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