Display contents of text file in the Browser

R

Rathtap

My web app starts a process on a separate thread that is long running
while I display the user a please wait page. This process writes
continuously to a log file. It would be a good idea to display its
contents in the wait page so the user knows something is going on.
I tried the following in the page load event but I get errors because
the file is being written to.
private void Page_Load(object sender, System.EventArgs e)
{
Response.Write(@"<META HTTP-EQUIV=Refresh CONTENT=""3; URL=""> ");
FileInfo dFile = new FileInfo(file);
long filesize = dFile.Length;
long start = 0;
long end = filesize;
Response.WriteFile(file, start, end);
Response.End();
}
The error reads:
System.IO.IOException: The process cannot access the file
"C:\Trace.log" because it is being used by another process.

Any suggestions?
 
K

Kevin Spencer

It sounds like your app is attempting to read from the file at the same time
that it is writing to it, which is not possible. I'm guessing that your
asynchronous thread is writing the file, while the Page is continuing to
process and attempting to read the file and display it at the same time. As
the old saying goes, "you can't have it both ways." You can either create an
asynchronous thread to write the file and not attempt to read it at the same
time, or you can write synchronously, in which case your Page class will
wait until the file is writeen and closed (I assume you're closing your
stream in the thread) before it attempts to read it.

Of course, this doesn't solve the problem of waiting for the page to load. I
have one idea. Perhaps you could store the log information in a string in
memory, and return that string to the client, while launching a separate
thread to write the string to a file.

--
HTH,
Kevin Spencer
..Net Developer
Microsoft MVP
Big things are made up
of lots of little things.
 
K

Kevin Spencer

That is certainly an alternative solution. :)

--
HTH,
Kevin Spencer
..Net Developer
Microsoft MVP
Big things are made up
of lots of little things.

Bill Borg said:
Likewise, is there a way to use a queue? MyLongProcess adds to queue each
line it writes to log file, and MyWaitPage dequeues periodically to update
the client display?
 
R

Rathtap

Well, you are right about writing and reading to the same file at the
same time us not going to work. I however do not know if writing the
log info in a string in memory is going to work either in my case
since the process that is writing to the log is itself started by an
asynchronous thread in the web page using System.Diagnostics.Process
using the following code:
private void Page_Load(object sender, System.EventArgs e)
{
string command= Request.Params["cmd"];
StartProgressDelegate startProgress = new
StartProgressDelegate(RunProcess);
startProgress.BeginInvoke(command, null, null); //redirect the
user to the response page
//while the thread is working
Response.Redirect("wait.aspx");
}
private string RunProcess(string cmd)
{
Session["Finished"]=false;
System.Diagnostics.Process p;
p= new System.Diagnostics.Process();
p.StartInfo.FileName= cmd;
p.StartInfo.WindowStyle = Diagnostics.ProcessWindowStyle.Normal;
p.StartInfo.RedirectStandardOutput = true;
p.StartInfo.UseShellExecute = false;
p.Start();
// must have the readToEnd BEFORE the WaitForExit(), to avoid a
deadlock condition
string output= p.StandardOutput.ReadToEnd();
p.WaitForExit();
Session["Finished"]=true;
return output;
}
Is there a way that I can create events in the process(which is
really a console app) and catch the event in the wait page?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top