Click here to Skip to main content
15,867,750 members
Please Sign up or sign in to vote.
3.00/5 (2 votes)
See more:
Hi iam working on upload a docx resume file and show that content of that docx resume in the textbox.
the first part is ok but when i try to show the content of docx in textbox i am getting error."the file is being used by another process".used dispose(),not working still getting the same error.

so can i use
C#
GC.Collect();                    GC.WaitForPendingFinalizers();
methods for showing the docx text in the textbox, because i am getting the error "file is being used by another process",so when i used these above methods that error is gone .so my question is can i use this methods and if i use then the performance be effected in production server.
Thanks
Srinivas
Posted
Updated 13-Jul-12 13:17pm
v2

The problem "file is being used by another process" is not related to garbage collection.

As the similar question was asked here many times, from this experience I know: in most cases the blocking process is your own process. You could have forgotten to dispose/close something in the same application. So, first of all, check it up. To explore this possibility, please see my past answer:
Clearing a Handle in C#[^].

In this answer, pay attention for the using of the using statement which helps you to guarantee that appropriate file system object is properly disposed after use, not keeping the file locked.

In same cases, you really need to investigate which process holds which file. For this, I recommend using one utility from the Sysinternals Suite. This set of utilities (formerly from Winternals company, presently at Microsoft) is a must-have for any developer, please see:
http://technet.microsoft.com/en-us/sysinternals/bb842062[^],
http://technet.microsoft.com/en-us/sysinternals/bb545027[^].

The utility you need is "handle.exe", please see:
http://technet.microsoft.com/en-us/sysinternals/bb896655[^].

In your case, you use it with file name parameter:
handle.exe <file_name>


This utility will scan all kinds of handles, not just file handles. For file, it will scan all file handles matching the file name (so it does not have to be a full path name) and return information sufficient to identify each process, including its pid. So, if you need more information on a process in question, you can also use other Sysinternals utilities, in particular, its Process Explorer:
http://technet.microsoft.com/en-us/sysinternals/bb896653[^].

[EDIT]

I mentioned pid above, because you can use it to terminate offending process using System.Diagnostics.Process.Kill, but I would not advice you to do so — this is quite unsafe.

Good luck,
—SA
 
Share this answer
 
v4
Comments
RaisKazi 11-Sep-12 16:03pm    
My 5.
Sergey Alexandrovich Kryukov 11-Sep-12 16:04pm    
Thank you, Rais.
--SA
BillW33 11-Sep-12 17:06pm    
Very fine answer, +5
Sergey Alexandrovich Kryukov 11-Sep-12 17:39pm    
Thank you very much.
--SA
If you need to access the file with read/write stream and also read-only stream you should look at System.IO.FileAcess and System.IO.FileShare for the file stream. If I remember correctly the System.IO.FileShare for both instances of the file stream must match (might be the other way around though)

GC.Collect() and GC.WaitForPendingFinalizers will do nothing for a file that currently has a process locking it.

If you are done accessing the file when this occurs, then you should re-evaluate your code and verify that all of your streams to the file are properly disposed.

http://msdn.microsoft.com/en-us/library/system.io.fileshare.aspx[^]
 
Share this answer
 
v3
Comments
Sergey Alexandrovich Kryukov 13-Jul-12 20:12pm    
That is correct (I voted 4), but what to do? I explained that to OP, please see my answer.
--SA
Trak4Net 13-Jul-12 20:31pm    
Thank you, I see your answer now. I appreciate positive feedback to my answers.
RaisKazi 11-Sep-12 16:03pm    
My 5.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900