C# 从 for 循环更新进度

C# Updating progress from for loop

我有这个问题,无论我尝试什么方法,我都无法在 for 循环的每次迭代中更新变量,以便我可以在循环的百分比中向用户显示当前状态。这是我的方法:

void startOperation()
    {
        Console.WriteLine("Beginning start operations..." + searchText);
        Console.WriteLine("Opening Workbook: " + filePath);

        // create excel-instance:
        Microsoft.Office.Interop.Excel.Application xlApp = new Microsoft.Office.Interop.Excel.Application();
        // open the concrete file:
        Workbook xlWorkbook = xlApp.Workbooks.Open(@filePath);
        //MessageBox.Show(filePath);
        // select worksheet. NOT zero-based!!:
        _Worksheet xlWorksheet = xlWorkbook.Sheets[1];
        //MessageBox.Show(Convert.ToString(xlWorksheet.Name));
        Microsoft.Office.Interop.Excel.Range range;

        int numRows = xlWorksheet.UsedRange.Rows.Count;


        currRow = 1;
        numRowsDeleted = 0;
        nullCounter = 0;
        decimal percentageComp;

        //Parallel.For(1, numRows, new ParallelOptions { MaxDegreeOfParallelism = 1 }, i =>
        for (int i = 1; i < numRows; i++)
        {
            percentageComp = currRow / numRows * 100;
            Console.Clear();
            Console.WriteLine("Number of Rows: " + numRows);
            Console.WriteLine("Checking Row #: " + currRow);
            Console.WriteLine("Number of Rows Deleted: " + numRowsDeleted);
            Console.WriteLine("Percentage Complete: " + percentageComp);

            if (i > 1)
            {
                i -= 1;
            }

            //Create Worksheet Range
            range = (Microsoft.Office.Interop.Excel.Range)xlWorksheet.Cells[i, 2];

            //MessageBox.Show(cellValue);

            if (nullCounter == nullCount) //was 5, decreased to 3, then changed to nullCount variable
            {
                closeOperation(xlApp, xlWorkbook);
                break;
            }

            if (Convert.ToString(range.Value) != null)  //if (cellValue != null || cellValue != "")
            {
                cellValue = Convert.ToString(range.Value);
                nullCounter = 0;

                if (cellValue.Contains(searchText))
                {
                    //MessageBox.Show("Found a row that needs deleting!");
                    //range.Rows[i].Delete(XlDeleteShiftDirection.xlShiftUp);
                    //Thread.Sleep(5);
                    xlWorksheet.Rows[i].Delete(XlDeleteShiftDirection.xlShiftUp);
                    //((Range)xlWorksheet.Rows[i]).Delete(XlDeleteShiftDirection.xlShiftUp);
                    numRowsDeleted++;
                    //nullCounter = 0;
                    i--;
                    currRow++;
                }
                else
                {
                    currRow++;
                    //percentageComp = currRow / numRows * 100;

                    //nullCounter = 0;
                }
            }
            else
            {
                //MessageBox.Show("Null cell value");
                nullCounter++;
            }
            i++;
        }

        //);
    }

无论我尝试什么,我的控制台输出都只显示 0。我很困惑我在这里可能做错了什么,所以感谢您的帮助。感谢您的宝贵时间!

currRow是整数,numRows也是。 curRow 小于 numRows

整数除法结果为整数。 40 / 100 小于一。唯一小于一的整数值是零。对于任何整数 x 和 ``y,其中 x 小于 yx / y 小于 1 —— 将截断为零。

一旦你得到零,你可以将零乘以任何你喜欢的数字——它仍然是零。

所以不要使用整数除法。将它们投射到 decimal

percentageComp = ((decimal)currRow / (decimal)numRows) * 100;

评论中的人发现了一堆其他错误,也看看那些。