睡眠X秒,并在每个秒更新标签 [英] Sleep for X sec and update label on each Sec
问题描述
你好,
我有一个调用方法(Connect())的backgrounworker线程.该过程完成后,将进入RunCompleted,在此处检查其connectedToServer与否.不,我叫一个方法(CallToReconnect()).在这里,我必须等待20秒,每秒钟更新状态栏,最后调用一个启动后台线程的方法.为此:
Hello,
I have a backgrounworker thread that calls a method (Connect()). When that process is completed it comes to RunCompleted where I check it its connectedToServer or not. I not, I call a method (CallToReconnect()). Over here, I got to wait for 20 secs updating the status bar on each sec and finally call a method that starts the background thread. For this :
private void CallToReConnect()
{
Logger.LogInfo("Call To ReConnect");
currentState = RECONNECTING;
int counter = 20;
cancelSplitButton.Visible = true;
// Try again after 20 secs
System.Windows.Threading.DispatcherTimer timer = new System.Windows.Threading.DispatcherTimer();
timer.Tick += delegate
{
statusLabel1.Text = "ReConnect in " + counter.ToString() + " secs";
counter--;
// Thread.Sleep(1000);
if (counter == 0)
timer.Stop();
};
timer.Interval = new TimeSpan(0, 0, 1);
timer.Start();
//Thread.Sleep(20000);
ConnectClicked();
}
此代码启动DispatchTimer,然后也启动ConnectClicked().我想在调用Connectclicked()之前完成该部分,因此我的状态栏可以看到在XX秒内重新连接"-XX-20、19、18等.如果将其替换为Thread.Sleep(20000),则它将等待持续20秒然后它才开始.但是我想在状态栏中显示正在重新连接",所以我选择了这种方式.
我要去哪里错了?如何实现目标?
感谢
This code starts the DispatchTimer and then also starts ConnectClicked(). I want to finish that part before Connectclicked() is called, so my status bar can see "Reconnecting in XX secs" - XX - 20, 19, 18, etc. If I replace that with Thread.Sleep(20000) then it waits for 20 secs & then only it starts. But I want to show the Reconnecting in status bar so I opted this way.
Where am I going wrong ? How to accomplish the goal ?
Thanks
推荐答案
将对ConnectClicked的调用移至计时器事件处理程序中:
Move the call to ConnectClicked into the timer event handler:
private void CallToReConnect()
{
Logger.LogInfo("Call To ReConnect");
currentState = RECONNECTING;
int counter = 20;
cancelSplitButton.Visible = true;
// Try again after 20 secs
System.Windows.Threading.DispatcherTimer timer = new System.Windows.Threading.DispatcherTimer();
timer.Tick += delegate
{
statusLabel1.Text = "ReConnect in " + counter.ToString() + " secs";
counter--;
if (counter == 0) {
timer.Stop();
ConnectClicked(); // might need to Invoke it?
}
};
timer.Interval = new TimeSpan(0, 0, 1);
timer.Start();
}
我不确定在哪个线程中调度计时器事件,如果要在UI线程中运行ConnectClicked,则可能必须使用Invoke.
I''m not sure which thread timer events are dispatched in, you may have to use Invoke if you want ConnectClicked to be run in the UI thread.
同时使用线程和计时器时间没有意义.使用Thread.Sleep
.这就是您所需要的.
另外,请务必避免使用计时器.在我过去的答案中进行了解释:
C#中的计时器线程 [
Using thread and timer at the same time makes no sense. UseThread.Sleep
. This is all you need.
Also, avoid using timer by all means. In explained it in my past answer:
Timer Threading in C#[^].
—SA
SA,然后使用Thread.Sleep,如何根据需要更新状态栏中的文本?我无法按预期处理该部分,因此尝试使用计时器.
您是否有避免计时器的想法,请使用Thread.Sleep并每秒更新一次文本?
SA, then by using Thread.Sleep, how do I update the text in statusbar as wanted ? I am not able to handle that part as expected hence tried with timer.
Do you have any idea to avoid timer, use Thread.Sleep & update the text every sec ?
这篇关于睡眠X秒,并在每个秒更新标签的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!