File Access problem - 2

sj1187534

Well-known member
Joined
Jun 10, 2003
Messages
108
Location
Dallas, Houston, etc.etc.
I have been fighting with this part of the code for a long time trying to figure out what is wrong with it. It is saying that the file is being used by another process( on line 197 ). As you can see from the code, the method that is giving me an error is a synchronized method so that only one thread can enter it at any time. I am even closing the streamwriter when I am done writing. Another wierd thing is in some threads, I am getting an error saying that the file represented by permlogfilepath already exists even though I am performaing a check that if that file exists, process it differently!! Any suggestions?

I have also tried debugging the code but I was not successfull in finding what process is locking it. Any other way to find out?

Thanks
SJ
 

Attachments

Are you opening the same filename anywhere else in you code and failing to close it? A good tool for finding what files are open is FileMon from www.sysinternals.com.

Also could you avoid posting large screenshots unnecessarily as some people access this site from dialup connections and a 90K attachment is a little large when the text could have been cut and pasted for less than a couple of K.
 
I am pretty sure that I am not opening the file anywhere else. The only reason I was able to think of is the first thread that is opening the file may not be closing it. But I am programmatically closing it.

And about the screenshot, I usually paste the code but I wanted everyone to know the line number that I am talking about and be able to read the code clearly. Anyway, thanks for the suggestion.

SJ
 
Hmmm, been looking into this a bit more and all Ive discovered is Im not sure what the
Code:
	<MethodImpl(MethodImplOptions.Synchronized)>
is supposed to do :confused:
Although i did hit a few pages that seemed to suggest avoiding it and implementing the locking yourself.

If you look at the ildasm output for a method with that attribute it decoreates the function but doesnt provide any explicit locing code :confused: (again)

looking at the following class
Code:
Friend Class Class1

	Private objLock As Object = New Object

	<MethodImpl(MethodImplOptions.Synchronized)> _
   Protected Sub Test1(ByVal i As Integer)
		i = i + 3
	End Sub

	Protected Sub Test2(ByVal i As Integer)
		System.Threading.Monitor.Enter(objLock)
		i = i + 3
		System.Threading.Monitor.Exit(objLock)
	End Sub

End Class

Test1 produces the following MSIL
Code:
.method family instance void  Test1(int32 i) cil managed synchronized
{
  // Code size       6 (0x6)
  .maxstack  8
  IL_0000:  ldarg.1
  IL_0001:  ldc.i4.3
  IL_0002:  add.ovf
  IL_0003:  starg.s    i
  IL_0005:  ret
} // end of method Class1::Test1

and Test2 produces this
Code:
.method family instance void  Test2(int32 i) cil managed
{
  // Code size       38 (0x26)
  .maxstack  8
  IL_0000:  ldarg.0
  IL_0001:  ldfld      object VB_WinApp_2.Class1::objLock
  IL_0006:  call       object [mscorlib]System.Runtime.CompilerServices.RuntimeHelpers::GetObjectValue(object)
  IL_000b:  call       void [mscorlib]System.Threading.Monitor::Enter(object)
  IL_0010:  ldarg.1
  IL_0011:  ldc.i4.3
  IL_0012:  add.ovf
  IL_0013:  starg.s    i
  IL_0015:  ldarg.0
  IL_0016:  ldfld      object VB_WinApp_2.Class1::objLock
  IL_001b:  call       object [mscorlib]System.Runtime.CompilerServices.RuntimeHelpers::GetObjectValue(object)
  IL_0020:  call       void [mscorlib]System.Threading.Monitor::Exit(object)
  IL_0025:  ret
} // end of method Class1::Test2

although Test1 is much shorter the only thing it seems to do is decorate the function with the synchronized attribute.

It may be worth removing the <MethodImpl(..)> attribute and implementing your own locking code (similar to the stuff in Test2) if for no other reason than to see if that fixes the problem. I have a feeling this will bug me for a while so if it solves the problem could you let me know? :)
 
Back
Top