[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1516
  • Last Modified:

Timer wait for 3 seconds C#

I am with this code below in what I call the class commport.cs that accesses the serial port.
I have 6 devices: ID01, ID02, ID03, ID04, ID05 and ID06.
The code sends the command id01 and accesses the device.
I need to receive the data and compare it really is the id01 device and the data are correct.
 
We give a time for the system receives the data and work through them.

Then send the command id02.
We give a time for the system receives the data and work through them.

And so in all six devices.

I am using for loop below to stop for a while,

for (int i = 0; i <= 10000; i + +)
                 for (int j = 0, j <= 10, j + +)
                 {}

however I would like to take this loop and make a stop with the timr_ID06 for 3 seconds and after that continue.

Or send the next command after received and compared the data.

I thought of using the System.Threading.Thread.Sleep (3000);
But i donĀ“t know what happens if we put this code.

private void button26_Click(object sender, EventArgs e)
        {
            #region TRABALHA ID01
            timr_ID06.Enabled = true;
            Thread.Sleep(250);
            string s_ID01_c = "id01";
            CommPort com = CommPort.Instance;
            s_ID01_c = ConvertEscapeSequences(s_ID01_c); ;
            com.Send(s_ID01_c);
            #endregion

            for (int i = 0; i <= 10000; i++)
                for (int j = 0; j <= 10; j++)
                { }

            #region TRABALHA ID02
            //timr_ID06.Enabled = true;
            Thread.Sleep(250);
            string s_ID02_c = "id02";
            //CommPort com = CommPort.Instance;
            s_ID02_c = ConvertEscapeSequences(s_ID02_c); ;
            com.Send(s_ID02_c);
            #endregion

            for (int i = 0; i <= 10000; i++)
                for (int j = 0; j <= 10; j++)
                { }

        }

Open in new window

0
ocaccy
Asked:
ocaccy
  • 2
  • 2
1 Solution
 
TommySzalapskiCommented:
Yes, Thread.Sleep(3000) will just wait for 3 seconds and is just the same as your for loops except you know exactly how long it will sleep and it won't waste processor time.
0
 
Mike TomlinsonMiddle School Assistant TeacherCommented:
So you want to place a Sleep() directly after your Send()?

    com.Send(s_ID01_c);
    System.Threading.Thread.Sleep(3000);

This is in your main UI thread since it is in the button handler.

Your app will not be able to process messages during those three seconds so you won't be able to receive a response from your device during that period.

If that is acceptable, and you can handle having your main UI unresponsive for three seconds then go for it.

Other than using a Timer (which breaks up the code), you can use a loop with DoEvents() in place of your sleep:

    com.Send(s_ID01_c);
    DateTime dt = DateTime.Now.AddSeconds(3);
    while (dt.Subtract(DateTime.Now).TotalMilliseconds > 0)
    {
        System.Threading.Thread.Sleep(50); // SMALL sleep to keep CPU usage down
        Application.DoEvents(); // pump the message queue to process events
    }

This would delay for three seconds while allowing your application to still receive a response from the device.
0
 
TommySzalapskiCommented:
Clever
0
 
ocaccyAuthor Commented:
Very good, we are going well.

Idle mn, I asked a question concerning the events for you.
You saw it?

The title is:

A quick "fix" is place "Application.DoEvents();"

Best Regards,
ocaccy
0
 
Mike TomlinsonMiddle School Assistant TeacherCommented:
I'm not sure exactly what you're asking in that question...

At any rate, you have two basic options to handle these types of scenarios (without using more complicated threading, which is sometimes not possible depending the communication method):
(1) Use a looping structure and DoEvents(), which allows you to control code flow in mostly one place.
(2) Use a Timer control and the Tick() event.  This requires you to use static members or class level members to control the flow of the loop since you would need to maintain the "state" between ticks.
0

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

  • 2
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now