Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/304.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/wpf/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用while循环C#WPF时不显示控件_C#_Wpf - Fatal编程技术网

使用while循环C#WPF时不显示控件

使用while循环C#WPF时不显示控件,c#,wpf,C#,Wpf,我的代码是: double screenHeight = SystemParameters.WorkArea.Height; double screenWidth = SystemParameters.WorkArea.Width; waitingGrid.Height = screenHeight; waitingGrid.Width = screenWidth; waitingGrid.Visibility = Visibility.Visible; waitingText.Visibili

我的代码是:

double screenHeight = SystemParameters.WorkArea.Height;
double screenWidth = SystemParameters.WorkArea.Width;
waitingGrid.Height = screenHeight;
waitingGrid.Width = screenWidth;
waitingGrid.Visibility = Visibility.Visible;
waitingText.Visibility = Visibility.Visible;
---------
---------
while (!is_approved)
{
     //statements checking whether user got approved and sets is_approved to true
}
问题是当while循环被注释时,网格和文本会显示出来。但是,当允许执行while循环时,控件只会被卡住,不会显示,然后应用程序转到不响应(通过检查任务管理器)


为什么会这样?

这是因为通常应用程序的主线程位于处理用户输入等的循环中,但事件处理代码不是立即返回以便事件循环可以处理更多输入,而是进入一个
,而
循环,直到
的值被批准后才会返回。由于UI线程在您的循环中很忙,因此只有当您有一些后台线程对其进行修改时,
才能得到批准。猜测您没有,
被批准
将永远不会更改,
while
循环将永远不会退出,无限期地阻塞事件循环并导致您看到的挂起。

您调试了while循环吗?它看起来像是一个while循环,需要很长时间才能运行,甚至可能是一个无限循环。@Dzyann当其他用户(管理员)脱机时也是如此。他未获得批准。您正在用户界面线程上运行一个冗长的操作。因此,用户界面不能做它通常做的事情,而它通常做的事情之一就是更新用户界面并保持应用程序的响应性。@是否有任何解决方法?不要在事件处理程序中放置无限
while
循环。我需要通过单击按钮来执行此操作。我能做什么?不,绝对不行。假设您希望通过某种用户交互进行更改,那么就要找出与之对应的UI事件,并通过对UI进行任何更改来处理该事件。