在 Thread.Sleep 之后更改标签文本
我的学校正在运行一个登录系统,当学生登录时,它会显示一条欢迎消息,5 秒后,该欢迎消息将被删除,并替换为“请刷学生卡”。我有一个睡眠线程正在运行。我希望在睡眠线程完成后将欢迎标签重置为“请刷身份证”,但它不会这样做。有什么帮助吗?我以正确的方式处理这件事吗?谢谢。
I have a sign in system running at my school, and when a student signs in, it shows a welcome message and after 5 seconds, this welcome message is erased to be replaced by, "Please swipe your student card". I have a sleep thread running. I want the welcome label to reset to say "Please swipe your ID card" after the sleep thread has finished, but it won't do it. Any help? Am I going about this the right way? Thank you.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您没有提供任何详细信息,因此我们需要猜测问题的确切原因。我假设您在 Windows 上使用 C#。
我猜您正在尝试从工作线程执行用户界面代码,这是一个错误。用户界面代码应该在主线程中运行。
虽然可以通过 Control.Invoke 解决这个问题,但更好的方法是避免任何线程,使用计时器等待 5 秒并在主线程中执行 UI 更新。
You haven't given any details so we need to guess at the exact cause of the problem. I'm going to assume that you are using C# on Windows.
I'd guess that you are trying to perform user interface code from a worker thread which is an error. User interface code should run in the main thread.
Whilst this could be solved with
Control.Invoke
a much better approach is to avoid any threads, use a timer for your 5 second wait and perform the UI update in the main thread.哪种语言?
你的代码是什么?
您还可以使用计时器,并在计时器计时时为标签分配新文本。
Which language?
Whats your code ?
You could also use a timer and assign a new text to the label when the timer ticks.