Solved

How to know if a html file has finished being written

Posted on 2011-02-23
5
339 Views
Last Modified: 2012-05-11
I have a third party application, when it is executed (as a background process), it will write a html file.  User will open this html file, but how can I tell if this html file has finished being written (it takes about 5 minutes to write).  I guess I need "Please Wait .." page till it finish.

The third party application never close itself, I have to kill it after 10 minutes.  

Now I open this html file as a process, but I don't know how can I wait till it finish be written.

static void Main()
{
Process myProcess = new Process();
try
{
myProcess.StartInfo.WindowStyle= ProcessWindowStyle.Hidden;
myProcess.StartInfo.UseShellExecute = true;  
myProcess.StartInfo.FileName = "C:\\test.exe";
myProcess.Start();
Thread.Sleep(600000)  //wait 10 minutes and kill
myProcess.Kill();
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}

Process p = new Process();
try
{
p.StartInfo.WindowStyle=ProcessWindowStyle.Maximized;
p.StartInfo.UseShellExecute=true;
p.StartInfo.FileName="C:\\download.html";
p.Start();
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}
}
0
Comment
Question by:ToneElec
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
5 Comments
 
LVL 17

Assisted Solution

by:Kent Dyer
Kent Dyer earned 125 total points
ID: 34965339
Simple: Put a Sleep for 300 seconds..

Complex: WMI (untested)
SELECT * FROM Win32_Process where Caption ='test.exe'

Open in new window


Now, some pseudocode..
If exist ProcessID, then Sleep 5 seconds

HTH,

Kent
0
 
LVL 10

Assisted Solution

by:ALaRiva
ALaRiva earned 25 total points
ID: 34965448
Could you check if the file exists?

Or does it reuse the same name over?
0
 
LVL 75

Assisted Solution

by:käµfm³d 👽
käµfm³d   👽 earned 100 total points
ID: 34965529
@ALaRiva

>>  Could you check if the file exists?

Depending on how the 3rd-party tool handles its files, that may or may not work. The tool could be flushing data to the file periodically, which cause it to exist, but it may not be finished actually writing the file.


@ToneElec

I would investigate why the 3rd party tool is not closing. Is this by design, or is it some flaw in the program. Also, there is Process.WaitForExit() rather than sleeping, but this won't have any effect if the process never terminates.
0
 
LVL 10

Expert Comment

by:ALaRiva
ID: 34965568
@kaufmed,
Agreed. Flushing the Stream intermittently (from the 3rd party perspective) would be the appropriate method, IMO, so my solution wouldn't work. However, since they have a "flaw" of not closing the app or providing any feedback to the completion, maybe they didn't do it as such.

Either way, the OP has a few options, of which 1 should work.
0
 
LVL 23

Accepted Solution

by:
wdosanjos earned 250 total points
ID: 34965701
If the 3rd party application closes the file when it's done, then you should be able to identify that the file is complete by trying to get an exclusive lock on it.  

- If the file is not there then the process has not started;
- If the lock fails the file is still being written;
- If the lock succeeds the file is ready.

The code would be something like this:

bool isDone = false;
for (var i = 0; i < 120; i++)  // tries for 10 mins
{
    Thread.Sleep(5000); // 5 seconds
    try
    {
         var lock = File.Open("C:\\Download.html", FileMode.Open, FileAccess.Read, FileShare.None);
         isDone = true;
         lock.Close();
    }
    catch {}
}

if (isDone)
    Console.WriteLine("File is complete");
else
    Console.WriteLine("File generation failed");

Open in new window

0

Featured Post

Creating Instructional Tutorials  

For Any Use & On Any Platform

Contextual Guidance at the moment of need helps your employees/users adopt software o& achieve even the most complex tasks instantly. Boost knowledge retention, software adoption & employee engagement with easy solution.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
Performance in games development is paramount: every microsecond counts to be able to do everything in less than 33ms (aiming at 16ms). C# foreach statement is one of the worst performance killers, and here I explain why.
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…

730 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question