我正在努力学习异步等待.我正在尝试一个简单的C#Windows窗体应用程序,只有一个文本框和一个按钮.
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
const bool USE_GET_STRING_DIRECTLY = true; // just for switching logic on button1_click
private async void button1_Click(object sender, EventArgs e)
{
textBox1.Clear();
if (USE_GET_STRING_DIRECTLY)
{
// Code #1 <<<---------------------
textBox1.Text += await GetString();
}
else
{
// Code #2 <<<---------------------
var s = …Run Code Online (Sandbox Code Playgroud) 我有一个在后台运行一些任务的方法,称为backgroundworker5_doWork()。此方法调用Invoke指令,该指令帮助我访问 Windows 窗体上的控件(因为通常无法从另一个线程访问控件)
private void backgroundWorker5_DoWork(object sender, DoWorkEventArgs e)
{
if(!isAdmin)
{
//Start of invoke instruction
Invoke(new Action(() =>
{
if(test)
{
return;
}
if (ValidateAll())
{
comboBox5.Items.Clear();
packageToSendList.Clear();
progressBar3.Visible = true;
Cursor.Current = Cursors.WaitCursor;
}
}));
//End of invoke
RunAfterInvokeMethod() //Instructions called after the invoke
}
}
Run Code Online (Sandbox Code Playgroud)
我的问题是,当true时,返回指令退出调用块(理所当然)并执行RunAfterInvokeMethod(),这不是我想要的。我想要的是退出backgroundWorker5_doWork ()我想要的是在调用return后
我知道它只退出调用指令,但是否也可以使其退出父方法?也许使用其他指令?
谢谢 !
我有个疑问.我已经读过当我们想要等待特定函数来完成操作时使用await关键字.
public async void Work()
{
await SlowTask();
Console.WriteLine("Execution completed");
}
Run Code Online (Sandbox Code Playgroud)
现在根据定义,当调用Work()方法时,它将等待SlowTask()方法在打印"执行完成"之前完成其执行.但我怀疑即使没有使用await和async关键字,那么Work()方法也会等待SlowTask()方法在打印"执行完成"之前完成执行,因为它将逐行执行.
有很多关于await和async的解释,现在可以在C#中找到; 但似乎没有解释为什么这......
using (XmlReader reader = XmlReader.Create(stream, settings))
{
while (await reader.ReadAsync())
{
// Do something
}
}
Run Code Online (Sandbox Code Playgroud)
比这更好......
using (XmlReader reader = XmlReader.Create(stream, settings))
{
while (reader.Read())
{
// Do something
}
}
Run Code Online (Sandbox Code Playgroud)
换句话说,如果您在等待... Async方法做其事情时,无意做其他事情,为什么要使用它?如果同步方法可用,为什么不使用它?
此Async示例取自:https://docs.microsoft.com/en-us/dotnet/api/system.xml.xmlreader?view = networkwork4.7.1
编辑:在阅读了被认为是重复的问题和答案后,他们没有帮助.我特意询问了与被调用的异步方法在同一行上等待的简单情况.我的问题已在下面得到解答.谢谢你们!
我正在使用uwp应用程序中的RenderTargetBitmap将UIelement转换为流。但是我不知道如何让线程等到流的转换。我尝试使用建议,但是找不到合适的解决方案。任何帮助深表感谢。
{
for (int count = 0; count <= Textpage.Children.Count; count++)
{
if (Textpage.Children[count] is Viewbox)
{
CustomView customView = (Textpage.Children[count] as Viewbox).Child as CustomView;
UIElement frameworkElement = customView as UIElement;
(Textpage.Children[count] as Viewbox).Child = null;
var element = (PdfDocumentPanel.Children[count] as Border).Child as Canvas;
Textpage.Children.Remove((Textpage.Children[count] as Viewbox));
SaveCustomAnnotation(frameworkElement, pageIndex, documentToBeSaved);
}
}
}
Stream savedStream = new MemoryStream();
}
internal async void SaveCustomAnnotation(UIElement element, int pageIndex, PdfLoadedDocument loadedDocument)
{
Type pageChild = null;
IRandomAccessStream randomStream;
randomStream = await Task.Run(() => …Run Code Online (Sandbox Code Playgroud) 我正在尝试创建一个使用 .Net Core Framework 运行异步函数的 Web 服务。
这个函数必须在它自己的线程中执行并返回一个新值,该值将被发送给正确的调用者。该服务必须在我的异步函数运行时侦听其他调用,并为每个调用启动一个新线程。线程完成后,我希望将响应发送给原始调用者。我试过这种方式:
在我的控制器中,我有:
[Route("api/orchestration")]
public class OrchController : Controller
{
[HttpGet("{value}")]
public void RunOrchestration(int value)
{
Program.GetResultAsync(value); // Notice that no value is returned but it respects the asynchronicity and the multithreading as asked
}
}
Run Code Online (Sandbox Code Playgroud)
在我的主要课程中,我有:
public async static Task<string> GetResultAsync(int i)
{
return await OrchestrationAsync(i);
}
public static Task<string> OrchestrationAsync(int i)
{
return Task.Run<string>(() => { return r = Orchestration(i); });
}
public static string Orchestration(Object i)
{
// This function is …Run Code Online (Sandbox Code Playgroud)