Solved

How to know if a html file has finished being written

Posted on 2011-02-23
5
334 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
Comment Utility
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
Comment Utility
Could you check if the file exists?

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

Assisted Solution

by:käµfm³d 👽
käµfm³d   👽 earned 100 total points
Comment Utility
@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
Comment Utility
@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
Comment Utility
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

How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

Join & Write a Comment

IntroductionWhile developing web applications, a single page might contain many regions and each region might contain many number of controls with the capability to perform  postback. Many times you might need to perform some action on an ASP.NET po…
More often than not, we developers are confronted with a need: a need to make some kind of magic happen via code. Whether it is for a client, for the boss, or for our own personal projects, the need must be satisfied. Most of the time, the Framework…
Illustrator's Shape Builder tool will let you combine shapes visually and interactively. This video shows the Mac version, but the tool works the same way in Windows. To follow along with this video, you can draw your own shapes or download the file…
In this tutorial you'll learn about bandwidth monitoring with flows and packet sniffing with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're interested in additional methods for monitoring bandwidt…

771 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

10 Experts available now in Live!

Get 1:1 Help Now