CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 5 of 5
  1. #1
    Join Date
    Jul 2002
    Location
    India
    Posts
    505

    UI thread question

    Hi gurus,

    Sorry about the lengthy post. I have this question about something in the application I am working on, that has been bugging me.
    Here's the scenario.

    I have a form with several UI fields. The user enters data in these fields and eventually clicks on a submit button which sends the data for processing to a webservice. The app is such that it can't proceed without getting the return from webservice. However, for some reason, there is a need to make the UI responsive during the Web service call. Here what I do is make an async call to the webservice and then go into a wait loop, with a call to Application.DoEvents, so that any events can be handled.

    This is how the code looks...


    Code:
    private void btnSubmit_Click(object sender, System.EventArgs e)
    {
    	...
    	...
    	//Code to retrieve field values from form fields and form Request string...
    	ProcessRequest(lstrRequest);
    }
    
    
    
    
    public void ProcessRequest(string astrRequest)
    {
    	
    	iHasWSReturned = false;
    	AsyncCallback objCallBack = new AsyncCallback(WSCompleted);
    
    	IAsyncResult objAsyncResult = objWS.BeginSubmitRequest(istrProtoCol,astrRequest, objCallBack, objWS);
    
    	while(! iHasWSReturned)
    	{
    		objAsyncResult.AsyncWaitHandle.WaitOne(500,true);
    		Application.DoEvents();
    	}
    	
    	...
    	...
    	//Further processing...
    
    }
    
    
    private void WSCompleted(IAsyncResult objAsyncResult)
    {
    	MYWS.myWS objWS = (MYWS.myWS) objAsyncResult.AsyncState;
    	istrMauiRet = "";
    	istrMauiRet = objWS.EndSubmitRequest(objAsyncResult);
    	iHasWSReturned = true;
    }
    My question is -
    Say, the main UI thread is running the wait loop in the ProcessRequest method. At this point if the user clicks some other button on the UI which fires that button's click event, is
    a) a new UI thread created to handle this event? (is it even possible to have multiple UI threads?)
    b) Does the UI thread in some way execute both code paths?
    c) none of the above

    Thanks
    Satish

  2. #2
    Join Date
    Apr 2005
    Location
    Norway
    Posts
    3,934

    Re: UI thread question

    Quote Originally Posted by Satishpp
    Is it even possible to have multiple UI threads?
    Yes, it is possible, but usually not necessary.

    Quote Originally Posted by Satishpp
    Does the UI thread in some way execute both code paths?
    Yes, when you call DoEvents the events (in the window message queue) are processed, and eventually down in the callstack your 'button handler' is called. When the events are processed DoEvents return.

    - petter

  3. #3
    Join Date
    Dec 2003
    Location
    http://map.search.ch/zuerich.en.html
    Posts
    1,074

    Re: UI thread question

    One observation, why not put your "//Further processing..." into the WSCompleted method?

    View the WSCompleted as a continuation of the ProcessRequest.
    ie. No need to set a flag and no need to sit in a while loop.

    Code:
    public void ProcessRequest(string astrRequest)
    {
    	// call web method async
    }
     
    private void WSCompleted(IAsyncResult objAsyncResult)
    {
    	// extract result
     
    	// Further processing...
    }
    Useful? Then click on (Rate This Post) at the top of this post.

  4. #4
    Join Date
    Jul 2002
    Location
    India
    Posts
    505

    Re: UI thread question

    Thanks for your replies Norfy and Petter,

    Norfy, to answer your question...
    The code I have posted is simplified. The actual app is much more complex. The method WsCompleted is in a base class. All MDI child forms in the app inherit from this base class. WsCompleted is reused by several methods in all forms. "//Further processing" depends on each method's functionality.

    Also, the wait loop shown here actually does more in the actual app. It has a check in it that times the loop execution. If the loop execution exceeds 'nn' seconds, the loop is terminated and an error condition is caused. This is done so that the UI does not wait indefinetly for the web service's return.


    Yes, when you call DoEvents the events (in the window message queue) are processed, and eventually down in the callstack your 'button handler' is called. When the events are processed DoEvents return.
    Taking my original question further,

    1) now if for some reason, the 'button handler' code causes an exception to be raised, will it be caught in the try/catch in the button handler or will it be caught in the try/catch ProcessRequest? (Assuming that the entire code in ProcessRequest has a try/catch around it).
    I know that the obvious answer is that it would be caught in the button handler try/catch, but I am seeing some weird log entries for my app that is causing me to question this whole process in detail. Hence double checking...

    2) Secondly, if as in point 1, if a exception is raised in the button handler, my app logs the error and then puts up a modal error form. Since the modal form will block the UI thread's execution, what happens of the loop? Does it just sit there waiting? What happens when the Web service returns?

    Your answers / observations are most appreciated
    -Satish

  5. #5
    Join Date
    Dec 2003
    Location
    http://map.search.ch/zuerich.en.html
    Posts
    1,074

    Re: UI thread question

    I can see it's complex why not make it simpler?

    Call the ProcessRequest method asynchronously instead. The webservice has a Timeout property which you can set for synchronous calls which will give you the functionality you want.

    You can get get back on the GUI thread very easily if you need to.
    Useful? Then click on (Rate This Post) at the top of this post.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured