如何将Backgroundworker与返回值集成

时间:2017-04-21 15:24:50

标签: c# multithreading backgroundworker

我想从测量板查询数据。我用2循环来检查循环,因此x轴和y轴。不幸的是,如果我只使用一个线程,会有很大的延迟。 我没有使用backgroundthreads的经验,我尝试了很多但是我遇到了很多错误。

我想用backgroundworker查询所有y轴值,但我不知道如何使用返回值的backgroundworker。你知道如何整合背景工作者吗?

这是我的代码剪辑:

var bufferMatrix = new object();
            var bufferDigital = new object();


            do
            {
                int read = TheDevice.Transfer();
                if ((read & (int)Transfer.TransferDataReady) != 0)
                {
                    if (Matrix != null)
                    {
                        if (Digital != null)
                        {
                            int quantcount = Matrix.GetQuantCount();
                            int quantCountDigi = Digital.GetQuantCount();
                            // Console.WriteLine("{0} quants downloaded from matrix component", Quantcount);
                            if ((quantcount & quantCountDigi) > 0)
                            {
                                Matrix.CreateCompatibleBuffer(ref bufferMatrix);
                                Digital.CreateCompatibleBuffer(ref bufferDigital);

                                Digital.GetQuants(0, quantCountDigi, bufferDigital, 0);
                                byte[] quantDigi = (byte[])bufferDigital;

                                byte[,] quant = new byte[Matrix.GetSizeX(), Matrix.GetSizeY()];
                                Matrix.GetQuant(quantcount - 1, ref bufferMatrix); //Bekommt Quants und schreibt auf buffer
                                quant = (byte[,])bufferMatrix;



                                double force = 0;



                                double res = Matrix.GetRes() * Matrix.GetResX() * Matrix.GetResY() * 0.01;
                                for (int x = 0; x < Matrix.GetSizeX(); x++)  //X-Achse Schleife 
                                {
                                    Console.WriteLine();
                                    Console.WriteLine();
                                    Console.WriteLine();


                                    for (int y = 0; y < Matrix.GetSizeY(); y++) //Y-Achse Schleife 
                                    {
                                        // var digi = quantDigi[x, y];                                           
                                        force += quant[x, y];
                                        if (force > 0)
                                        {
                                            quantListY.Add(force);
                                            Console.Write(x+" , "+y+" => "+force*res);

                                        }
                                    }

                                }

                            }
                        }
                    }
                }
                else
                {
                    System.Threading.Thread.Sleep(100);
                }

            } while (!Console.KeyAvailable || Console.ReadKey().Key != ConsoleKey.S);

0 个答案:

没有答案