C#单线程代码断路器不工作
我是C#编程新手,我一直在遵循Ringler C#完整指南,因为我需要演示一个简单的代码转换器,锁定UI,因为当单击任何其他按钮时,它只是单线程的。我遵循了书中的教程,但当我单击“开始”时,它只输入代码,UI完全正常工作。谁能告诉我代码中缺少了什么?多谢各位C#单线程代码断路器不工作,c#,single-threaded,C#,Single Threaded,我是C#编程新手,我一直在遵循Ringler C#完整指南,因为我需要演示一个简单的代码转换器,锁定UI,因为当单击任何其他按钮时,它只是单线程的。我遵循了书中的教程,但当我单击“开始”时,它只输入代码,UI完全正常工作。谁能告诉我代码中缺少了什么?多谢各位 public partial class MainWindow : Window { // The simulated code to be broken private string Code; // The l
public partial class MainWindow : Window
{
// The simulated code to be broken
private string Code;
// The list of Labels of the characters to be broken.
private List<TextBlock> OutputCharLabels;
public MainWindow()
{
InitializeComponent();
// Generating a random code to be broken
SimulateCodeGeneration();
// Create a new list of Label controls that show the characters of the code being broken.
OutputCharLabels = new List<TextBlock>(4);
// Add the Label controls to the List
OutputCharLabels.Add(txtOutput1);
OutputCharLabels.Add(txtOutput2);
OutputCharLabels.Add(txtOutput3);
OutputCharLabels.Add(txtOutput4);
//Hide the Stop button initially.
btnStop.IsEnabled = false;
// Hide the demo form and show the CodeBreaker
showCodeBreaker();
}
private void SimulateCodeGeneration()
{
// A Random number generator.
Random loRandom = new Random();
// The char position being generated
int i;
Code = "";
for (i = 0; i <= 4; i++)
{
// Generate a Random Unicode char for each of the 4 positions
Code += (char)(loRandom.Next(65535));
}
}
private void setDemoVisibility(System.Windows.Visibility pbValue)
{
// Change the visibility of the controls related to the demo game.
btnGameOver.Visibility = pbValue;
}
private void setCodeBreakerVisibility(System.Windows.Visibility pbValue)
{
// Change the visibility of the controls related to the CodeBreaking procedure.
txtCodeBreaker.Visibility = pbValue;
txtNumber1.Visibility = pbValue;
txtNumber2.Visibility = pbValue;
txtNumber3.Visibility = pbValue;
txtNumber4.Visibility = pbValue;
txtOutput1.Visibility = pbValue;
txtOutput2.Visibility = pbValue;
txtOutput3.Visibility = pbValue;
txtOutput4.Visibility = pbValue;
btnStart.Visibility = pbValue;
btnHide.Visibility = pbValue;
btnStop.Visibility = pbValue;
}
private void showDemo()
{ // Hide all the controls related to the code
// breaking procedure.
setCodeBreakerVisibility(System.Windows.Visibility.Hidden);
// Change the window title
this.Title = "Demo Form";
// Make the Demo form visable
setDemoVisibility(System.Windows.Visibility.Visible);
}
private void showCodeBreaker()
{
// Hide all the controls related to Demo form
setDemoVisibility(System.Windows.Visibility.Hidden);
// Change the window title
this.Title = "CodeBreaker Application";
// Make the code breaker controls visible
setCodeBreakerVisibility(System.Windows.Visibility.Visible);
}
private bool checkCodeChar(char pcChar, int piCharNumber)
{
// Returns a bool value indicating whether the piCharNumber position of the code is the pcChar received.
return (Code[piCharNumber] == pcChar);
}
private void btnGameOver_Click(object sender, RoutedEventArgs e)
{
// Hide the CodeBreaker and show the demo form
showDemo();
}
private void btnStart_Click(object sender, RoutedEventArgs e)
{
// This code will break the simulated code.
// This variable will hold a number to iterate from 1 to 65,535 - Unicode character set.
int i;
// This variable will hold a number to iterate from 0 to 3 (the characters positions in the code to be broken).
int liCharNumber;
// This variable will hold a char generated from the number in i
char lcChar;
// This variable will hold the current Label control that shows the char position being decoded.
TextBlock loOutputCharCurrentLabel;
for (liCharNumber = 0; liCharNumber < 4; liCharNumber++)
{
loOutputCharCurrentLabel = OutputCharLabels[liCharNumber];
// This loop will run 65,536 times
for (i = 0; i <= 65535; i++)
{
// myChar holds a Unicode char
lcChar = (char)(i);
loOutputCharCurrentLabel.Text = lcChar.ToString();
if (checkCodeChar(lcChar, liCharNumber))
{
// The code position was found
break;
}
}
}
}`
公共部分类主窗口:窗口
{
//要破解的模拟代码
私有字符串码;
//要打断的字符的标签列表。
私有列表输出字符标签;
公共主窗口()
{
初始化组件();
//生成要中断的随机码
模拟退化();
//创建一个新的标签控件列表,其中显示要中断的代码的字符。
OutputCharLabels=新列表(4);
//将标签控件添加到列表中
OutputCharLabels.Add(txtOutput1);
OutputCharLabels.Add(txtOutput2);
OutputCharLabels.Add(txtOutput3);
OutputCharLabels.Add(txtOutput4);
//最初隐藏停止按钮。
btnStop.IsEnabled=false;
//隐藏演示窗体并显示代码生成器
showCodeBreaker();
}
私人空间
{
//随机数发生器。
Random loRandom=新的Random();
//正在生成的字符位置
int i;
代码=”;
对于(i=0;i)调试代码并跟踪开始按钮代码时会发生什么?只要checkCodeChar()
返回true,内部循环就会中断。几乎可以肯定,它不会重复64K次(尽管有注释),我打赌整个方法返回的速度很快,你看不到UI被锁定。用Thread.Sleep(10000)替换整个按钮处理程序代码
您会看到它被锁定!非常感谢您的回复,这正是发生的事情,它返回得如此之快,以至于我无法演示它被锁定。我需要能够启动代码中断,然后通过点击隐藏按钮锁定程序,所以我无法替换整个按钮处理程序?