Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Path.GetTempFileName in MultiProcessing

we run several instances of our program (c#) on a single computer. In each instance our code tries to create "many" temporary files with help of method Path.GetTempFile(). And sometimes, our program fails with exception:

Exception: Access to the path is denied.
StackTrace:    at System.IO.__Error.WinIOError(Int32 errorCode, String maybeFullPath)
   at System.IO.Path.GetTempFileName()

I checked temporary folder and didn't find something strange: free disk is enough, number of temporary files is not very big, etc.

I have only one explanation: one instance gets temporary file and opens it, but in the same time, another instance also gets name of the temporary file and tries to open it. If it is correct? If yes, how to solve the issue, if not how to understand what a problem?

UPD: failed on computer with Windows Server 2008 HPC

Thank you, Igor.

like image 661
constructor Avatar asked Jul 15 '10 12:07

constructor


People also ask

What is path GetTempPath ()?

Path GetTempPath Returns the path of the current user's temporary folder.

How do I find the temp folder?

The Temporary files created by the Windows operating system are usually stored in the %system%\Windows\Temp folder, whereas the ones created by the User when running any software is stored in his user profile at %userprofiles%\AppData\Local\.

Can not create temp file?

This error has the following cause and solution: The drive that contains the directory specified by the TEMP environment variable is full. Delete files from the full drive or specify a different drive in the TEMP environment variable. The TEMP environment variable specifies an invalid or read-only drive or directory.


1 Answers

msdn states for the Path class:

Any public static (Shared in Visual Basic) members of this type are thread safe.

Furthermore there are two reasons given for IO exceptions:

  1. The GetTempFileName method will raise an IOException if it is used to create more than 65535 files without deleting previous temporary files.

  2. The GetTempFileName method will raise an IOException if no unique temporary file name is available. To resolve this error, delete all unneeded temporary files.

I'd recommend to check for this conditions (since you explicitly state that you create many temp files).

like image 143
tanascius Avatar answered Nov 15 '22 15:11

tanascius