在模拟图灵机时无法看到输入运动.. [英] Unable to see the input movement while simulating the turing machine..

查看:93
本文介绍了在模拟图灵机时无法看到输入运动..的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用图灵机模拟器。我在Windows窗体上放置了2个文本框和1个richtextbox。第一个文本框包含说明。第二个文本框包含当前状态,richtextbox包含输入字符串。我正在使用richtextbox的selectionbackcolor属性来突出显示当前的输入字符。我使用了do while循环来运行图灵机,直到它达到最终状态。但问题是我无法看到输入中的运动。这意味着selectionbackcolor从一个输入变为下一个输入。能否请你给我一个解决方案..



以下是while循环代码:



I am trying to do a Turing machine simulator. I have placed 2 textboxes and 1 richtextbox on the windows form. The first text box contains instructions. The second text box contains present state and richtextbox contain input string. I am using selectionbackcolor property of the richtextbox to highlight the present input character. I have used a do while loop to run the turing machine untill it reaches the end state. But the problem I cannnot see the movement in the input. That means selectionbackcolor changing from one input to the next input. Can you please suggest me a solution for this..

Below is the while loop code:

do
           {

               string instruction = string.Empty;
               string nextstate;
               string output;
               string direction;

               string temp;
               temp = CurrentInstruction.Substring(CurrentInstruction.IndexOf('{') + 1);
               temp = temp.Substring(0, temp.IndexOf('}'));
               string[] temp1 = temp.Split(',');
               currentstate = temp1[0];

               output = temp1[1];
               direction = temp1[2];

               StringBuilder sb = new StringBuilder(richTextBox1.Text);
               sb[currentinputindex] = Convert.ToChar(output);
               richTextBox1.Text = sb.ToString();

               if (direction == "R")
               {    //resetting the color for present input
                   richTextBox1.Select(currentinputindex, 1);
                   richTextBox1.SelectionBackColor = System.Drawing.SystemColors.Control;
                   richTextBox1.Focus();
                   //higlisting the next  input
                   currentinputindex = currentinputindex + 1;
                   richTextBox1.Select(currentinputindex, 1);
                   richTextBox1.SelectionBackColor = Color.Blue;
                   richTextBox1.Focus();
                   //richTextBox1.SelectionBullet = true;

               }
               else
               {
                   //resetting the color for present input
                   richTextBox1.Select(currentinputindex, 1);
                   richTextBox1.SelectionBackColor = System.Drawing.SystemColors.Control;
                   richTextBox1.Focus();

                   //higlisting the next  input
                   currentinputindex = currentinputindex - 1;
                   richTextBox1.Select(currentinputindex, 1);
                   richTextBox1.SelectionBackColor = Color.Blue;
                   richTextBox1.Focus();
                   // richTextBox1.SelectionBullet = true;
               }


               txtCurrentState.Text = currentstate;
               string SearchString = "d[" + txtCurrentState.Text + "," +              richTextBox1.Text[currentinputindex] + "]";
               sr = new System.IO.StreamReader(filename);
               string line;
               while ((line = sr.ReadLine()) != null)
               {
                   if (line.Contains(SearchString))
                   {
                       instruction = line;
                   }

               }
               sr.Close();

               CurrentInstruction = instruction;
               txtInstructions.SelectionStart = txtInstructions.Text.IndexOf(instruction);
               txtInstructions.SelectionLength = instruction.Length;
               txtInstructions.Focus();

          }while (currentstate !="h" && currentstate != "H");

推荐答案

你看不出发生了什么的简单原因是它正在忙着解决接下来发生的事情!

你只有一个线程:所以如果你坐在循环中处理任何东西,那么显示元素在循环结束之前不会更新。



基本上,你有两个选择:

1)从循环移动到基于计时器的系统,其中每个通过现有循环的传递都是在计时器滴答上完成的。这是一个令人讨厌的解决方案,你必须非常小心地选择你的计时器间隔,否则你仍然看不到更新或运行得太慢。

2)将你的循环移动到一个单独的线程中,这样两个任务可以并行进行。这在理论上非常简单,因为它只是跟随MSDN上的BackgorundWorker类中的示例。然而,在实践中,它比这要复杂得多,因为你的大部分循环都涉及访问你的控件而且只能在UI线程上完成,所以你必须调用你的大部分代码 - 稍微击败对象,将更新移回UI线程...



我建议你重新检查你的代码,找到一种方法,你可以将处理移动到BackgroundWorker上,用它报告UI线程的更新,以便显示它。这可能是相当多的工作,但从长远来看,这是最好的路线!
The simple reason you don't see what is going on, is that it's busy working out what happens next!
You only have one thread: so if you are sitting in a loop processing anything, then the display elements don't get updated until the loop ends.

Basically, you have two options:
1) Move from a loop to a timer based system, where each pass through your existing loop is done on a timer tick instead. This is a nasty solution, and you will have to pick your timer interval fairly carefully or you will still not see updates or run far too slowly.
2) Move your loop into a separate thread so the two tasks can proceed in parallel. This is pretty simple in theory, since it's just a case of following the example in the BackgorundWorker class on MSDN. In practice however, it's a lot more complex than that since most of your loop involves accessing your controls and that can only be done on the UI thread, so you'd have to Invoke most of your code - defeating the object a bit, by moving the updates back to the UI thread...

I would suggest that you reexamine your code, and find a way that you can move the processing onto a BackgroundWorker, with it reporting updates to the UI thread for it to display. It's probably a fair amount of work, but it is the best route in the long run!


这篇关于在模拟图灵机时无法看到输入运动..的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆