You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

C#控制台进度指示器卡在85.8%停止更新的问题求助

问题解决:控制台进度指示器中途停止更新

问题场景

开发控制台应用时,为耗时操作添加了进度指示器代码,但进度会卡在特定百分比(如...85.8%,当progGranDig=1时)停止更新,后续代码仍能正常执行完毕并输出Done。调整精度参数progGranDig=2时,会卡在...8.58%,且卡在第858次更新。

根本原因

整数溢出。在计算进度百分比的核心表达式中:

int pct = (i * trials * jMax + j * trials + t) * 100 * progGran / (iMax * jMax * trials);

参与计算的变量均为int类型,当计算到后期,i * trials * jMax + j * trials + t接近总步数5*5*100000=2500000,再乘以100*progGran(如progGranDig=1时为1000),得到2500000*1000=2500000000,而int类型的最大值仅为2147483647,超出范围后发生溢出,导致计算出的pct不再大于prvpct,因此进度停止更新。

解决方案

将计算过程中的关键数值强制转换为long类型,避免整数溢出。修改核心计算代码如下:

// 用long存储当前步数和总步数,避免溢出
long currentStep = (long)i * trials * jMax + (long)j * trials + t;
long totalSteps = (long)iMax * jMax * trials;
int pct = (int)(currentStep * 100 * progGran / totalSteps);

或者直接在表达式中强制转换:

int pct = (int)(((long)i * trials * jMax + (long)j * trials + t) * 100 * progGran / ((long)iMax * jMax * trials));

修改后,进度指示器会持续更新至100%,最终正常输出Done。

内容的提问来源于stack exchange,提问作者Ground Thing

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.14 20:02:46