文件IO竞赛!! [英] File IO Race!!

查看:144
本文介绍了文件IO竞赛!!的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,这是一种情况,我正在尝试使用FileMonitor通过

Win Svc处理一些大型文件。在测试过程中,我尝试在一个文件夹中复制并粘贴

a文件,并收到错误消息Process无法访问文件

foo.txt因为它是被另一个进程使用。我相信复制

操作还没有完成,虽然我的WinSvc开始.....因为它有一个

FileChange通知......

如何避免竞争条件?


TIA

Hi All, Here''s a situation, I am trying to process some large Files via a
Win Svc using FileMonitor. During the test process I tried copy and pasting
a file in a folder and got an error saying "Process can''t access the file
foo.txt as it is being used by another process". I believe the Copy
operation hasn''t finished yet though my WinSvc kicked in.....since it got a
FileChange Notification...
How can I avoid the Race Condition?

TIA

推荐答案

Vai2000


请发布完整的代码片段来演示问题。否则,

你有机会获得通用答案。像我的一样:

- 可能是你在完成复制时没有关闭文件

- 可能是你尝试访问文件而尚未准备就绪


第一个是问题,第二个是你可以通过简单地等待文件来避免

变得可用


HTH

Alex


" Vai2000" <无**** @ microsoft.com>在消息中写道

新闻:%2 **************** @ TK2MSFTNGP12.phx.gbl ...
Hi, Vai2000

please post complete code snippet demonstrating the problem. Otherwise,
chances are you will get generic answers. Like mine:
- could be that you don''t close file when finish copy
- could be that you try to access file while it is not yet ready

First one is problem, second one you can avoid by simply waiting for file to
become available

HTH
Alex

"Vai2000" <no****@microsoft.com> wrote in message
news:%2****************@TK2MSFTNGP12.phx.gbl...
大家好,这是一种情况,我试图通过使用FileMonitor的Win Svc来处理一些大型文件。在测试过程中,我尝试复制并将
粘贴到文件夹中的文件,并收到错误消息,说进程无法访问文件
foo.txt,因为它正被另一个进程使用。我相信复制
操作还没有结束,虽然我的WinSvc开始.....因为它获得了
a FileChange通知......
我怎样才能避免竞争条件?

TIA
Hi All, Here''s a situation, I am trying to process some large Files via a
Win Svc using FileMonitor. During the test process I tried copy and pasting a file in a folder and got an error saying "Process can''t access the file
foo.txt as it is being used by another process". I believe the Copy
operation hasn''t finished yet though my WinSvc kicked in.....since it got a FileChange Notification...
How can I avoid the Race Condition?

TIA



不幸的是我的模块太大了,不能在这里张贴....但是这里'' s

勺子

我在FileCreate事件上调用一个代表。

FileMonitor.Created + = new

系统.IO.FileSystemEventHandler(this.FileMonitor_已更改);

现在,如果您有一个大文件,复制操作可能无法完成,但

代理会立即被调用为文件被创建!!!然后,Delegate中的

函数尝试访问该文件......并抛出

异常!!!

我怎么知道什么时候该文件已经完全创建,以便我的工人

课程可以访问它?


我试图做这样的事情

重试:

尝试

{

OperateOnFile(); // FileStram freader = new FileStream(this.fileName,

FileMode.Open,FileAccess.Read,FileShare.ReadWrite);

}

catch(例外e)

{

Thread.Sleep(15000);

goto retry;

}


但仍然没有运气!!!

TIA

" AlexS" < SA *********** @ SPAMsympaticoPLEASE.ca>在消息中写道

新闻:uJ ************** @ TK2MSFTNGP11.phx.gbl ...
Unfortunately my module is just too large to post out here.... but here''s
the scoop
I call a Delegate on FileCreate event.
FileMonitor.Created += new
System.IO.FileSystemEventHandler(this.FileMonitor_ Changed);
Now if you have a large file, the copy operation might not be completed but
the Delegates gets invoked immediately as a file gets created!!! Then the
function in the Delegate tries to access the file... and it throws
exception!!!
How do I know when the file has been completely created so that my worker
classes can access it ?

I was trying to do something like this

retry:
try
{
OperateOnFile(); // FileStram freader = new FileStream(this.fileName,
FileMode.Open, FileAccess.Read, FileShare.ReadWrite);
}
catch(Exception e)
{
Thread.Sleep(15000);
goto retry;
}

But still no Luck!!!
TIA
"AlexS" <sa***********@SPAMsympaticoPLEASE.ca> wrote in message
news:uJ**************@TK2MSFTNGP11.phx.gbl...
Vai2000

请发布完整的代码片段来演示问题。否则,
很可能会获得通用答案。像我一样:
- 可能是你在完成复制时没有关闭文件
- 可能是你试图访问文件还没有准备好

第一个是问题,第二个你可以通过简单地等待文件
变得可用来避免

HTH
Alex

" Vai2000" <无**** @ microsoft.com>在消息中写道
新闻:%2 **************** @ TK2MSFTNGP12.phx.gbl ...
Hi, Vai2000

please post complete code snippet demonstrating the problem. Otherwise,
chances are you will get generic answers. Like mine:
- could be that you don''t close file when finish copy
- could be that you try to access file while it is not yet ready

First one is problem, second one you can avoid by simply waiting for file to become available

HTH
Alex

"Vai2000" <no****@microsoft.com> wrote in message
news:%2****************@TK2MSFTNGP12.phx.gbl...
大家好,这里'' sa情况,我正在尝试使用FileMonitor通过
a Win Svc处理一些大型文件。在测试过程中,我尝试复制并粘贴
Hi All, Here''s a situation, I am trying to process some large Files via a Win Svc using FileMonitor. During the test process I tried copy and pasting
文件夹中的文件,并收到错误消息Process无法访问
文件foo.txt,因为它正被使用另一个过程。我相信Copy
操作还没有完成,虽然我的WinSvc开始了.....因为它
a file in a folder and got an error saying "Process can''t access the file foo.txt as it is being used by another process". I believe the Copy
operation hasn''t finished yet though my WinSvc kicked in.....since it



得到了一个


got a

FileChange通知......
如何避免竞争条件?

TIA
FileChange Notification...
How can I avoid the Race Condition?

TIA




当您的委托被调用时,请检查该文件是否可用。

如果没有,请设置一个计时器5秒钟,然后重试。继续设置那个计时器

直到文件可用......你可能在想:嘿,迈克尔

杰克逊,我知道你是国王流行但你真的知道什么关于

电脑?如何判断文件何时可用? - 好吧,我很高兴你问他们很高兴。我,迈克尔杰克逊,通常尝试打开文件

EXCLUSIVE访问(请参阅FileStream)。如果抛出异常,文件

还没有被完全复制。


Michael Jackson


Vai2000 <无**** @ microsoft.com>在消息中写道

新闻:%2 **************** @ TK2MSFTNGP12.phx.gbl ...
When your delegate gets called, check to see if the file is available.
If not, set a timer for 5 seconds and try again. Keep setting that timer
until the file becomes available... You''re probably thinking: "Hey, Michael
Jackson, I know you''re the king of pop but do you really know anything about
computers? How can I tell when the file becomes available?" -- well, I''m
glad you asked. I, Michael Jackson, typically try opening the file for
EXCLUSIVE access (see FileStream). If an exception is thrown, the file
hasn''t been fully copied yet.

Michael Jackson

"Vai2000" <no****@microsoft.com> wrote in message
news:%2****************@TK2MSFTNGP12.phx.gbl...
大家好,这是一种情况,我试图通过使用FileMonitor的Win Svc来处理一些大型文件。在测试过程中,我尝试复制并将
粘贴到文件夹中的文件,并收到错误消息,说进程无法访问文件
foo.txt,因为它正被另一个进程使用。我相信复制
操作还没有结束,虽然我的WinSvc开始.....因为它获得了
a FileChange通知......
我怎样才能避免竞争条件?

TIA
Hi All, Here''s a situation, I am trying to process some large Files via a
Win Svc using FileMonitor. During the test process I tried copy and pasting a file in a folder and got an error saying "Process can''t access the file
foo.txt as it is being used by another process". I believe the Copy
operation hasn''t finished yet though my WinSvc kicked in.....since it got a FileChange Notification...
How can I avoid the Race Condition?

TIA



这篇关于文件IO竞赛!!的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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