R
richard.markiewicz
Hi,
I'm developing a custom web part (basically an ASP web control in a
fancy wrapper if you're not familiar), something which I've done
several times before. However this one is a little different, as it
exists on a page and when the user presses a button, has to do a long
running operation. I want the page to still be interactive in this
time, with the web part showing status updates until it's finished.
So - I've implemented the Microsoft AJAX controls to allow me to
refresh just the web part when I need to update it. I can update the
panel using a Timer control. Now I need to do the actual work - and to
do that I believe the only way is to spin the work out into a separate
thread? I've never worked with threading before, and am anxious about
ending up thedailywtf.com. My requirements are very simple - just a
single thread to do the work in the background.
So - I was hoping someone with more experience might look over what
I've got and tell me if I'm on the right lines, or heading for a fall:
System.Web.UI.Timer myTimer = new System.Web.UI.Timer();
Thread threadObj;
private static string Status = "";
private void HandleButtonClick(object sender, EventArgs
eventArgs)
{
Page.Validate();
if (Page.IsValid)//Do some validation on the information
entered
{
myTimer.Enabled = true;
threadObj = new Thread(new
ThreadStart(LongRunningProcess));
threadObj.Start();
}
}
void myTimer_Tick(object sender, EventArgs e)
{
if ((Status == "Error") || (Status == "Finished"))
{
T.Enabled = false;
}
}
private void LongRunningProcess()
{
try
{
//Do some work that's going to take a while, call some
other classes/methods
Status = "Finished that bit of work";
//Do some work that's going to take a while, call some
other classes/methods
Status = "Finished";
}
catch (Exception ex)
{
Status = "Error";
}
}
Is it appropriate to use a thread in this scenario? Is it appropriate
for me set a "status" in my long running process, and then poll that
status using a timer every few seconds? Do I need to be doing anything
else with my thread when it's finished, or when it errors?
Many many thanks in advance, Richard
I'm developing a custom web part (basically an ASP web control in a
fancy wrapper if you're not familiar), something which I've done
several times before. However this one is a little different, as it
exists on a page and when the user presses a button, has to do a long
running operation. I want the page to still be interactive in this
time, with the web part showing status updates until it's finished.
So - I've implemented the Microsoft AJAX controls to allow me to
refresh just the web part when I need to update it. I can update the
panel using a Timer control. Now I need to do the actual work - and to
do that I believe the only way is to spin the work out into a separate
thread? I've never worked with threading before, and am anxious about
ending up thedailywtf.com. My requirements are very simple - just a
single thread to do the work in the background.
So - I was hoping someone with more experience might look over what
I've got and tell me if I'm on the right lines, or heading for a fall:
System.Web.UI.Timer myTimer = new System.Web.UI.Timer();
Thread threadObj;
private static string Status = "";
private void HandleButtonClick(object sender, EventArgs
eventArgs)
{
Page.Validate();
if (Page.IsValid)//Do some validation on the information
entered
{
myTimer.Enabled = true;
threadObj = new Thread(new
ThreadStart(LongRunningProcess));
threadObj.Start();
}
}
void myTimer_Tick(object sender, EventArgs e)
{
if ((Status == "Error") || (Status == "Finished"))
{
T.Enabled = false;
}
}
private void LongRunningProcess()
{
try
{
//Do some work that's going to take a while, call some
other classes/methods
Status = "Finished that bit of work";
//Do some work that's going to take a while, call some
other classes/methods
Status = "Finished";
}
catch (Exception ex)
{
Status = "Error";
}
}
Is it appropriate to use a thread in this scenario? Is it appropriate
for me set a "status" in my long running process, and then poll that
status using a timer every few seconds? Do I need to be doing anything
else with my thread when it's finished, or when it errors?
Many many thanks in advance, Richard