I have a pretty long task for my page, so I'm running it in a background task since I don't want it to block any postback events. Let's just assume this code looks something like this

Task task = new Task(() =>
{
    for (;;)
    {
        Thread.Sleep(5000);
        var x = (int)ViewState["progress"];
        x++;
        ViewState["progress"] = x;
    }
});

task.Start();

As you can see, I'm storing the code in the ViewState. I have a progress bar on the page along with a timer that ticks every 5 seconds to perform a postback event. In this event, I'm updating the progress bar. The code looks something like this.

protected void Timer1_Tick(object sender, EventArgs e)
{
    int perc = (int)ViewState["progress"];

    //Progress bar code here
}

The progress bar wasn't updating so I tried debugging and for some reason, I'm getting entirely different values in both the cases. The timer's event gives me 0 all the time but it seems to be updating in the loop.

I've also tried locking the ViewState or using the global variable instead, but no luck.

Related posts

Recent Viewed