Results 1 to 3 of 3

Thread: How to execute a Function() and wait for it?

  1. #1

    Thread Starter
    Frenzied Member
    Join Date
    May 2002
    Posts
    1,602

    How to execute a Function() and wait for it?

    Hi!

    I have a small helper method for executing client code (windows mobile). The purpose of this helper is to minimize the lines of code on each user interaction on the client, stuff like hourglass on/off, logging etc.

    So the method looks like this 8the most basic one)

    Code:
    public static void DoWork(Action method, object caller, bool showMessageOnException)
           {
               try
               {
                   Cursor.Current = Cursors.WaitCursor;
                   method();
                   
               }
               catch (Exception ex)
               {               
                   HandleClientSideException(ex, caller, showMessageOnException);               
               }
               finally
               {
                   Cursor.Current = Cursors.Default;
               }
           }
    Now, the obvious problem with this code is that it doesn't work... the finally statement is executed before the code in the method(); is executed, rendering the whole method pointless.

    Is there any way to solve this? The code coming in can contain calls to UI, so just fire up a new thread and then use thread.Join is not an option.

    Basically, I want to execute the code in method and WAIT, and then keep executing code in the finally block. I have another helper method for async work that is far more sophisiticated. This should be used for basic syncronous calls.

    Ideas, thoughts?

    /S

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,299

    Re: How to execute a Function() and wait for it?

    Is the method being executed inherently asynchronous?

  3. #3

    Thread Starter
    Frenzied Member
    Join Date
    May 2002
    Posts
    1,602

    Re: How to execute a Function() and wait for it?

    Hi!

    I dind some research on the code, and it turned out there was a small snippet of code embedded in a lambda that was called asynchronously. And that is a totally different use case than what I was trying to accomplish here.

    With pure synchronous code being passed in the method delegate, the above code works perfectly. I could execute it like method.Invoke(); if I want... but it is not needed.

    Please feel free to add anything that could improve this pattern further, but for async code or a mix of async/sync I need a totally different solution than the one above.

    /H

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