简体   繁体   中英

Call async Method from a normal method c#

I am trying to call an async method from a normal method. I have the following two issues here:

  1. When I use Task.Run in class A to class B's method, JQuery is not receiving the call.
  2. I cannot use await because it throws this error:

    The await can only be used in an async method

I am not sure what the issue is with respect to the first problem I am having. Secondly, is there a way to use await when calling classB because it seems to work when I made a call from a different method that is async.

    public class A :IA
    {
        public A ()
    {
        consumer.Received += Received;
    }
        public void Received(object sender, BasicDeliverEventArgs ea)
        { 
           //get message here and send to classB 
            Task.Run(() => classB.PingMesssage(Message));
        }
    }


    public class B 
    {

       public async Task PingMesssage(string message)
        {
             await InvokeClientMethodToAllAsync("callJQueryMethod", "message"); //calling js here
        }
    }

You can make Received async and await a call to PingMessage :

public async void Received(object sender, BasicDeliverEventArgs ea) {
    await classB.PingMessage(message);
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM