0

I am using Visual Studio C# 2010 on Windows 10.

EDIT: My objective is to create an abort button which allows me to use the UI to cancel a loop.

I'm using an example I found online which is similar to this: Here is the code:

 public void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)
        {

                e.Result = "";
                for (int i = 0; i < 100; i++)
                {

                    System.Threading.Thread.Sleep(50); //do some intense task here.


                    if (backgroundWorker1.CancellationPending)
                    {
                        e.Cancel = true;
                        return;
                    }
                }

        }

(There was a bit of code sharing the date/time but I removed it since it was superfluous to the basic example).

How I'm understanding this code is that, 100 times, the code pauses the main thread and works on "some intense task" for 50ms. Is this the case?

If so, I think I've ran into a problem. I have this code I want to run:

 private void btn_runtest_Click(object sender, EventArgs e)
        {

            // Exterior Platform Loop
                Output.AppendText("Starting Test \n");


            for (int i = 0; i <= 3200; i += 178)
            {

                // Interior Platform Loop
                for (int ii = 0; i <= 6400; ii += 178)
                {
                    comport7_interior.Write("@1N178\r");   //change to have actual length?
                    comport7_interior.Write("@1G\r");
                    Output.AppendText("Interior shift 5 degrees \n");

                    Thread.Sleep(4000);

                    // ***********************************************************************************
                    // Read data from comport2_lightsensor, Parse byte, store Lux value, store angle (i,ii)
                    // ***********************************************************************************
                }

                comport8_exterior.Write("@0N178\r");
                comport8_exterior.Write("@0G\r");
                Output.AppendText("Exterior shift 5 degrees \n");

               Thread.Sleep(4000);

               //flip direction for internal
               if (IsOdd(jjj))
               {
                   //
                   comport7_interior.Write("@1-\r");
                   Output.AppendText("Interior switching direction to counter clockwise \n");
                    }
                    else
                    {
                        comport7_interior.Write("@1+\r");
                        Output.AppendText("Interior switching direction to clockwise \n");
                    }
                    jjj = jjj + 1;

                    // ***********************************************************************************
                    // Read data from compart2_lightsensor, Parse byte, store Lux value, store angle (i,ii)
                    // ***********************************************************************************
                }
                Output.AppendText("Loop Ended");
                // manually reverse mount direction
                // repeat the whole double for loop

        }

This is pretty "fatty" code, so to summarize, it controls two stepper motors, rotates them to follow the desired path, pauses for 4 seconds, and will eventually log data. We could do the exact math but simply estimating we can realize this code will take 1-2 hours. With all of this pausing, it does not seem conducive to being split into 100 little chunks on worked on separately (if my understanding of the previously stated code is correct). Correct me I'm wrong but I don't think this code would fit in the above code.

Does this mean I'm looking at the wrong approach?

Thank you all in advance.

  • I'm not sure what the significance of the first code block is, in relation to the second code block. It's just an empty for loop that sleeps the thread for 50ms every iteration. It is essentially doing nothing for those 50 ms, not anything intense. – pay Jun 21 '16 at 17:36
  • You either don't clearly understand what Thread.Sleep does or your question is not clear enough. – Camilo Terevinto Jun 21 '16 at 17:44
  • See my edit to the main post to see my objective. The solution to my problem may be to use "DoEvents()" . Telling me I'm important does nothing to solve this ignorance. For the sake of learning, could you ELI5 the first codeblock? – Daniel Dyck Jun 21 '16 at 17:51
  • http://stackoverflow.com/questions/5181777/use-of-application-doevents , apparently ShowDialog() is superior. – Daniel Dyck Jun 21 '16 at 17:54
  • @Pay - thank your for the description. Inside that for loop would BE the intense work, that you want to be able to abort from , by using the UI. – Daniel Dyck Jun 21 '16 at 17:56
  • If you want to be able to break out of the loop with a UI interaction, I would simply create a boolean, check it every iteration of the loop, and set it to true on the button click. If it's true, break out of the loop. – pay Jun 21 '16 at 18:47
  • @Pay, I tried that - as long as the loops is running, I can't click anything on the UI. Unless I implemented it wrong. See the top answer here: http://stackoverflow.com/questions/27608357/how-to-stop-a-for-loop-using-a-button – Daniel Dyck Jun 21 '16 at 21:41
  • @Pay , so I guess I just have to jump into multithreading. – Daniel Dyck Jun 21 '16 at 21:50
  • Oh, don't do any work inside UI event handlers (on the UI thread). Put all that work into separate functions and call those in the event handlers, on a different thread. If you want to be able to cancel the operation you should use your own managed thread. – pay Jun 22 '16 at 00:02

0 Answers0