0

Im using a web service for getting some result in long array as below:

      try
      {

            System.Threading.ThreadPool.QueueUserWorkItem((o) =>
            {
                byte[] result = (new MagfaGetMessages()).getMessages(false,SMSUseProxy,SMSProxyAddress,SMSProxyUserName,SMSProxyPassword,1000);
            });
            }
        }

        catch
        {
            SMSwebServiceFailed = DateTime.Now.ToString("yyyy/MM/dd HH:mm:ss:ff");
        }



// GetMessage Class

         public MAGFAWebService.DatedCustomerReturnIncomingFormat[] getMessages(Boolean useProxy, String proxyAddress, String proxyUsername, String proxyPassword, String username, String password, String domain, int numberOfMessages)
    {
        lock (MagfaLock)
        {
            MAGFAWebService.SoapSmsQueuableImplementationService sq = new MAGFAWebService.SoapSmsQueuableImplementationService();

            if (useProxy)
            {
                WebProxy proxy;
                proxy = new WebProxy(proxyAddress);
                proxy.Credentials = new NetworkCredential(proxyUsername, proxyPassword);
                sq.Proxy = proxy;
            }
            sq.Credentials = new System.Net.NetworkCredential(username, password);
            sq.PreAuthenticate = true;

            return (MAGFAWebService.DatedCustomerReturnIncomingFormat[])sq.getMessages(domain, numberOfMessages);
        }

    }

but some times I get this error :

The request failed with HTTP status 504: Gateway Time-out.

I used try{} catch as I shown in my code, I want to know why I got error and why it dosent catch?

the error will occure in this line :

  MAGFAWebService.DatedCustomerReturnIncomingFormat[])sq.getMessages(domain, numberOfMessages

thanks for any helping.

Elahe
  • 1,379
  • 2
  • 18
  • 34

2 Answers2

0

504: Gateway Time-out means that you could connect to the host, but the connection got timed out due to some reason. Reasons could be due to problem with the host itself, its agent, DNS settings, or connectivity issues. There is a question similar to this in: Difference between operation has time out and (504) Gateway Timeout

Community
  • 1
  • 1
  • Note that information in the post while correct is completely unrelated to the question - "why operations scheduled to be executed on thread pool asynchronously don't throw exception when scheduled (before starting the operation)?". – Alexei Levenkov Feb 10 '15 at 05:27
  • so It dosent cthrow exception because of thread pooling ? @AlexeiLevenkov – Elahe Feb 10 '15 at 05:50
0

Try/catch in the sample catches exception that happen during call to ThreadPool.QueueUserWorkItem and nothing else. One can expect something like "null passed as queued item" or hypothetical "not enough space in the pool", but exceptions thrown by scheduled operation will not show up in that try/catch block.

One of the reason is operation may not even start for some time after that try/catch block is finished.

Fix - try/catch in asynchronous operation itself.

Alternatively - use new async/await that allow you to write asynchroneous code that look synchronous:

try {
   var message = await GetMessagesAsync();
}
catch (Exception ex) {
  LogException(ex); 
}

async Task<IEnumerable<Message>> GetMessagesAsync() {....}
Alexei Levenkov
  • 98,904
  • 14
  • 127
  • 179
  • I got what happen and why try/catch dosent work. but can u explain more that how can I fix this problem? – Elahe Feb 10 '15 at 06:19