0

I have a c# winform desktop app. It is checking for messages on my Server by invoking my [web method].

As I am in development this [web method] is not always available for my test client to use.

Nevertheless the client is still invoking this [web method].

I have noticed in this scenario (on my PC build) that CPU usage goes to 25%.

At the moment my [web method] is invoked on my client in a while(true) loop. Subsequently, the [web method] is called recursively.

This while loop has been started by a new Thread.

I have a c# winform desktop app. It is checking for messages on my Server by invoking my [web method].

As I am in development this [web method] is not always available for my test client to use.

Nevertheless the client is still invoking this [web method].

I have noticed in this scenario (on my PC build) that CPU usage goes to 25%.

At the moment my [web method] is invoked on my client in a While(true) loop. Subsequently, the [web method] is called recursively.

This while loop has been started by a new Thread.

Thread _th = new Thread(MyLoop);
_th.Start();


void MyLoop()
{
  while (true)
  {
    if (Disconnect)
    {
      return;
    }
    string[] _requests = Shared.WSconnector.GetRequests(Shared.ActiveMac);
    //do something with these server requests...
  }
}

Whilst the outage of my Web Services will hopefully be minimal it can obviously still happen.

How can i protect my client(s) from this CPU increase?

Thanks

Whilst the outage of my Web Services will hopefully be minimal it can obviously still happen.

How can i protect my client(s) from this CPU increase?

Andrew Simpson
  • 6,883
  • 11
  • 79
  • 179
  • Could you show some code on how you invoke this [web method]. – Jite Nov 07 '13 at 07:44
  • How often do you check? We have sth similiar where we test eriodically if other machines are reachable. You can also add a sleep in the while loop, so it does not bang against a not available system... – Offler Nov 07 '13 at 07:45
  • @Jite Hi, code added.. – Andrew Simpson Nov 07 '13 at 07:51
  • @Offler Hi, thanks for your feedback. I had considered putting a thread.sleep(1000) in but just wanted to check here first. The issue for me is that communications need to be real-time as possible. – Andrew Simpson Nov 07 '13 at 07:53

1 Answers1

4

Do not call web client in while(...) loop, but use a System.Timers.Timer.

The call itself make async: How to use HttpWebRequest (.NET) asynchronously, so no any CPU intense operation will be involved in all this.

This will lead, naturally, to slight re-architecturing of your code, but this is a way to go.

Community
  • 1
  • 1
Tigran
  • 61,654
  • 8
  • 86
  • 123
  • Hi, I had considered doing that as well. So do you think I should remove the Thread. Put the 'web invoker' code in a timer and then use the async call within the Timer instead? Many Thanks – Andrew Simpson Nov 07 '13 at 07:54
  • @AndrewSimpson: yes, I would schedule timer, *after* I get any type of responce from the service. So first get responce (success or failure), after start timer for next scheduler web service query. In this way I would avoid that timer overlap response still in run, and start another response. – Tigran Nov 07 '13 at 07:59
  • Cool, so In timer_tick event do: this.timer.enabled = false; 'invoke my [web method]'; this.timer.enabled = true;? Thanks.. – Andrew Simpson Nov 07 '13 at 08:06
  • @AndrewSimpson: correct. run timer only on responce, that is. – Tigran Nov 07 '13 at 08:13