简体   繁体   English

如何使用 Async 和 Await 技术编写函数?

[英]How to write a function using Async and Await techniques?

I want to run a void function that writes to a textblock in WPF我想在 WPF 中运行一个写入文本块的 void 函数

private async Task<int> TASK_ClickthroughBuild(string server, int frame, int click)
{
    if (server == "value")
    {
        CreativeCode.Inlines.Add(openScript + newLine);
        CreativeCode.Inlines.Add("var " + click + " = '%%CLICK_URL_ESC%%%%DEST_URL%%';" + newLine);
        CreativeCode.Inlines.Add("var " + frame + " = document.getElementById('" + frame + "');" + newLine);
        CreativeCode.Inlines.Add(frame + ".href = " + click + ";" + newLine);
        CreativeCode.Inlines.Add(closeScript);
        PASSBACKframeINT = frame;              
    }
    return PASSBACKframeINT;
}

The above function returns an integer value and writes code to a textblock.上述函数返回一个整数值并将代码写入文本块。

This is the second function.这是第二个功能。

private async Task clickElementBuild()
{
    CreativeCode.Inlines.Add("<a href='#' id='" + PASSBACKframeINT + "' target='_blank' class='" + PASSBACKwrapINT + "' >" + newLine);
    CreativeCode.Inlines.Add("<div class='" + PASSBACKblockINT + "' id='" + overlayINT + "'></div>" + newLine);
}

The second function, the code needs to write the textblock code ABOVE the first function, but depends on the returned value of first function to write properly.第二个函数,代码需要写在第一个函数ABOVE的textblock代码,但是要依赖第一个函数的返回值才能写好。

So I need to write this in an Asynchronous format.所以我需要以异步格式编写它。 Can I have pointers or a better way of doing this?我可以有指针或更好的方法吗?

Thanks谢谢

I'm assuming you're using .NET 4.5 or else you'd have chosen a Threading-Solution.我假设您使用的是 .NET 4.5,否则您会选择线程解决方案。

Calling an async Function is basically telling the Mainthread to pass everything to a Backgroundthread.调用异步函数基本上是告诉主线程将所有内容传递给后台线程。

As asynchronous functions are called there's the "risk of soft data consistency".当调用异步函数时,存在“软数据一致性风险”。 Like in your case or in simpler words the analogy of Forms.show() and Forms.showDialog().就像在你的情况下或更简单的话,Forms.show() 和 Forms.showDialog() 的类比。

There are several ways to bypass this.有几种方法可以绕过这个。 Threading solves this with JOIN() OR WAITALL() and async with System.Threading.Thread.Sleep(x) or some prefer Application.DoEvents()线程使用 JOIN() 或 WAITALL() 解决这个问题,并与 System.Threading.Thread.Sleep(x) 或一些更喜欢 Application.DoEvents() 异步

You can place an Await-For-Background-Task-Clause above your Code which is in risk of "soft data consistency" or place the code it in the Mainthread.您可以将 Await-For-Background-Task-Clause 置于存在“软数据一致性”风险的代码上方,或将其置于主线程中。

If you're new to async functions.如果您不熟悉异步函数。 I recommend this tutorial to use it.我推荐这个教程来使用它。

https://visualstudiomagazine.com/articles/2014/06/01/how-to-simplify-asynchronous-programming.aspx https://visualstudiomagazine.com/articles/2014/06/01/how-to-simplify-asynchronous-programming.aspx

Sorted.排序。 Basically, return the value and used Async to await the returned value in the first place.基本上,首先返回值并使用 Async 等待返回值。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM