我愿意报告密集任务 (I/O) 的进度。我一直在阅读所有方法、界面、backGroundWorker、进度,但没有人包含任何类型的附加信息,例如任务的当前阶段。所以我写了这个方法,如果你能给我任何反馈、建议或评论,我会很高兴。我在一个 winforms 4.0 项目中工作。
这是任务
class _ThreadTest
{
public static void ReporteProgreso(out int p, out string stage)
{
p = 0;
stage = "";
for (int i = 0; i < 10; i++)
{
System.Threading.Thread.Sleep(500);
p++;
switch (p)
{
case 0: stage = "init"; break;
case 1: stage = "Loading Data"; break;
case 2: stage = "Parsing Data"; break;
case 3: stage = "Checking"; break;
case 4: stage = "Inserting n Records"; break;
case 5: stage = "10%"; break;
case 6: stage = "50%"; break;
case 7: stage = "70%"; break;
case 8: stage = "100%"; break;
case 9: stage = "finish"; break;
default:
break;
}
}
}
}
这就是我所说的
static void Main()
{
//Reportar progreson con OUT
int p = 0;
string stage = "";
_ThreadTest t = new _ThreadTest();
Thread oThread = new Thread( () => _ThreadTest.ReporteProgreso(out p,out stage) ) ;
oThread.Start();
int tmp = 0;
while (oThread.IsAlive)
{
if (tmp != p)
{
Console.WriteLine(stage);
tmp = p;
}
}
Console.ReadKey();
}
}
是否有任何有趣的测试,更好的方法来实现这一目标?由于这种方法对我有用,因此在某些类型的环境中使用可能不安全。