Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

How to know if a html file has finished being written

Posted on 2011-02-23
5
Medium Priority
?
342 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 500 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 100 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 400 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 1000 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

Hire Technology Freelancers with Gigs

Work with freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely, and get projects done right.

Question has a verified solution.

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

This article is for Object-Oriented Programming (OOP) beginners. An Interface contains declarations of events, indexers, methods and/or properties. Any class which implements the Interface should provide the concrete implementation for each Inter…
A long time ago (May 2011), I have written an article showing you how to create a DLL using Visual Studio 2005 to be hosted in SQL Server 2005. That was valid at that time and it is still valid if you are still using these versions. You can still re…
How to fix incompatible JVM issue while installing Eclipse While installing Eclipse in windows, got one error like above and unable to proceed with the installation. This video describes how to successfully install Eclipse. How to solve incompa…
We’ve all felt that sense of false security before—locking down external access to a database or component and feeling like we’ve done all we need to do to secure company data. But that feeling is fleeting. Attacks these days can happen in many w…
Suggested Courses

972 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