大佬教程收集整理的这篇文章主要介绍了如果两者都在等待用户响应,那么 await Console.In.ReadLineAsync() 和 Console.In.ReadLine() 之间有区别吗?,大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
在 C# 中,编写如下代码是否有意义:
await Console.In.ReadlineAsync()
?
因为应用程序必须等待用户的响应来决定下一步做什么。
所以我不妨纠正一下:
Console.In.Readline();
如果您想在单个任务控制台应用程序中使用它们,则类似。当您想要一个多任务控制台应用程序并且必须与正在进行的任务并行运行时,一切都会改变。
@H_874_15@ using System;
using System.IO;
namespace ConsoleApplication
{
class Program
{
static async Task Main()
{
await ReadCharacters();
}
static async Task ReadCharacters()
{
String result;
using (StreamReader reader = File.opentext("exisTingfile.txt"))
{
Console.WriteLine("Opened file.");
result = await reader.ReadLineAsync();
Console.WriteLine("First line contains: " + result);
}
}
}
}
根据您的要求,我将此代码更改为波纹管
class Program
{
static async Task Main()
{
Task thisTask= Task.Run(()=>ReadCharacters());
int res = LogicalOperation(5,6);
await thisTask;
Console.ReadLine();
}
static async Task ReadCharacters()
{
String result;
using (StreamReader reader = File.opentext("exisTingfile.txt"))
{
Console.WriteLine("Opened file.");
result = await reader.ReadLineAsync();
Console.WriteLine("First line contains: " + result);
}
}
static int LogicalOperation(int x,int y )
{
Console.WriteLine("result of logial Operation is:" + (x*y).ToString());
return x*y;
}
}
此图只是为了更好的理解
以上是大佬教程为你收集整理的如果两者都在等待用户响应,那么 await Console.In.ReadLineAsync() 和 Console.In.ReadLine() 之间有区别吗?全部内容,希望文章能够帮你解决如果两者都在等待用户响应,那么 await Console.In.ReadLineAsync() 和 Console.In.ReadLine() 之间有区别吗?所遇到的程序开发问题。
如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。
本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。