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

x
?
Solved

C# - Timer and For loop.

Posted on 2011-10-22
9
Medium Priority
?
888 Views
Last Modified: 2012-08-13
Hi, I'm having trouble making a program to work. This routine must rotate, wait a second, and then run. This in 5 ranges of the loop.
#region ID01
            label_id01.Text = "";
            string s_ID01_c = "id01";
            tmr_loop.Enabled = false;

            for (int loop = 1; loop <= 5;loop++ )
            {
                tmr_loop.Enabled = true;
                tbCount.Text = loop.ToString();
                CommPort com = CommPort.Instance;
                s_ID01_c = ConvertEscapeSequences(s_ID01_c); ;
                com.Send(s_ID01_c);
                //if (label_id01.Text.Contains("02"))
                //{
                gB_id01.Visible = true;
            }
            #endregion

Open in new window

0
Comment
Question by:ocaccy
9 Comments
 
LVL 45

Expert Comment

by:AndyAinscow
ID: 37010697
What bit doesn't work the way you want ?
0
 
LVL 10

Expert Comment

by:Umar Topia
ID: 37010726
You can use Thread.Wait to stop for a specific moment
0
 

Author Comment

by:ocaccy
ID: 37011009
I need to run this loop with 1 second interval.
That compares the label_id01 has the string ("02").
If has "02", out of the loop. If don't have "02" rerun.
Turning up the fifth time and has not, drop an audible alert.
0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
LVL 10

Expert Comment

by:Umar Topia
ID: 37011025
label_id01.Text = "";
            string s_ID01_c = "id01";
            bool bIsSuccess = false;

            for (int loop = 1; loop <= 5;loop++ )
            {
                tmr_loop.Enabled = true;
                tbCount.Text = loop.ToString();
                CommPort com = CommPort.Instance;
                s_ID01_c = ConvertEscapeSequences(s_ID01_c); ;
                com.Send(s_ID01_c);
                if (label_id01.Text.Contains("02"))
                {
bIsSuccess = true;
break;
}
else
{
Thread.Sleep(new TimeSpan(0,0,1));
}
                gB_id01.Visible = true;
            }

if(bIsSuccess)
{
// Do Something
}
0
 

Author Comment

by:ocaccy
ID: 37011201
Thank you, Umartopia.
Apparently it seems to be working.
I run, and return to complete.

Best Regards,
ocaccy
0
 
LVL 86

Expert Comment

by:Mike Tomlinson
ID: 37011383
Is this a WinForms app?

How does the Label value get changed?

What you have is a tight loop with a blocking call, Sleep(), that runs for up to five seconds.  If this loop is running in the main UI thread then pending messages won't get processed during that period and possibly your Label will not get updated (and other events won't get processed either).

A quick "fix" is place "Application.DoEvents();" inside the loop so that messages get processed while the loop still iterates.  A possible solution is to use only a Timer() in place of the loop, or to move that code to a background thread where it is safe to block.
0
 
LVL 10

Accepted Solution

by:
Umar Topia earned 1500 total points
ID: 37011633
If the approach which I have suggested worked for you, then please assign the points to me :)
0
 

Author Comment

by:ocaccy
ID: 37012381
Idle_Mind this is a WinForms app!
I'll open another question and we can continue to talk about "Application.DoEvents ();"
The title is: A quick "fix" is place "Application.DoEvents();" 500 points.
Umartopia, I'm giving you the points.
Very grateful for the attention, I hope your help in my other questions.


0
 

Author Closing Comment

by:ocaccy
ID: 37012388
Thank you,
ocaccy
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
High user turnover can cause old/redundant user data to consume valuable space. UserResourceCleanup was developed to address this by automatically deleting user folders when the user account is deleted.
I've attached the XLSM Excel spreadsheet I used in the video and also text files containing the macros used below. https://filedb.experts-exchange.com/incoming/2017/03_w12/1151775/Permutations.txt https://filedb.experts-exchange.com/incoming/201…
Is your data getting by on basic protection measures? In today’s climate of debilitating malware and ransomware—like WannaCry—that may not be enough. You need to establish more than basics, like a recovery plan that protects both data and endpoints.…
Suggested Courses

830 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question