小编Fro*_*Pro的帖子

C# - 将同步程序转换为异步程序

我目前正在重写我的代码以添加异步执行。但是,我无法适应一组方法。

我看着像几个问题这一个要求,但往往时间之前的问题是使用Task.Run()而不是异步/ AWAIT。

========摘要========

我的程序将用于自动化带有多个阀门的测试台。因此,我编写了open/closeValve()等方法将阀门打开或关闭到某个阈值,增加/减少流量()选择打开或关闭哪个阀门,以及调用 increqse/decreaseFlow()的调节()方法。

就在最近,我在阅读后决定添加异步执行是实时显示有关工作台的数据的好主意。但是我的方法都被认为以同步方式工作,所以我很难适应它们。

========代码======

例如这里是 openValve() 方法

public int openValve(int limit)
    {
        if(_masterFlowrate >= _instructions - 0.1 && _masterFlowrate <= _instructions + 0.1)
        {
            //flowrate ~= instructions => no need to manipulate the valves
            return 1;
        }
        else
        {
            if(valveOpening < limit)
            {
                //flowrate < instructions & valve opened less than the limit
                // => opening valve untill the limit or untill …
Run Code Online (Sandbox Code Playgroud)

c# asynchronous async-await

0
推荐指数
1
解决办法
72
查看次数

标签 统计

async-await ×1

asynchronous ×1

c# ×1