简体   繁体   中英

“…because this call is not awaited”

using System;
using System.Net.Sockets;
using System.Threading.Tasks;

namespace Rohan.Gateway
{
    public class Player
    {
        private PlayerSocket _mapSocket;
        private PlayerSocket _clientSocket;

        public int _characterId = -1;
        public int _characterType = -1;
        public string _characterName = "";
        public int _playerId = -1;
        public string _dumpFolder = "";
        public int _packetIndex;

        public PlayerSocket mapSocket
        {
            get { return this._mapSocket; }
        }

        public PlayerSocket clientSocket
        {
            get { return this._clientSocket; }
        }

        public bool isConnected
        {
            get { return this._mapSocket.isConnected && this._clientSocket.isConnected; }
        }

        public Player(Socket clientSocket)
        {
            this._clientSocket = new PlayerSocket(clientSocket, this, PlayerSocketType.Client);
            Socket socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.IP);
            socket.Connect(TCPListenerEx.remoteAddress, 22100);
            this._mapSocket = new PlayerSocket(socket, this, PlayerSocketType.MapServer);
            this.ValidateConnection();
        }

        public bool ValidateConnection()
        {
            if (!this.isConnected)
            {
                this.Disconnect();
                return false;
            }

            return true;
        }

        public void Disconnect()
        {
            this._mapSocket.Disconnect();
            this._clientSocket.Disconnect();
        }

        public void ReceiveClientData()
        {
            if (this.ValidateConnection())
            {
                this._clientSocket.ReceiveAsync();
            }
        }

        public void DispatchClientData()
        {
            if (this.ValidateConnection())
            {
                this._mapSocket.SendAsync(this._clientSocket.reader);
            }
        }

        public void ReceiveMapserverData()
        {
            if (this.ValidateConnection())
            {
                this._mapSocket.ReceiveAsync();
            }
        }

        public void DispatchMapserverData()
        {
            if (this.ValidateConnection())
            {
                this._clientSocket.SendAsync(this._mapSocket.reader);
            }
        }
    }
}

On the lines below, I get this warning

because this call is not awaited

public void ReceiveClientData()
{
    if (this.ValidateConnection())
    {
        this._clientSocket.ReceiveAsync();
    }
}

public void DispatchClientData()
{
    if (this.ValidateConnection())
    {
        this._mapSocket.SendAsync(this._clientSocket.reader);
    }
}

public void ReceiveMapserverData()
{
    if (this.ValidateConnection())
    {
        this._mapSocket.ReceiveAsync();
    }
}

public void DispatchMapserverData()
{
    if (this.ValidateConnection())
    {
        this._clientSocket.SendAsync(this._mapSocket.reader);
    }
}

Why am I getting this error?

In response to the comment asking:

Have you tried adding async to the signature then use await for the call of the method

You answered:

Yes, i did it already

But none of the code you posted shows this. And this is in fact the right way to correct this warning message.


Without a good Minimal, Complete, and Verifiable example that shows clearly all of the relevant types (including the PlayerSocket type and how these methods are being called) it's impossible to say for sure how you should fix your code. Your options include:

  • Disable the warning with the #pragma warning directive
  • Implicitly suppress the warning by assigning the returned Task object from each ...Async() method to a local variable. Eg:

public void ReceiveClientData()
{
    if (this.ValidateConnection())
    {
        var _ = this._clientSocket.ReceiveAsync();
    }
}
  • Call the asynchronous operation synchronously. Eg:

public void ReceiveClientData()
{
    if (this.ValidateConnection())
    {
        this._clientSocket.ReceiveAsync().Wait();
    }
}
  • Correctly await the asynchronous operation. This is, frankly, by far the best choice but will be the most complicated. Primary advantage is that you get the benefit of asynchronous operation (ie the code doesn't block waiting for the operation to complete), but you will still be able to observe any exception that might occur. Primary disadvantage is that you will have to change each method from void to async Task , and then effect the same change to each method's caller. And each caller of those callers, and so on, until you get to the top of the call stack for the thread.

Personally, I would strongly recommend the third or (especially) the fourth option. They are the only options that allow you to add try / catch to observe exceptions. There are some scenarios where the first and second options might make sense, but I would not say that doing socket I/O would be one of those scenarios.


There are a number of existing Stack Overflow questions and answers that go into more detail as to how to deal with this sort of thing. Please decide which approach you want to use, do some research, and if you have a specific issue, post a question that includes a good code example, so that an appropriate answer can be provided.

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