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

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

dotsandcomsAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Manoj PatilSr. Software EngineerCommented:
0
dotsandcomsAuthor 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

0
VoloxCommented:
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.
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
dotsandcomsAuthor Commented:
Hi,
I have used JQuery Progressbar from http://t.wits.sg/jquery-progress-bar/
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C#

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.