WPF UI 在 while 循环期间保持冻结
WPF UI keeps freezing during while loop
在这段代码中,我的 UI 冻结并且没有像我希望的那样更新,尽管控制台写入行工作完美所以我确定它正在按照我想要的方式进行循环
while (true)
{
MyMethod();
}
void MyMethod() {
Console.WriteLine(DateTime.Now);
TimeSpan duration = SetDate - DateTime.Now;
int days = duration.Days + 1;
string strDays = days.ToString();
string LeftorAgo = "";
if (strDays[0] == '-')
{
LeftorAgo = "ago";
}
else
{
LeftorAgo = "left";
}
this.Dispatcher.Invoke(() =>
{
ShowDate.Text = $"{strDays.TrimStart('-')}\n days {LeftorAgo}";
ShowSubject.Text = Subject;
});
System.Threading.Thread.Sleep(5000);
}
在 Darkonekt 的帮助下编辑我使用他的定时器技术解决了这个问题并且它完美地工作谢谢!
下面的代码
private System.Windows.Threading.DispatcherTimer remainTimer = new System.Windows.Threading.DispatcherTimer();
InitializeComponent();
remainTimer.Tick += new EventHandler(MyMethod);
remainTimer.Interval = TimeSpan.FromSeconds(5);
remainTimer.Start();
void MyMethod(object sender, EventArgs e) {
...
感谢您的帮助
使用 DispatcherTimer 而不是 while 循环:
private DispatcherTimer remainTimer = new DispatcherTimer();
public MainWindow()
{
InitializeComponent();
remainTimer.Tick += MyMethod;
remainTimer.Interval = TimeSpan.FromSeconds(5);
remainTimer.Start();
}
private void MyMethod(object sender, EventArgs e)
{
Console.WriteLine(DateTime.Now);
TimeSpan duration = SetDate - DateTime.Now;
int days = duration.Days + 1;
string strDays = days.ToString();
string LeftorAgo = "";
if (strDays[0] == '-')
{
LeftorAgo = "ago";
}
else
{
LeftorAgo = "left";
}
ShowDate.Text = $"{strDays.TrimStart('-')}\n days {LeftorAgo}";
ShowSubject.Text = Subject;
}
在这段代码中,我的 UI 冻结并且没有像我希望的那样更新,尽管控制台写入行工作完美所以我确定它正在按照我想要的方式进行循环
while (true)
{
MyMethod();
}
void MyMethod() {
Console.WriteLine(DateTime.Now);
TimeSpan duration = SetDate - DateTime.Now;
int days = duration.Days + 1;
string strDays = days.ToString();
string LeftorAgo = "";
if (strDays[0] == '-')
{
LeftorAgo = "ago";
}
else
{
LeftorAgo = "left";
}
this.Dispatcher.Invoke(() =>
{
ShowDate.Text = $"{strDays.TrimStart('-')}\n days {LeftorAgo}";
ShowSubject.Text = Subject;
});
System.Threading.Thread.Sleep(5000);
}
在 Darkonekt 的帮助下编辑我使用他的定时器技术解决了这个问题并且它完美地工作谢谢! 下面的代码
private System.Windows.Threading.DispatcherTimer remainTimer = new System.Windows.Threading.DispatcherTimer();
InitializeComponent();
remainTimer.Tick += new EventHandler(MyMethod);
remainTimer.Interval = TimeSpan.FromSeconds(5);
remainTimer.Start();
void MyMethod(object sender, EventArgs e) {
...
感谢您的帮助
使用 DispatcherTimer 而不是 while 循环:
private DispatcherTimer remainTimer = new DispatcherTimer();
public MainWindow()
{
InitializeComponent();
remainTimer.Tick += MyMethod;
remainTimer.Interval = TimeSpan.FromSeconds(5);
remainTimer.Start();
}
private void MyMethod(object sender, EventArgs e)
{
Console.WriteLine(DateTime.Now);
TimeSpan duration = SetDate - DateTime.Now;
int days = duration.Days + 1;
string strDays = days.ToString();
string LeftorAgo = "";
if (strDays[0] == '-')
{
LeftorAgo = "ago";
}
else
{
LeftorAgo = "left";
}
ShowDate.Text = $"{strDays.TrimStart('-')}\n days {LeftorAgo}";
ShowSubject.Text = Subject;
}