暂停我的应用程序,直到可以读取文件 [英] Pause my application until a file can be read
问题描述
嗨!
我的应用程序打开另一个应用程序(红色TITIAN)将.pcl文件转换为.pdf文件,我必须等待一个文件,即转换完成后可以打开的日志文件.直到我想冻结,睡觉,暂停我的应用程序,我才尝试这样做:
Hi!
My application opens another application(RED TITIAN) to convert .pcl files into .pdf files and I must wait for a file, a log file that can be open when the conversion is complete. Until the I want to freez, sleep, pause my application , I tried this :
public void OprireProces(string fis)
{
FileInfo Size = new FileInfo(fis);
if (Size.Length > 629145600)
{
Thread.Sleep(180000);
}
//daca fisierul pcl este mai mare de 100 MB pune pauza de 2 min
else if (Size.Length > 104857600)
{
Thread.Sleep(120000);
}
fis = fis.Replace(".pcl", ".csv");
Process[] processlist = Process.GetProcessesByName("escapee");
bool p = false;
//se verifica daca fisierul csv poate fi citit
p = VerificareCSV(fis);
//daca p = false, adica fisierul csv nu poate fi citit, se reia functia de oprire a procesului
if (!p)
OprireProces(fis);
//daca p == true se opreste procesul escapee
else
processlist[0].Kill();
}
函数VerificareCSV,仅检查相应文件是否可读,并返回false或true:
the function VerificareCSV, just checks if the respective file can be read or not, and returns false or true:
public bool VerificareCSV(string fis)
{
fis = fis.Replace(".pcl", ".csv");
FileStream stream = null;
try
{
stream = File.Open(fis, FileMode.Open, FileAccess.ReadWrite, FileShare.None);
}
catch (IOException)
{
//daca se intalneste o excetie de Input/Output
//adica nu se poate deschide
//sau este inca procesat de un thread
return false;
}
finally
{
if (stream != null)
stream.Close();
}
return true;
}
当我的应用程序必须等待RED TITAN转换大文件时(大约10或15分钟),就会出现问题,我认为是Thred.Sleep的原因,但是我可以想到另一种方法来暂停我的应用程序.崩溃是("MyApplication已停止工作" ...)
有人可以告诉我是我做错了吗?
谢谢!
the problem occoures when my application has to wait for RED TITAN to convert big files, around 10 or 15 min., I think that Thred.Sleep is the cause, but I can think of another way to pause my application. The crash is ("MyApplication has stop working"...)
Can some one tell me were did I made a mistake ?
Thanks!
推荐答案
我注意到的第一件事是,您的最长暂停时间为3分钟,因此,对于写入时间超过3分钟的文件,您将获得IO例外.
我这样做的方法是检查文件的大小,然后在1/2秒后再次检查文件的大小.
如果在此期间文件大小没有更改,我会认为文件写入已完成.
您可以增加此1/2秒的检查时间,但是作为信封估计的基础,我想说,如果可以读取文件,则每1/2秒检查一次文件大小应该可以使您有所了解...
First thing I noticed is that your longest pause is for 3 minutes so for a file that takes over 3 minutes to write you are going to get an IO Exception.
The way I would do this is to inspect the file''s size, then 1/2 seconds later inspect the size again.
If the file size had not changed in that period I would consider the file writing as complete.
You could increase this 1/2 second inspection time, but as a back of envelope estimate I would say that inspecting the file size every 1/2 second should give you a good idea if it is ready to read...
在启动TITAN程序之后,您可以弹出一个模式对话框(假定为winform),并等待该文件出现在Verificare()中.
启动Titan之后,我将启动一个计时器,该计时器每#n秒调用一次Verificare(在tick方法中停止时间),每次返回false时更新UI直到成功,然后告诉用户它已完成.
我个人从不使用睡眠.
You could pop a modal dialog (assuming winforms) after you launch the TITAN thing and wait until the file appears in Verificare().
I would start a timer after you launch Titan that calls Verificare every #n seconds (stopping the time in the tick method) and each time it returns false update the UI until success and then tell the user it has completed.
Personally I NEVER use sleep.
这篇关于暂停我的应用程序,直到可以读取文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!