我认为这在标题中非常明显; 我想复制文件而不等待结果.
我想要这个:
static void Main(string[] args)
{
string strCmdText = @"/C xcopy c:\users\florian\desktop\mytestfile.fil p:\";
System.Diagnostics.Process.Start("CMD.exe", strCmdText);
Console.WriteLine("Finished !");
}
Run Code Online (Sandbox Code Playgroud)
基本上我的主线程在几毫秒后释放.我尝试这样做:
static void Main(string[] args)
{
var t = Task.Run(() => Copy(@"c:\Users\florian\Desktop\mytestfile.fil", "p:"));
}
private static void Copy(string source, string destination)
{
using (FileStream SourceStream = File.Open(source, FileMode.Open))
{
using (FileStream DestinationStream = File.Create(destination + source.Substring(source.LastIndexOf('\\'))))
{
SourceStream.CopyToAsync(DestinationStream);
}
}
}
Run Code Online (Sandbox Code Playgroud)
我的mytestfile.fil是在我的目标文件夹中创建的,但其大小为0kb.
问候,
我对TPL完全是绿色的,并希望在控制台应用程序中执行异步方法.
我的代码:
static void Main()
{
Task<string> t = MainAsync();
t.Wait();
Console.ReadLine();
}
static async Task<string> MainAsync()
{
var result = await (new Task<string>(() => { return "Test"; }));
return result;
}
Run Code Online (Sandbox Code Playgroud)
此任务永远运行.为什么?我错过了什么?
我正在尝试制作Main异步,所以我尝试了:
class Program
{
static async Task Main(string[] args)
{
Books books = new Books();
await books.AddBooksAsync();
}
}
Run Code Online (Sandbox Code Playgroud)
AddBooksAsync这个结构在哪里:
public async Task AddBooksAsync()
{
//some contents
}
Run Code Online (Sandbox Code Playgroud)
我收到此错误:
不包含适用于入口点的静态"主"方法
根据这篇文章,票数最高的答案表明我们可以直接在main中使用async。还是我误会了?
我的主班:
public class Program
{
public static async Task Main(string[] args)
{
ApproveEvents ap = new ApproveEvents();
List<MyModel> result = new List<MyModel>();
result = await ap.ApproveAsync();
if (result.count > 0)
{
//do something here
}
}
}
Run Code Online (Sandbox Code Playgroud)
和,
public class ApproveEvents
{
public async Task<List<MyModel>> ApproveAsync()
{
//blah blah
}
}
Run Code Online (Sandbox Code Playgroud)
Visual Studio 2017抱怨no Main method for an entry point。
我该如何解决?
我刚刚阅读了控制台项目中的特殊情况.你能告诉我我的做法是否正确吗?那里的两个工作我不确定我是否应该像在这里一样使用等待在Task.Run中,如果它是正确的你可以解释如果我从这里删除两个等待的差异将是什么.接下来的问题如果我将从WhenAny删除.Wait().一般来说它是控制台应用程序的正确方法 忘记提出异步Main到目前为止,让我们谈谈void.
public class Program
{
public static void Main()
{
//jobs can work in parael not blocking program
var job0 = Task.Run(async () => await new DoThisAsync().Run());
var job1 = Task.Run(async () => await new DoThatAsync().Run());
//Any Independent synchronous work can run meantime jobs
IndependentSynchronousMethod;
//We wait on tasks
Task.WhenAll(job0, job1).Wait();
}
}
Run Code Online (Sandbox Code Playgroud) 所以我一直在用 C# 摆弄 Web 响应和请求,当我尝试运行该程序时遇到了问题。我的其中一行代码:
var response = await httpClient.SendAsync(request);
Run Code Online (Sandbox Code Playgroud)
方法制作中需要异步
private static void Main(string[] args)
Run Code Online (Sandbox Code Playgroud)
进入
private static async Task Main(string[] args)
Run Code Online (Sandbox Code Playgroud)
看起来没有错误,但是在构建时,我收到错误消息:
程序不包含适合入口点的静态“Main”方法。
这是我的代码
private static async Task Main(string[] args)
{
try
{
/*HttpWebRequest request = (HttpWebRequest)WebRequest.Create("https://billing.roblox.com/v1/gamecard/redeem");
request.Method = "POST";
request.ContentType = "application/json";
request.Accept = "application/json";
JsonExtensionDataAttribute data = new JsonExtensionDataAttribute();
data = '3335996838'*/
using (var httpClient = new HttpClient())
{
using (var request = new HttpRequestMessage(new HttpMethod("POST"), "https://billing.roblox.com/v1/gamecard/redeem"))
{
request.Headers.TryAddWithoutValidation("Accept", "application/json");
request.Content = new StringContent("3335996838", Encoding.UTF8, "application/json"); …Run Code Online (Sandbox Code Playgroud) 我有一个非常简单的代码片段,用于测试如何在 Main() 中调用 Task<> 方法
using System;
using System.Threading;
using System.Threading.Tasks;
class Program
{
private static async Task<int> F1(int wait = 1)
{
await Task.Run(() => Thread.Sleep(wait));
Console.WriteLine("finish {0}", wait);
return 1;
}
public static async void Main(string[] args)
{
Console.WriteLine("Hello World!");
var task = F1();
int f1 = await task;
Console.WriteLine(f1);
}
}
Run Code Online (Sandbox Code Playgroud)
它无法编译,因为:
(1) F1 是异步的,所以 Main() 必须是“异步的”。
(2) 编译器说:
error CS5001: Program does not contain a static 'Main' method suitable for an entry point
Run Code Online (Sandbox Code Playgroud)
所以如果我删除 Main 的“async”,编译器会说: …
我试图了解异步等待行为,特别是它如何影响主线程,所以我的问题与以下代码相关:
static async Task Main(string[] args)
{
await LongAction();
}
static Task LongAction() => Task.Delay(10000);
Run Code Online (Sandbox Code Playgroud)
主线程LongAction执行时会发生什么。据我了解,如果调用者线程不是主线程(让我们称之为线程2),在可等待操作进行期间,该线程(线程2)将返回到ThreadPool(使用默认任务调度程序)。但是主线程会发生这种情况吗(看起来不是这样,因为最初它不是从 TP 中获取的)?如果主线程是具有线程上下文的 UI 线程怎么办?
我正在尝试学习异步和等待.NET 4.5的功能.首先,这是我的代码
static async void Method()
{
await Task.Run(new Action(DoSomeProcess));
Console.WriteLine("All Methods have been executed");
}
static void DoSomeProcess()
{
System.Threading.Thread.Sleep(3000);
}
static void Main(string[] args)
{
Method();
//Console.WriteLine("Method Started");
Console.ReadKey();
}
Run Code Online (Sandbox Code Playgroud)
此代码不会在控制台上给我任何结果.我不明白为什么.我的意思是不是任务假设只是没有阻塞的线程.但是,如果我在main方法中取消注释Console.WriteLine(),一切似乎都正常.
谁能告诉我这里发生了什么?
我编写了以下内容(一个简单的控制台应用程序)来测试我对C#中异步和等待的理解.
public class Program
{
public static void Main(string[] args)
{
Console.WriteLine("Async result: " + getLengthFromWebsiteAsync());
}
public static async Task<int> getLengthFromWebsiteAsync()
{
HttpClient httpClient = new HttpClient();
Task<string> sampleTask = httpClient.GetStringAsync("http://www.adobe.com");
Console.WriteLine("Before the await: ");
int lengthRequired = (await sampleTask).Length;
Console.WriteLine("After the await: " + lengthRequired.ToString());
return lengthRequired;
}
Run Code Online (Sandbox Code Playgroud)
以下是我运行的结果:
Before the await:
Async result: System.Threading.Tasks.Task'1[System.Int32]
Run Code Online (Sandbox Code Playgroud)
我的问题是,是不是应该出现的"等待之后:"这一行?我是否在理解异步/等待流程的错误轨道上?
这是一个模仿我的实际案例流程的程序:
using System;
using System.Threading.Tasks;
namespace TestAsync
{
interface IInterface
{
Task<int> DoSomething(int i);
}
class MyClass : IInterface
{
public async void MainAsync()
{
var i = 1;
Console.WriteLine("Start MainAsync:" + i);
var t = DoSomething(i);
Console.WriteLine("After DoSomething: " + i );
i = await t;
Console.WriteLine("Done waiting: " + i);
}
public async Task<int> DoSomething(int i)
{
i = i + 1;
Console.WriteLine("In Something:" + i);
await Task.Delay(1000);
Console.WriteLine("After Long Process: " + i);
return i;
}
} …Run Code Online (Sandbox Code Playgroud) 我将raspberry pi设置为服务器,通过HTTP将json作为输入."API"允许设置连接到pi的LED.一切正常,我可以从浏览器发送请求,一切都很好.
到达时,响应需要一段时间.这就是我想要异步沟通的原因.
Run Code Online (Sandbox Code Playgroud)// Three things to note in the signature: // - The method has an async modifier. // - The return type is Task or Task<T>. (See "Return Types" section.) // Here, it is Task<int> because the return statement returns an integer. // - The method name ends in "Async." async Task<int> AccessTheWebAsync() { // You need to add a reference to System.Net.Http to declare client. HttpClient client = new HttpClient(); // GetStringAsync returns a Task<string>. …
I have the following code
class Program
{
public async Task<bool> StartMyTask()
{
await Foo();
return true;
}
public async Task<bool> Foo()
{
for (int i = 0; i < 1000000; i++)
{
Console.WriteLine("Loop");
}
return true;
}
static void Main(string[] args)
{
Program obj = new Program();
var myTask = obj.StartMyTask();
Console.WriteLine("Before Task Return");
Console.ReadLine();
}
}
Run Code Online (Sandbox Code Playgroud)
根据我的理解,当调用“await Foo()”时,将创建一个线程来执行“Foo()”方法,并将控制权返回给调用者(Main方法)。
考虑到这一点,“Before Task Return”应该在“Foo()”方法完成之前打印。但它没有发生,首先“Foo()”方法完成,然后显示“任务返回之前”。