What debugging tool (if any) allows me to identify the thread that holds the lock in the file?

I am debugging a test that periodically calls IOException, noting that the file cannot be deleted because it is being used by another process. I suspect that this process is indeed my test harness, and that some other thread in the process did not delete its file resources when I expected it to be.

Is there a tool I can use to determine which thread contains a blocking lock? If I can determine the flow, I can check its call stack and at least try to determine why the resource has not yet been deleted. The SOS debugging tool looks promising, but I don’t see any function that would remove enough guesswork from my research.

One thought is to identify a native OS thread identifier, which can then be mapped to a managed thread identifier through SOS. How can I accomplish the first?

+3
source share
3 answers

Process Explorer SysInternals. http://technet.microsoft.com/en-us/sysinternals/bb896653 . , .


Edit:

, , . , ProcessExplorer . !


2:

, agent-j:

try/catch, IOException, , , :

catch(IOException)
{
    LogMessage( string.Format(
        "Managed Thread Id: {0}",
        System.Threading.Thread.CurrentThread.ManagedThreadId) );

    LogMessage( string.Format(
        "Stack Trace: {0}",
        new System.Diagnostics.StackTrace(true).ToString()) );
}

3

, , , . :

catch(IOException)
{
  foreach (var thread in System.Diagnostics.Process.GetCurrentProcess().Threads)
  {
    LogMessage(string.Format(
      "Managed Thread Id: {0}",
      thread.ManagedThreadId));

    LogMessage(string.Format(
      "Stack Trace: {0}",
      new System.Diagnostics.StackTrace(thread, true).ToString()));

  }
}
+5

try{delete();}catch(IOException) catch. ?

+2

( , ). . t . . , . .

     Stream stream = null;
     Thread t = new Thread(() => stream = File.OpenWrite (@"c:\temp\junk111.txt"));
     t.Start();
     Thread.Sleep(1000);
     Console.WriteLine(t.ThreadState);
     stream.WriteByte(89);
     stream.Close();
     File.OpenWrite (@"c:\temp\junk222.txt");

stopped, , , , .

FxCop

C:\Program Files (x86)\Microsoft Visual Studio 10.0\Team Tools\Static Analysis Tools\FxCop>FxCopCmd.exe /file:c:\code\jeremy.sellars\test\Junk\bin\Debug\Junk.exe /console
Microsoft (R) FxCop Command-Line Tool, Version 10.0 (10.0.30319.1) X86
Copyright (C) Microsoft Corporation, All Rights Reserved.

...
[Location not stored in Pdb] : warning  : CA2210 : Microsoft.Design : Sign 'Junk.exe' with a strong name key.
C:\code\jeremy.sellars\TEST\Junk\Program.cs(50,1) : warning  : CA2000 : Microsoft.Reliability : In method 'Program.Main()', call System.IDisposable.Dispose on object 'File.OpenWrite("c:\\temp\\junk2.txt")' before all references to it are out of scope.
Done:00:00:06.1251568

C:\Program Files (x86)\Microsoft Visual Studio 10.0\Team Tools\Static Analysis Tools\FxCop>
0

All Articles