可以使用GC.Finalize方法吗 [英] can GC.Finalize method be used

查看:56
本文介绍了可以使用GC.Finalize方法吗的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好,iam正在上载docx简历文件,并在文本框中显示该docx简历的内容.
第一部分是可以的,但是当我尝试在文本框中显示docx的内容时,我遇到了错误.文件正在被另一个进程使用."使用dispose(),仍无法正常工作,仍然遇到相同的错误.

所以我可以使用

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

GC.Collect();                    GC.WaitForPendingFinalizers();

用于在文本框中显示docx文本的方法,因为我收到错误文件正在被另一个进程使用",所以当我使用上述这些方法时,错误消失了.所以我问题是我可以使用这种方法吗?如果使用,那么性能将在生产服务器中受到影响.
谢谢
Srinivas

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

推荐答案

问题文件正在被另一个进程使用"与垃圾回收无关.

正如在这里多次问过类似的问题一样,从这种经验中我知道:在大多数情况下,阻止过程是您自己的过程.您可能忘记了在同一应用程序中处理/关闭某些内容.因此,首先,进行检查.要探索这种可能性,请查看我过去的回答:
在C#中清除句柄 [ Winternals 公司,目前在Microsoft)是必须具备的对于任何开发人员,请参见:
http://technet.microsoft.com/en-us/sysinternals/bb842062 [ ^ ],
http://technet.microsoft.com/en-us/sysinternals/bb545027 [ ^ ].

您需要的实用程序是" handle.exe ",请参见:
http://technet.microsoft.com/en-us/sysinternals/bb896655 [ ^ ].

在您的情况下,可以将其与文件名参数一起使用:
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>



该实用程序将扫描所有类型的句柄,而不仅仅是文件句柄.对于文件,它将扫描与文件名匹配的所有文件句柄(因此它不必是完整的路径名),并返回足以标识每个进程的信息,包括其 pid .因此,如果您需要有关该进程的更多信息,则还可以使用其他Sysinternals实用程序,尤其是其 Process Explorer :
http://technet.microsoft.com/en-us/sysinternals/bb896653 [ ^ ].



我在上面提到了pid,因为您可以使用它使用System.Diagnostics.Process.Kill终止违规过程,但是我不建议您这样做-这是非常不安全的.

祝你好运,

—SA



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[^].



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


如果您需要使用读/写流和只读流访问文件,则应查看System.IO.FileAcessSystem.IO.FileShare文件流.如果我没记错的话,文件流的两个实例的System.IO.FileShare必须匹配(尽管可能相反)

GC.Collect()GC.WaitForPendingFinalizers对于当前具有锁定该文件的进程的文件不起作用.

如果在发生这种情况后访问完文件,则应重新评估代码并验证是否已正确处理文件的所有流.

http://msdn.microsoft.com/en-us/library/system.io. fileshare.aspx [^ ]
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[^]


这篇关于可以使用GC.Finalize方法吗的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆