Solved

How to know if a html file has finished being written

Posted on 2011-02-23
5
336 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
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

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
SSRS ReportViewer report timeout 7 104
ASP.NET 5 Templates 2 66
Pagebreak issue while printing the aspx page 3 15
Need a starter for ETL protocol? 4 42
Entity Framework is a powerful tool to help you interact with the DataBase but still doesn't help much when we have a Stored Procedure that returns more than one resultset. The solution takes some of out-of-the-box thinking; read on!
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…
Video by: Mark
This lesson goes over how to construct ordered and unordered lists and how to create hyperlinks.

895 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

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now