Windows CE 3.0Windows CE 2.11Pocket PC 2002Windows Mobile.NET 1.0Windows 2000Visual C++ 6.0Windows XP移动应用中级开发Visual StudioWindowsC++.NET
CxProgress 类 - 时间、进度和速率估算变得简单





4.00/5 (10投票s)
2003 年 3 月 14 日
2分钟阅读

54044

640
当您从互联网下载文件时,您必须找到传输速率。这个类只需 3 行简单的代码即可完成!
引言
本文解决了一个简单但棘手的问题!时间估算!
您是否曾经创建一个函数来从网上下载文件,但却无法计算传输速率、估算时间……如果您尝试过,您就会收到“除零错误”的致命消息!好吧,这个类可以通过几次函数调用完成许多很棒的事情。
CxProgress
typedef unsigned long u_long; typedef unsigned int u_int; class CXProgress { public: CXProgress(); void SetRange(u_long prgmin, u_long prgmax); const char * FormatLeftTime(); const char * FormatElapsedTime(); u_long ToComeSeconds(); bool StepIt(u_long steps=1); bool SetStep(u_long step=1); u_long RatePerHour(); u_long RatePerMinute(); u_long RatePerSecond(); u_long Rate(u_long milisecs=1000); u_long ElapsedMilisecs(); u_long ElapsedDays(); u_long ElapsedHours(); u_long ElapsedMinutes(); bool IsTimeElapsed(); bool DecreaseMinPos(u_long num); bool EncreaseMinPos(u_long num); bool DecreaseMaxPos(u_long num); bool IncreaseMaxPos(u_long num); u_long ElapsedSeconds(); u_int GetPercentLeft(); u_int GetPercentElapsed(); u_long GetLeftPos(); u_long GetCurrentPos(); bool Can(); u_long LeftDays(); u_long LeftHours(); u_long LeftMinutes(); u_long LeftSeconds(); bool SetCurrentPos(u_long); CXProgress(u_long, u_long); virtual ~CXProgress(); private: // do not modify the variables below u_long startMom,nowMom; // they are private and by modifying them i don`t long startTime,nowTime; // know how class will behave u_long prgMin; u_long prgMax; u_long curPos; u_long numStep; // interneal buffer for functions that return strings char buf[512]; }; /*---------------------------------------------------------*/ /*CXProgress.cpp*/ // XProgress.cpp: implementation of the CXProgress class. // //////////////////////////////////////////////////////////////////////
使用代码
代码使用方法非常简单
void main(void) { CXProgress prg(0,100); /*we create a prg with range 0-100*/ int cur=0; while(cur<100) { cur++; prg.SetCurrentPos(cur); /*we set it`s position to cur*/ Sleep(1000); printf("%s -",prg.FormatElapsedTime()); printf(" %s - Rate: %d\r",prg.FormatLeftTime(),prg.RatePerSecond()); /*run this example and see how it works*/ } }
正如您所见,它非常适合互联网传输,用于获取速率……
文档
如何使用每个函数
void SetRange |
它可以具有介于这两个值之间的值。 |
const char * FormatLeftTime |
返回 00:00:00 格式的剩余时间。 |
const char * FormatElapsedTime |
返回 00:00:00 格式的已用时间。 |
u_long ToComeSeconds |
剩余秒数。 |
bool StepIt |
u_long steps= 1 - 将当前位置增加 steps *step |
bool SetStep |
u_long step=1 - 步长 - 通常为 1 |
u_long RatePerHour |
每小时估计进度速率。 |
u_long RatePerMinute |
每分钟估计进度速率。 |
u_long RatePerSecond |
每秒估计进度速率。 |
u_long Rate |
u_long milisecs =1000 - 小于一秒的间隔的进度速率。 |
u_long ElapsedMilisecs |
已用毫秒数。 |
u_long ElapsedDays |
已用天数。 |
u_long ElapsedHours |
已用小时数。 |
u_long ElapsedMinutes. |
已用毫秒数。 |
bool IsTimeElapsed |
从开始到现在是否已经过了任何时间。 |
bool DecreaseMinPos |
u_long num - 使 minPos =minPos -num |
bool EncreaseMinPos |
u_long num - 使 minPos =minPos +num |
bool DecreaseMaxPos |
u_long num - 使 maxPos =maxPos -num |
bool IncreaseMaxPos |
u_long num - 使 maxPos =maxPos +num |
u_long ElapsedSeconds |
已用秒数。 |
u_int GetPercentLeft |
剩余百分比。 |
u_int GetPercentElapsed |
已完成百分比。 |
u_long GetLeftPos |
返回剩余位置。(maxPos -curPos ) |
u_long GetCurrentPos |
返回当前位置。 |
bool Can |
它是否有任何内部有效时间可以估算。 |
u_long LeftDays |
剩余天数。 |
u_long LeftHours |
剩余小时数。 |
u_long LeftMinutes |
剩余分钟数。 |
u_long LeftSeconds |
剩余秒数。 |
bool SetCurrentPos |
u_long pos - 将当前位置设置为 pos |
历史
- 仅在 2003 年 3 月 15 日上午 6:31 发布(我想)
最后但并非最不重要的一点
如果您不明白任何内容,请查看代码。您不可能错过。您肯定会理解的!:~)
源代码只需看一眼,就能向您凡人的眼睛揭示它最黑暗的秘密。我还可以告诉您我的另一个小秘密。如果源代码的复杂性让您不知所措,您可以使用古老但早已被遗忘的复制粘贴技术。如果您需要更多信息,请查看软件工程师的圣经,被禁止的 MSDN。在其肮脏尘土飞扬的页面中将发现许多智慧之语。
要找出关于我们的存在、人类、宇宙和外星人的真相,请给我发邮件。