How to use HtmlTextWriter.WriteBeginTag with Threading in ASP.Net C# gives ?

dotsandcoms
dotsandcoms used Ask the Experts™
on
Hello experts,
I am writing a web application in which I have to do Progress Bar Like mechanism.

Untill now its work perfectly but if the process is long then the application is stuck untill the process is not completed.

So, I have implemented Threading. But after implementation of threading I am not able to use HtmlTextWriter's WriteBeginTag or any other Write method which I am using for ProgressBar.

My Sample code :
ProgressServer.cs
        public void Begin()
        {
            // create the unbuffered output
            // for beginning of step actions
            if(context == null)
                context = System.Web.HttpContext.Current;
            htmlOutput = new HtmlTextWriter(context.Response.Output);
            context.Response.Buffer = false;
            htmlOutput.WriteFullBeginTag("html");//Gives Error
            htmlOutput.WriteFullBeginTag("body");
        }

public void Increment(int increment, string message){
HtmlTextWriter htmlOutput = new HtmlTextWriter(context.Response.Output);
htmlOutput.WriteFullBeginTag("script");
htmlOutput.Write(String.Format("window.parent.SetProgress({0}, '{1}');", progressNow.ToString(), message.Replace("\\", "\\\\").Replace("'", "\\'").Replace(Environment.NewLine, "\\n")));
htmlOutput.WriteEndTag("script");
htmlOutput.Flush();
}


Process.aspx.cs
protected void Page_Load(object sender, EventArgs e)
{
//Other Coding
Thread     m_WorkerThread = new Thread(new ThreadStart(this.ProcessMethod));
m_WorkerThread.Start();
}

void ProcessMethod()
{
//Other Coding
ProgressServer progressServer = new ProgressServer();
progressServer.Begin();
//Other Coding
//Other Coding
progressServer.Increment(completePercent, String.Format("Total Process Completed Now :   {0} of {1}", completePercent, 100));
}

Open in new window

Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®

Author

Commented:
Hi,
I always got error at
ClientScriptManager.RegisterJQueryControl(this, builder); "Object reference not set to an instance of an object. ".

Even I download the dna jquery webcontrols which also give me same error. My Implementation :
Progressbar pb = new Progressbar();

        pb.ShowPercentage = true;
        pb.Value = 100;
        pb.BackColor = System.Drawing.Color.Red;
        pb.BorderColor = System.Drawing.Color.White;
        pb.ValueChanged += new EventHandler(pb_ValueChanged);
        this.Form.Controls.Add(pb);

Open in new window

Commented:
Well to speak to your original issue...

I believe the source of your issue is that when you launch the thread to perform the work, you then allow the page to go ahead and complete it's rendering.  Therefore, from the .Net / IIS perspective the page lifecycle had completed when your thread comes back around to use HttpContext; hence the error.

A jQuery progress bar is certainly one option.  Another is to use the old standby of pushing a page that has an animation of a progress bar with an automatic reload to go back to the server to determine whether the thread has completed or not.  

You could also potentially combine what you had before with threading... Keep the progress bar that is feed via the HtmlWriter (in the main page thread) but have the actual work happening on a seperate thread.  When the work takes longer than X amount of time, have the page complete with an auto-reload to come back to the progressbar feeding page again.  This way you keep the progress generation the way you want it, but you avoid browser timeouts.

Author

Commented:
Hi,
I have used JQuery Progressbar from http://t.wits.sg/jquery-progress-bar/

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial