问题描述
我有一行代码:
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;
}
我在代码中省略了主要部分,因为我认为它们不会影响问题的性质.
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)
好吧,我已经尝试await 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)
我有工作 void
任务,这正是我期待转换为类型化结果的原因.由于我对 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.
这里有什么问题?
推荐答案
通常使用 async 会更好地使用 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();
Listen
的返回值是Task
bool
,所以StartNew
'返回类型需要是 Task<Task<bool>>
,您需要等待两次或使用 Unwrap
.Task.Run
在构建时考虑了 async
,因此它为您完成了所有工作.
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.
这篇关于任务返回类型出错 - “x 有错误的返回类型";的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!