Link to home
Start Free TrialLog in
Avatar of ocaccy
ocaccyFlag for Japan

asked on

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

Avatar of TommySzalapski
TommySzalapski
Flag of United States of America image

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.
ASKER CERTIFIED SOLUTION
Avatar of Mike Tomlinson
Mike Tomlinson
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of ocaccy

ASKER

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
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.