title: C# Web Method: Ensure a reply within a time limit?
body: <p>My web service is like this:</p>
  
  <pre class="lang-cs prettyprint-override"><code>[WebMethod]
  public clsReponse MyWS(string a, string b, string c) {
    clsReponse reply = doSth( a, b, c );
    return reply;
  }
  
  public clsReponse doSth(string a, string b, string c) {
    // Call other web service and hard disk I/O (i.e. write log files)
  }
  
  public class clsReponse() {
    public bool success { get; set; }
    public string msg { get; set; }
  }
  </code></pre>
  
  <p>I know that the client call <code>MyWS</code> has set a 60 second timeout, but I want to reply within 30 seconds. If <code>doSth()</code> hasn't done its work within 30 seconds, I want to reply to the client first, while letting it finish its work.</p>
  
  <p>I'm thinking of setting a timer in <code>MyWS()</code>, changing <code>doSth()</code> to an <code>async</code> method. But first, I'm not sure if that means every request will have its own timer?<br>
  Second, I searched the internet, and it seems <code>async</code>/<code>await</code> should not use in web methods?  </p>
  
  <p>I'm so confused. Is there a better way to do it?</p>
  
  <hr>
  
  <p>Thank you for edit and comments!<br>
  I do expect to reply a message like "Taking too long" to client, but I'm not ready for second endpoint. For now, I just want to write the  process of <code>doSth()</code> into log file. So there's no output when the method is done.</p>
  
  <hr>
  
  <p>This might be the answer:</p>
  
  <pre class="lang-cs prettyprint-override"><code>[WebMethod]
  public clsReponse MyWS(string a, string b, string c) {
    float m_state = 0;
    clsReponse reply = null;
    Task task = Task.Run( () =&gt; { reply = doSth( a, b, c ); } );
    do {
      if( task.IsCompleted )
      return reply;
      m_state += (float)1000 / 1000;
      Thread.Sleep( 1000 );
    } while( m_state &lt; 30 );
    return reply = new clsReponse(){ success = false; msg = "Timeout." };
  }
  </code></pre>
  
  <p>Please answer if there is a better way to do it. Thank you.</p>