0

For some reason when i put a long running web service call (doesn't matter if its a legacy one or WCF) in a background thread it seems to be locking up the UI main thread.

I cant see anything wrong with the code im putting in.

workerThreadInitialNotify = new BackgroundWorker();
workerThreadInitialNotify.WorkerSupportsCancellation = true;
workerThreadInitialNotify.DoWork += new DoWorkEventHandler(workerThreadInitialNotify_DoWork);
workerThreadInitialNotify.RunWorkerCompleted += new RunWorkerCompletedEventHandler(workerThreadInitialNotify_RunWorkerCompleted);          

workerThreadInitialNotify.RunWorkerAsync();

Then in my do work i have a webservice call like:

void workerThreadInitialNotify_DoWork(object sender, DoWorkEventArgs e)
{       
    if (!(sender as BackgroundWorker).CancellationPending)
    {
         try 
         {
             TestWebService service = new TestWebService();
             service.RunLongRunningMethod();

         }
         catch(Exception ex)
         {

         }
    }
}

When this thread is called, it occasionally locks up the UI thread, now i have setup the RunLongRunningMethod to purposely run slow and timeout (for testing purposes), but technically this shouldn't lockup the UI thread at all as its in a seperate thread.

Here is what the method contains:

using (SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["customConnection"].ConnectionString))
{
    conn.Open();

    using (SqlCommand cmd = new SqlCommand("TestDelay", conn))
    {
        cmd.CommandType = CommandType.StoredProcedure;

        try
        {
           cmd.ExecuteNonQuery();
        }
        catch (Exception ee)
        {

        }
    }        
}

and TestDelay stored proc just contains this to simulate a delay so that the web service times out:

ALTER PROCEDURE [dbo].[TestDelay]

AS

WAITFOR DELAY '00:05:20';

The strange thing is that if i replace the web service call with a Thread.Sleep(20000); in the dowork, it runs perfectly, or even if i put a long running while loop it runs perfectly also.

I have no idea why specifically the webservice makes it lock up the UI.

ps: if i set up the webservice to be hosted locally to the Win Forms app, it runs ok, only when the webservice is running on another service, the strange lockup occurs.

ps (2): i am using devexpress library for ui controls of forms while the background thread runs in the background, not sure if this is related. I cant imagine why, if this is run in a seperate thread correctly

Marty
  • 2,965
  • 4
  • 30
  • 45
  • Do you have the source for `TestWebService.RunLongRunningMethod()`? – JosephHirn May 20 '13 at 12:45
  • does it matter whats in the source? its a long running web service, just calls a stored proc which has a wait delay in it so that it purposely times out – Marty May 20 '13 at 13:05
  • 1
    I ask because the only reason this would lock up the UI thread is if `RunLongRunningMethod` is executing something on the UI thread (or otherwise blocking it; either way, the problem is in that method). – JosephHirn May 20 '13 at 13:12
  • how can it be executing something on the UI thread when its a web service on another server tho? it cant even reference the UI thread – Marty May 20 '13 at 13:36
  • 1
    The code sample you provided indicates that `RunLongRunningMethod` is the only logical place for the problem to exist. It could be invoking a delegate on the UI thread which waits for a response from the server. It could be locking a resource that the UI thread is also trying access. It could be any number of issues. The bottom line is we need more information to answer your question. – JosephHirn May 20 '13 at 13:49
  • added some extra info – Marty May 21 '13 at 06:15

1 Answers1

0

This ended up being a maxconnection .net setting in the App.config (its defaulted to a very low 2 by microsoft)

<system.net>
   <connectionManagement>
      <add address="*" maxconnection="40"/>
    </connectionManagement>
</system.net>

Its now set to 40 and the issue has gone away.

There was multiple threads running at the same time, each calling web services, each of them were blocking each other.

There is more information in these links: http://blogs.msdn.com/b/adarshk/archive/2005/01/02/345411.aspx

What is limiting the # of simultaneous connections my ASP.NET application can make to a web service?

http://msdn.microsoft.com/en-us/library/fb6y0fyc.aspx

Community
  • 1
  • 1
Marty
  • 2,965
  • 4
  • 30
  • 45