File being used by another process.

Please support our C# advertiser: Intel Parallel Studio Home
Reply   View First Unread View First Unread

Join Date: Aug 2009
Posts: 5
Reputation: hdjim is an unknown quantity at this point 
Solved Threads: 0
hdjim hdjim is offline Offline
Newbie Poster

Re: File being used by another process.

 
0
  #11
Aug 20th, 2009
I have the same problem with a simple text file.

I solved the problem by:

while (!File.Exists(fileName))
Thread.Sleep(100);

I tested this by changing the sleep time to 50 msec which still worked. Anything under that delay caused the exception.
Reply With Quote Quick reply to this message  
Join Date: Dec 2009
Posts: 1
Reputation: svetikshark is an unknown quantity at this point 
Solved Threads: 0
svetikshark svetikshark is offline Offline
Newbie Poster
 
-1
  #12
1 Day Ago
Originally Posted by Denis St Flour View Post
I just had the same problem and

I added
GC.Collect();
GC.WaitForPendingFinalizers();

after the doc.Close(true) and that fixed the problem.

I only found that by accident. I was in debug mode with a break point after doc.Close. I noticed if I waited for a while and resumed debugging the next iteration would work sometimes. So I decided to form GC to release whatever resources that was holding the lock and it worked.
Thank you man!It helped me !!!!
Reply With Quote Quick reply to this message  
Join Date: Aug 2009
Posts: 425
Reputation: Ryshad has a spectacular aura about Ryshad has a spectacular aura about Ryshad has a spectacular aura about 
Solved Threads: 86
Ryshad's Avatar
Ryshad Ryshad is offline Offline
Posting Pro in Training
 
-1
  #13
1 Day Ago
another alternative is to read/write files through filestreams.
Filestreams dont create a file lock so they wont tie up your files, saving you the hastle of manually calling the grabage collector
Last edited by Ryshad; 1 Day Ago at 8:27 am.
Please don't take for granted the work that solvers do for you. Take the time to fully understand the code they give you so that you might adapt it to future problems.

"Learning is more than absorbing facts, it is acquiring understanding.” - William Arthur Ward
Reply With Quote Quick reply to this message  
Join Date: Aug 2009
Posts: 5
Reputation: hdjim is an unknown quantity at this point 
Solved Threads: 0
hdjim hdjim is offline Offline
Newbie Poster
 
0
  #14
1 Day Ago
Here is my final solution which seems to work quite well:

  1. using System;
  2. using System.Collections.Generic;
  3. using System.Text;
  4. using System.IO;
  5.  
  6. namespace Toolbox
  7. {
  8. public class FileCheck
  9. {
  10. public bool Exists(string fileName)
  11. {
  12. if (File.Exists(fileName))
  13. {
  14. Console.WriteLine("File Exists: " + fileName);
  15. return true;
  16.  
  17. }
  18. return false;
  19. }
  20.  
  21. public bool InUse(string fileName)
  22. {
  23. try
  24. {
  25. using (File.OpenRead(fileName)) // the using statement performs the close automatically!
  26. {
  27. return false;
  28. }
  29.  
  30. }
  31. catch (IOException)
  32. {
  33. Console.WriteLine("File is in use...");
  34. }
  35. return true;
  36. }
  37. }
  38. }
  39.  
  40. There are 2 functions: FileCheck.Exists and FileCheck.InUse
  41.  
  42. .
  43. .
  44. .
  45. string filePath = "FileName and Path";
  46. int timeInSeconds = 10; // depends on how long it takes
  47.  
  48. Toolbox.FileCheck fCheck = new Toolbox.FileCheck();
  49. Toolbox.Timer timer = new Toolbox.Timer();
  50.  
  51. bool fileInUse = true;
  52. bool elapsedTimer = false;
  53.  
  54. // set the timer timeout value
  55. int timeOut = timeInSeconds; // timeout in seconds
  56. timer.Start(timeOut);
  57.  
  58. // check if file is in use
  59. while ((fileInUse = Check.InUse(filePath)))
  60. {
  61. if (timer.Elapsed())
  62. {
  63. elapsedTimer = true;
  64. Console.WriteLine("Timeout occurred waiting for file: <" + filePath + "> to be release!");
  65. break;
  66. }
  67. else
  68. {
  69. Console.WriteLine("File: <" + filePath + "> in Use...");
  70. }
  71. System.Threading.Thread.Sleep(100);
  72. }
  73.  
  74. if (elapsedTimer)
  75. {
  76. Console.WriteLine("FileInUse: Timeout, File: <" + filePath + "> is still in use!");
  77. // here you might return file in use flag:fileInUse true
  78. }
  79. else
  80. {
  81. Console.WriteLine("File: <" + filePath + "> has been released by the Operating System.");
  82. // here you might return file in use flag: fileInUse false
  83. }
  84.  
  85. .
  86. .
  87. .
  88.  
  89. Here is the TimerRoutine:
  90.  
  91. using System;
  92. using System.Collections.Generic;
  93. using System.Text;
  94.  
  95. namespace Toolbox
  96. {
  97. public class Timer
  98. {
  99. private long _endTime; // getter/setter is called "timeOut"
  100.  
  101. private long currentTime
  102. {
  103. get { return DateTime.Now.Ticks; }
  104. }
  105. private long timeOut
  106. {
  107. get { return _endTime; }
  108. set { _endTime = DateTime.Now.Ticks + new TimeSpan(0, 0, 0, (int)value).Ticks; }
  109. }
  110. public bool Start(int timeout)
  111. {
  112. timeOut = timeout;
  113. return true;
  114. }
  115. public bool Elapsed()
  116. {
  117. if (currentTime > timeOut)
  118. {
  119. return true;
  120. }
  121. else
  122. {
  123. return false;
  124. }
  125. }
  126. }
  127. }
Last edited by niek_e; 1 Day Ago at 12:04 pm. Reason: Add code-tags
Reply With Quote Quick reply to this message  
Join Date: Mar 2009
Posts: 1
Reputation: eskiya1996 is an unknown quantity at this point 
Solved Threads: 0
eskiya1996's Avatar
eskiya1996 eskiya1996 is offline Offline
Newbie Poster

Thanks

 
0
  #15
1 Day Ago
Thanks for help it really works and gain time to me. Thanks again.
Reply With Quote Quick reply to this message  
Join Date: Aug 2009
Posts: 425
Reputation: Ryshad has a spectacular aura about Ryshad has a spectacular aura about Ryshad has a spectacular aura about 
Solved Threads: 86
Ryshad's Avatar
Ryshad Ryshad is offline Offline
Posting Pro in Training
 
0
  #16
1 Day Ago
There is a small risk involved with that method; you have created a race condition. It is possible that the file is not in use when the check is made so it returns false, but the file is subsequently locked by another process.

What i've garnered from forum responses to questions like this is that the test isn't worth doing. To handle file locks you can
a) Test if file is locked before you use it
b) Try using it and handle the exception if its locked

The file system is volatile and can change between you doing the test and trying to access the record. Because the test could return a false positive you will still need to wrap your file access in try...catch blocks. If you are handling the exception anyway, then why spend time coding to perform the test?
Please don't take for granted the work that solvers do for you. Take the time to fully understand the code they give you so that you might adapt it to future problems.

"Learning is more than absorbing facts, it is acquiring understanding.” - William Arthur Ward
Reply With Quote Quick reply to this message  
Join Date: Aug 2009
Posts: 5
Reputation: hdjim is an unknown quantity at this point 
Solved Threads: 0
hdjim hdjim is offline Offline
Newbie Poster
 
0
  #17
1 Day Ago
You are welcome! This took me a little time to figure out!
Cheers,
Jim
Reply With Quote Quick reply to this message  
Join Date: Aug 2009
Posts: 5
Reputation: hdjim is an unknown quantity at this point 
Solved Threads: 0
hdjim hdjim is offline Offline
Newbie Poster
 
0
  #18
1 Day Ago
not true
Reply With Quote Quick reply to this message  
Join Date: Aug 2009
Posts: 425
Reputation: Ryshad has a spectacular aura about Ryshad has a spectacular aura about Ryshad has a spectacular aura about 
Solved Threads: 86
Ryshad's Avatar
Ryshad Ryshad is offline Offline
Posting Pro in Training
 
0
  #19
1 Day Ago
Originally Posted by hdjim View Post
not true
Which part isnt true? The race condition? Or that the test isnt worth doing?
Please don't take for granted the work that solvers do for you. Take the time to fully understand the code they give you so that you might adapt it to future problems.

"Learning is more than absorbing facts, it is acquiring understanding.” - William Arthur Ward
Reply With Quote Quick reply to this message  
Join Date: Aug 2009
Posts: 5
Reputation: hdjim is an unknown quantity at this point 
Solved Threads: 0
hdjim hdjim is offline Offline
Newbie Poster
 
-1
  #20
1 Day Ago
Judy try it and quite bothering me dude.
Reply With Quote Quick reply to this message  
Reply

Message:


Thread Tools Search this Thread



Tag cloud for C#
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC