Click here to Skip to main content
15,890,557 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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:

C#
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");
Posted
Comments
BillWoodruff 18-Jan-14 2:42am    
Have you tried setting break-points and single-stepping through the code to observe the values of variables ?
Kandepu Rajesh 18-Jan-14 2:45am    
yes...when I step through each step I can see the color changing from one input to another. But when I ran on a whole I cannot see such movement..

1 solution

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!
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900