Solved

How to know if a html file has finished being written

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

Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
What is "public object" in C#? 3 45
asp.net mvc5 6 12
What Does This C# Code Block Do? 5 24
insert value of checklistbox checked 4 25
ASP.Net to Oracle Connectivity Recently I had to develop an ASP.NET application connecting to an Oracle database.As I am doing it first time ,I had to solve several problems. This article will help to such developers  to develop an ASP.NET client…
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!
Windows 10 is mostly good. However the one thing that annoys me is how many clicks you have to do to dial a VPN connection. You have to go to settings from the start menu, (2 clicks), Network and Internet (1 click), Click VPN (another click) then fi…
In this video I am going to show you how to back up and restore Office 365 mailboxes using CodeTwo Backup for Office 365. Learn more about the tool used in this video here: http://www.codetwo.com/backup-for-office-365/ (http://www.codetwo.com/ba…

813 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

19 Experts available now in Live!

Get 1:1 Help Now