2

In my setter method of a property which bounds to UI, I need to call an async method.

public string Property
{
   set {
        _property = value;

        AMethodAsync();
   }
}

But I am getting a compiler warning saying 'execution of this call method continues before the call is complete'.

How can I fix this compiler warning?

I read this thread, How to call an async method from a getter or setter? but the Dispatcher is not available on Windows phone.

Thank you.

Community
  • 1
  • 1
n179911
  • 19,547
  • 46
  • 120
  • 162
  • Maybe you could wrap the call (plus the `.Wait()` call) into a function and stick it in the `ThreadPool` for execution. – Philip Pittle Oct 25 '14 at 07:29
  • I agree with @PhilipPittle. You should to use System.Threading.ThreadPool.QueueUserWorkItem(delegate(object state) { AMethodAsync(); }); – Sohaty Oct 25 '14 at 08:13
  • Could you explain why you want to invoke the async method in the property? Based on the purpose there are several ways to achieve a goal without calling the async method in the setter. The compiler warning is not a problem at all if you know what you are doing, though such calls are actually a bad practice. – danyloid Oct 25 '14 at 09:30
  • I need to call async method in order to relate the change in my property (in Setter of the property) to server. – n179911 Oct 29 '14 at 04:41

1 Answers1

1

I interpreted your question as simply how to disable a warning. You can temporarily disable this particular warning in this property like this:

#pragma warning disable 4014
public string Property
{
    set
    {
        _property = value;
        AMethodAsync();
    }
}
#pragma warning restore 4014

This is useful to silence the warning inside a section of code. Keep in mind, all this does is stops the compiler from generating the warning. Usually if you do this, it means you have a good reason for ignoring the warning in the first place.


Another way to silence the warning is to assign the task object to a variable like this:

public string Property
{
    set
    {
        _property = value;
        var task = AMethodAsync();
    }
}
Decade Moon
  • 32,968
  • 8
  • 81
  • 101