任务的返回类型出了错 - 英寸×有错误的返回类型" [英] Task return types gone wrong - "x has the wrong return type"

查看:273
本文介绍了任务的返回类型出了错 - 英寸×有错误的返回类型"的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个线code,即:

I have a line of code, that:

bool stop = await Task<bool>.Factory.StartNew(Listen, (TcpClient) client);

和相应的任务:

public static async Task<bool> Listen(object state)
{
    TcpClient client = (TcpClient) state;
    NetworkStream connectionStream = client.GetStream();

    IPEndPoint endPoint = client.Client.RemoteEndPoint as IPEndPoint;

    byte[] buffer = new byte[4096];

    Encoding encoding = Encoding.UTF8;

    Random randomizer = null;

    // Are we still listening?
    bool listening = true;

    // Stop the server afterwards?
    bool stop = false;

    while (listening)
    {
        int bytesRead = await connectionStream.ReadAsync(buffer, 0, buffer.Length);

        if (bytesRead > 0)
        {
            // Create a new byte array for recieved data and populate
            // with data from buffer.
            byte[] messageBytes = new byte[bytesRead];
            Array.Copy(buffer, messageBytes, messageBytes.Length);

            // Generate a message from message bytes.
            string message = encoding.GetString(messageBytes);

            switch (message)
            {
                /*
                Message handling, where one can set stop to true.
                */
            }
        }

        if (bytesRead == 0 || listening == false)
        {   
            client.Client.Dispose();
            break;
        }
    }

    return stop;
}

我已经离开主要部分了code,因为我觉得他们不影响问题的性质。

I have left major parts out of the code, as I feel they do not affect the nature of question.

所以,是的,在运行应用程序时,我得到一个错误:

So yes, when running the application, I get an error:

76: <..>\Program.cs(63,63): Error CS0407: 'System.Threading.Tasks.Task<bool> SOQAsyncQuit.MainClass.Listen(object)' has the wrong return type (CS0407) (SOQAsyncQuit)

好吧,我试图等待Task.Factory.StartNew(...),但后来我结束了一个不同的错误:

Well, I have tried to await Task.Factory.StartNew(...), but then I end up with a different error:

76: <..>\Program.cs(35,35): Error CS0121: The call is ambiguous between the following methods or properties: 'System.Threading.Tasks.TaskFactory.StartNew(System.Action<object>, object)' and 'System.Threading.Tasks.TaskFactory.StartNew<System.Threading.Tasks.Task<bool>>(System.Func<object,System.Threading.Tasks.Task<bool>>, object)' (CS0121) (SOQAsyncQuit)  
76: <..>\Program.cs(57,57): Error CS0407: 'System.Threading.Tasks.Task<bool> SOQAsyncQuit.MainClass.Listen(object)' has the wrong return type (CS0407) (SOQAsyncQuit)
76: <..>\Program.cs(29,29): Error CS0029: Cannot implicitly convert type 'void' to 'bool' (CS0029) (SOQAsyncQuit)

我有工作无效任务,它只是这一点,我很期待转变成一个类型化的结果。因为我是相当新的C#和.NET总的来说,我在这里的线索。

I have working void tasks, it's just this that I'm looking forward to transform into a typed result. Since I'm fairly new to C# and .NET in general, I'm out of clues here.

我也一直挖通过: http://msdn.microsoft.com /en-us/library/hh524395.aspx ,的 http://msdn.microsoft.com/en-us/library/dd537613(v = vs.110)的.aspx ,SO上的一些问题,不过,他们都没有使用 Task.Factory.StartNew ,我不能设法将它们链接在一起。

I have also been digging through: http://msdn.microsoft.com/en-us/library/hh524395.aspx, http://msdn.microsoft.com/en-us/library/dd537613(v=vs.110).aspx, some questions on SO, though, neither of them use the Task.Factory.StartNew and I cannot manage to link them together.

这里有什么问题?

推荐答案

通常与异步这将是更好地使用 Task.Run 而不是 StartNew 。你不能,如果你打算使用一个状态对象。在这种情况下,你需要修复 StartNew 用法是:

Usually with async it would be better to use Task.Run instead of StartNew. You can't if you plan on using a state object. In this case you need to "fix" the StartNew Usage to be:

bool stop = await Task<Task<bool>>.Factory.StartNew(Listen, client).Unwrap();

收听的返回值是的任务&LT ;布尔&GT; ,而不是 BOOL StartNew 的返回类型将需要任务&LT;任务&LT;布尔&GT;&GT; ,你将需要等待两次或使用展开 Task.Run 内置异步记住,所以它做的都是为了你。

Listen's return value is of Task<bool> and not bool, and so StartNew's return type would need to be Task<Task<bool>> which you would need to await twice or use Unwrap. Task.Run was built with async in mind and so it does that all for you.

这篇关于任务的返回类型出了错 - 英寸×有错误的返回类型&QUOT;的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆