110,538
社区成员
发帖
与我相关
我的任务
分享
System.Timers.Timer timer = new System.Timers.Timer();
timer.Enabled = true;
timer.Elapsed += new System.Timers.ElapsedEventHandler(timer_Elapsed);
timer.AutoReset = true;
timer.Interval = 500;
timer.Enabled = true;
public void timer_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
{
t = t + 1;//得到总的毫秒数
time = GetAllTime(t);//转化成秒表
this.SetText(time);
}
}
delegate void SetTextCallback(string x);
private void SetText(string x)
{
//创建一个委托,用于封装一个方法,在这里是封装了 控制更新控件 的方法
//System.Action invokeAction = new System.Action(ChangeControlsByTimer);
//判断操作控件的线程是否创建控件的线程
//调用方位于创建控件所在的线程以外的线程中,如果在其他线程则对控件进行方法调用时必须调用 Invoke 方法
if (this.label2.InvokeRequired)
{
//与调用线程不同的线程上创建(说明您必须通过 Invoke 方法对控件进行调用)
SetTextCallback d = new SetTextCallback(SetText);
this.label2.Invoke(d, new object[] {x });
}
else
{
label2.Text = x;
}
}
public static string time;
timer.Enabled = true;
for (int j = 0; j < 12; j++)
{
IWorkbook workbook = null;
FileStream fileStream = new FileStream(fileNames[j], FileMode.Open, FileAccess.Read);
if (fileNames[j].IndexOf(".xlsx") > 0)
{
//xlsx数据读入workbook
workbook = new XSSFWorkbook(fileStream);
}
else if (fileNames[j].IndexOf(".xls") > 0)
{
//xls数据读入workbook
workbook = new HSSFWorkbook(fileStream);
}
class Session
{
public bool 是否停止;
}
static async Task testc()
{
var flag = new Session();
var task1 = Task.Run(() => 显示当前时间(flag));
var task2 = Task.Run(() => 你的阻塞任务());
var t = await Task.WhenAny(task1, task2);
Console.WriteLine("task1完成了");
flag.是否停止 = true;
}
static void 显示当前时间(Session s)
{
var startTime = DateTime.Now;
while (!s.是否停止)
{
Console.SetCursorPosition(0, Console.CursorTop);
var t = DateTime.Now.Subtract(startTime);
Console.Write($"{t.Minutes}分{t.Seconds}秒{t.Milliseconds}毫秒 ");
Thread.Sleep(1000);
}
Console.WriteLine("计时停止");
}
static void 你的阻塞任务()
{
Thread.Sleep(6000);
}
var show = Datetime.Now.Substract(startTime).ToString("m分s秒");
这类代码就行了,可以不使用 StopWatch。