Click here to Skip to main content
15,901,666 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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 :
C#
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();
       }



the function VerificareCSV, just checks if the respective file can be read or not, and returns false or true:

C#
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;
       }




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!
Posted
Comments
Timberbird 17-Nov-11 4:01am    
Another way to pause is to wait for event or timer, but Sleep usually don't throw exceptions anyway. Could it be due to this call:
processlist[0].Kill();
Maybe you have no rights to kill the process, or it doesn't exist by that time, or processlist array is just empty? Put this line in try...catch block and see if it causes the exception
Mitran Stefanita Alexandru 17-Nov-11 4:06am    
no, because when I close the proccess dose stop the program and sayse that a process can't be killed.
this crash only occures when is waiting for a big file to be converted
Timberbird 17-Nov-11 4:22am    
Well, you can replace Sleep() with ManualResetEvent.WaitOne(), but I doubt it'll help. Sleep() shouldn't be that harmful :). Have you checked memory usage?

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...
 
Share this answer
 
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.
 
Share this answer
 
Comments
Mitran Stefanita Alexandru 17-Nov-11 4:17am    
I tried with timer, but don't know how, the file that I'm cheking will be written when titan ends the conversion, here :"if (Size.Length > 629145600): I check if the file .pcl I'm trying to convert is a bigger then 600 MB, to sleep the thread 3 min, because at start I only checked when a file was bigger than 100 MB and sleeped it 1 min. My app crash the same, then I sleeped it 2 min and it worked for files under 600 MB, know crashes at files at 1 GB... that is way I think is doing this because of the Thread.Sleep function. this is the crash https://plus.google.com/105606022912012406932/posts
Mitran Stefanita Alexandru 17-Nov-11 5:32am    
Can you show me an example or the code that you use for timer... pls

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