欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

c# 将一个长耗时任务改成task

程序员文章站 2022-11-30 14:57:54
调用 var reuslt = await _stTrendController.InitalrenderSourceandBrush2((int)firstrowgrid.ActualWidth - 27); ......
public task<observablecollection<waterfalldatasource2>> initalrendersourceandbrush2(int width)
        {
            stopwatch w = new stopwatch();
            w.start();
            points3.clear();
            int wavelength = globalstorage.bigwaves.length;
            int allrows = wavelength / width;
            w.stop();
            debug.writeline("w.elapsedmilliseconds:" + w.elapsedmilliseconds);
            debug.writeline("allrows:" + allrows);
            if (allrows > 0)
            {
                parallel.for(0, _pagesize, (lk) =>
                {
                    observablecollection<datapoint3> datapoints = new observablecollection<datapoint3>();
                    for (byte j = 0; j < 150; j++)
                    {
                        for (short i = 0; i < width; i++)
                        {
                            datapoint3 datapoint = new datapoint3() { variablex = i, variabley = j };
                            datapoint.type = getcolorindex2(globalstorage.bigwaves[lk * width + i][j]);
                            datapoints.add(datapoint);
                        }
                    }
                    waterfalldatasource2 waterfalldatasource = new waterfalldatasource2();
                    waterfalldatasource.datapoints = datapoints;
                    waterfalldatasource.brushinfo = brushes;
                    waterfalldatasource.index = lk;
                    waterfalldatasource.width = width;
                    waterfalldatasource.starttime = beatwavedataconversionextension.wavedottodatetime(beatinfocache.beatinfos.elementat(lk * width).r).tostring();
                    points3.add(waterfalldatasource);
                });
                return task.fromresult<observablecollection<waterfalldatasource2>>(points3.orderby(p => p.index).toobservablecollection<waterfalldatasource2>());
            }
            else
            {
                observablecollection<datapoint3> datapoints = new observablecollection<datapoint3>();
                waterfalldatasource2 waterfalldatasource = new waterfalldatasource2();
                for (byte j = 0; j < 150; j++)
                {
                    for (short i = 0; i < wavelength; i++)
                    {
                        short value = globalstorage.bigwaves[i][j];
                        datapoint3 datapoint = new datapoint3() { variablex = i, variabley = j };
                        datapoint.type = getcolorindex2(value);
                        datapoints.add(datapoint);
                    }
                }
                waterfalldatasource.datapoints = datapoints;
                waterfalldatasource.brushinfo = brushes;
                waterfalldatasource.index = 0;
                waterfalldatasource.width = width;
                points3.add(waterfalldatasource);
                return task.fromresult<observablecollection<waterfalldatasource2>>(points3);
            }
        }

调用 

 var reuslt = await _sttrendcontroller.initalrendersourceandbrush2((int)firstrowgrid.actualwidth - 27);