Prog bar/timer not working when waiting for Web Service

  • Thread starter Thread starter Richard Sheridan
  • Start date Start date
R

Richard Sheridan

Hi,

When waiting for a Web Service (async) call to complete,
my timer / progress bar doesn't work. The code looks OK;
have recreated in a simple project. Is there a
consideration around threading that prevents the timer
from firing while waiting for a web service to return, or
should this just work?

Richard
 
Which kind of timer are you using? System.Threading.Timer,
System.Timers.Timer, or System.Windows.Forms.Timer?

Are you calling the Web service synchronously or asynchronously? Are
you calling it from your main UI thread?
 
Hi,

I've tried with both types of timer, calling asynch and
synch (all suffer the same problem) and I don't know
whether or not I'm calling from the main UI thread (how
would I tell?).

To recreate, make a simple web service containg code such
as:

[WebMethod(Description="This method waits for the
specified number of milliseconds.")]
public void wait(int numberOfSeconds)
{
// do something that takes a long time
System.Diagnostics.Process proc =
System.Diagnostics.Process.GetCurrentProcess();
proc.WaitForExit(numberOfSeconds);
}

Create web reference and a simple one button form as
follows (presuming timer is wired up to a progress bar,
with designer default properties):

private void button1_Click(object sender,
System.EventArgs e)
{
myNamespace.myClass myWait = new wait.InEditStorage();
timer2.Start();
MessageBox.Show("calling");
myWait.wait(10000);
MessageBox.Show("called");
timer2.Stop();
}

The progress bar works while waiting for user to
press "calling" but suspends while the web service call
is being made. The progress bar "catches up" once
the "called" dialogue is shown. This is with a
System.Timers.Timer. If using a
System.Windows.Forms.Timer then the behaviour is similar,
except that the progress bar doesn't "catch up", it
starts from where it was previously suspended when the
web service was called.

I suspect this is something to do with UI threads, as has
been mentioned, but I don't understand what is going
wrong here.

Any help would be appreciated.

Thanks,
Richard.
 
Hi,
private void button1_Click(object sender,
System.EventArgs e)
{
myNamespace.myClass myWait = new wait.InEditStorage();
timer2.Start();
MessageBox.Show("calling");
myWait.wait(10000);
MessageBox.Show("called");
timer2.Stop();
}

The progress bar works while waiting for user to
press "calling" but suspends while the web service call
is being made. The progress bar "catches up" once
the "called" dialogue is shown.

i don't really understand what is your myWait object (i have not taken the
time to look at your code carefully) but when you call "myWait.wait(10000)",
i guess that it blocks the thread for 10000 (sec ? ms ?) and you do that
inside the method called when the button1 is clicked !! It's a very bad
idea to block within the handler method of an UI event as it freezes all
your UI. If you need to perform a long action when your button is clicked,
your button1_Click method should just create a new thread and start it.That
way, this thread will be in charge of all your time consuming stuff while
your UI will be still alive.
 
Yes. Your understanding is correct.

Regards,
Justin Wan
Microsoft Partner Online Support

This posting is provided "AS IS" with no warranties, and confers no rights.
 
Back
Top