How to make button run operation in the background [duplicate] - c#

This question already has answers here:
Await/async doesn't work as expected
(3 answers)
Closed 5 years ago.
I have a program that checks what is the user reaction time based on speed he clicks on the button when it changes to green. I would like the function that is working after clicking the button to run in the background.
I though by using Task it will make it asynchronously but it seems to not be the case. I understand that there is an await and probably I should make it somehow return to place it was called but I have a problem finding the solution.
My code so far looks like this
public partial class Reaction : Form
{
Stopwatch timer;
bool start = false;
public Reaction()
{
InitializeComponent();
button1.Text = "START";
button1.BackColor = Color.Red;
}
public async Task Test()
{
if (start)
{
timer.Stop();
TimeSpan timespan = timer.Elapsed;
string timeString = String.Format("{0:00}:{1:00}:{2:00}", timespan.Minutes, timespan.Seconds, timespan.Milliseconds / 10);
MessageBox.Show(timeString);
button1.BackColor = Color.Red;
button1.Text = "START";
start = false;
}
else
{
Random rnd = new Random();
int number = rnd.Next(1, 10000);
System.Threading.Thread.Sleep(3000 + number);
timer = Stopwatch.StartNew();
button1.BackColor = Color.Green;
button1.Text = "CLICK";
start = true;
}
}
private async void button1_Click(object sender, EventArgs e)
{
button1.BackColor = Color.Red;
button1.Text = "Dont click";
await Test();
}
}

Based on the comments I edited the post to reflect more closely what you were asking for. Remove the async and await in the button's click event and replace the System.Threading.Thread.Sleep(... with await Task.Delay(... This way you will not have to use BeginInvoke on button1 and the only thing on a separate thread will be the Task.Delay(...
The UI will become responsive during the Task.Delay(... and then resume where it left off when the delay is finished.
public partial class Reaction : Form
{
Stopwatch timer;
bool start = false;
public Reaction()
{
...
}
public async Task Test()
{
if (start)
{
...
}
else
{
Random rnd = new Random();
int number = rnd.Next(1, 10000);
// Replace Thread.Sleep with Task.Delay
await Task.Delay(3000 + number);
timer = Stopwatch.StartNew();
button1.BackColor = Color.Green;
button1.Text = "CLICK";
start = true;
}
}
// Make button1_Click not async and remove await
private void button1_Click(object sender, EventArgs e)
{
button1.BackColor = Color.Red;
button1.Text = "Dont click";
Test();
}
}

Related

Why is the text not showing up on screen?

I'm trying to create a countdown where the text displays, "GAME STARTS IN: " and using a for loop and Thread.Sleep a variable counts down from three. I started by using the designer to create the "game starts in:" part, but after the variable wouldn't show up I moved it to code. Now nothing shows up. This is what I have now in my timer method:
if (!countedDown)
DoCountdown();
Countdown.Hide();
And then in a DoCountdown method:
this.Countdown.BackColor = System.Drawing.Color.Transparent;
this.Countdown.ForeColor = System.Drawing.Color.White;
this.Countdown.Location = new System.Drawing.Point(360, 17);
this.Countdown.Name = "Countdown";
this.Countdown.Font = new System.Drawing.Font("Segoe UI", 12F,
System.Drawing.FontStyle.Regular,
System.Drawing.GraphicsUnit.Point);
this.Countdown.Size = new System.Drawing.Size(185, 24);
this.Countdown.TabIndex = 6;
countedDown = true;
for (int i = 3; i > 0; i--)
{
Countdown.Text = "GAME STARTS IN: " + i;
System.Threading.Thread.Sleep(1000);
}
I put a breakpoint at System.Threading.Thread.Sleep(100) and everything seemed normal. Countdown.Text was equal to "GAME STARTS IN: 3". After trying to integrate the solutions the text doesn't show up. Here is some more context in my code:
This is from my start screen form
private void QuitGame(object sender, EventArgs e)
{
Application.Exit();
}
private void StartMultiplayerGame(object sender, EventArgs e)
{
GameScreen startGame = new GameScreen();
startGame.Show();
Hide();
}
Try something like below. A button is used to start the timer and set the initial values.
int count = 3;
private void button2_Click(object sender, EventArgs e) {
timer1.Interval = 1000;
count = 3;
label1.Text = "GAME STARTS IN: " + count;
timer1.Start();
}
private void timer1_Tick(object sender, EventArgs e) {
count--;
if (count != 0) {
label1.Text = "GAME STARTS IN: " + count;
}
else {
timer1.Stop();
label1.Text = "GAME STARTED";
MessageBox.Show(" -> GO");
}
}
Edit per OP comments.
Try the code like this in the start screen form...
private void StartMultiplayerGame(object sender, EventArgs e) {
count = 3;
label1.Text = "GAME STARTS IN: " + count;
timer1.Start();
}
Then change the timer code to...
private void timer1_Tick(object sender, EventArgs e) {
count--;
if (count != 0) {
label1.Text = "GAME STARTS IN: " + count;;
}
else {
timer1.Stop();
label1.Text = "Game Started";
GameScreen startGame = new GameScreen();
startGame.Show();
this.Hide();
}
}
loop blocking the main thread to refresh UI so the required scenario can be archived by moving the loop to a separate method
void doCountDown()
{
for (int i = 10; i > 0; i--)
{
setCountDownText( "GAME STARTS IN: " + i);
System.Threading.Thread.Sleep(1000);
}
}
creating anew thread that start this method
new System.Threading.Thread(new System.Threading.ThreadStart(doCountDown)).Start();
and because of the need to update UI in another thread and to make it safe separate the setText in a separate method that update based on checking required to invoke property this will make it work in all cases
void setCountDownText(string txtValue)
{
if (Countdown.InvokeRequired)
{
Action safeWrite = delegate { setCountDownText(txtValue); };
Countdown.Invoke(safeWrite);
}
else
Countdown.Text = txtValue;
}
The "modern" way to do this is using async/await.
For example, launching the DoCountdown() from a button handler could look like this:
async void testBtn_Click(object sender, EventArgs e)
{
await DoCountdown();
}
async Task DoCountdown()
{
// <Initialisation of Countdown elided for brevity>
for (int i = 3; i > 0; i--)
{
Countdown.Text = "GAME STARTS IN: " + i;
await Task.Delay(1000);
}
}
However, whatever calls DoCountdown() will need to be declared as async, and so on up the call tree.
Note that the only acceptable place to have async void rather than async Task as a return type for an async method is where the method is an event handler such as the button handler in the example above.

How to change C# Windows Forms label color for 1 sec and reset the label color? [duplicate]

I have a form that displays queue of messages and number this messages can be changed. Really I want to blink label (queue length) when the number of messages were increased to improve form usability.
Should I implement custom control and use additional thread or timer to change color of label? Has anybody implemented so functionality? What is the best solution (less resources and less performance degradation) to implement so behaviour?
SOLUTION:
Form's component with timer that can restrict number of animations per second and implement fade out effect to external control background color.
The following is blinking using async and await
private async void Blink(){
while (true){
await Task.Delay(500);
label1.BackColor = label1.BackColor == Color.Red ? Color.Green : Color.Red;
}
}
I know this is a really old post, but anyone looking for something a little more versatile than the Boolean solutions posted may get some use out of the following:
using System.Diagnostics;
using System.Threading.Tasks;
private async void SoftBlink(Control ctrl, Color c1, Color c2, short CycleTime_ms, bool BkClr)
{
var sw = new Stopwatch(); sw.Start();
short halfCycle = (short)Math.Round(CycleTime_ms * 0.5);
while (true)
{
await Task.Delay(1);
var n = sw.ElapsedMilliseconds % CycleTime_ms;
var per = (double)Math.Abs(n - halfCycle) / halfCycle;
var red = (short)Math.Round((c2.R - c1.R) * per) + c1.R;
var grn = (short)Math.Round((c2.G - c1.G) * per) + c1.G;
var blw = (short)Math.Round((c2.B - c1.B) * per) + c1.B;
var clr = Color.FromArgb(red, grn, blw);
if (BkClr) ctrl.BackColor = clr; else ctrl.ForeColor = clr;
}
}
Which you can call like such:
SoftBlink(lblWarning, Color.FromArgb(30, 30, 30), Color.Red,2000,false);
SoftBlink(lblSoftBlink, Color.FromArgb(30, 30, 30), Color.Green, 2000,true);
Timer timer = new Timer();
timer.Interval = 500;
timer.Enabled = false;
timer.Start();
if( messagesNum > oldMessagesNum)
timer.Tick += new EventHandler( timer_Tick );
else
timer.Tick -= timer_Tick;
void timer_Tick( object sender, EventArgs e )
{
if(messageLabel.BackColor == Color.Black)
messageLabel.BackColor = Color.Red;
else
messageLabel.BackColor = Color.Black;
}
Here is a pretty simple implementation that would work inside your form. You could also create a custom control with the same code and just throw the Timer.Start() into a method for that control.
Create your own UserControl for this, one that inherits from Label instead of from Control directly. Add a StartBlinking method, in which you start a Timer object whose tick event alters the style of the label (changing the BackgroundColor and ForegroundColor properties each time to create the blink effect).
You could also add a StopBlinking method to turn it off, or you could have your Timer stop itself after 5 seconds, perhaps.
You can create a custom component and events to start blinking --which I think is a good solution. The Blinking you can implement with a timer.
Can you use an animated .gif instead (perhaps as the background of the number)? it would make it look like old school web pages, but it might work.
You can use Timer class here.
Here what I have implemented.
Label color blinking on Button_click Event.
//click event on the button to change the color of the label
public void buttonColor_Click(object sender, EventArgs e)
{
Timer timer = new Timer();
timer.Interval = 500;// Timer with 500 milliseconds
timer.Enabled = false;
timer.Start();
timer.Tick += new EventHandler(timer_Tick);
}
void timer_Tick(object sender, EventArgs e)
{
//label text changes from 'Not Connected' to 'Verifying'
if (labelFirst.BackColor == Color.Red)
{
labelFirst.BackColor = Color.Green;
labelFirst.Text = "Verifying";
}
//label text changes from 'Verifying' to 'Connected'
else if (labelFirst.BackColor == Color.Green)
{
labelFirst.BackColor = Color.Green;
labelFirst.Text = "Connected";
}
//initial Condition (will execute)
else
{
labelFirst.BackColor = Color.Red;
labelFirst.Text = "Not Connected";
}
}
this is how i ended up doing it
public partial class MemberDisplay : Form
{
public string Input;
public int MASS_STOP = 1;
public MemberDisplay(string msg)
{
InitializeComponent();
State_Entry();
Input = msg;
}
public void State_Entry()
{
this.SpecialFocus.Select();
this.lbl_TimerTest.Hide();
}
private async void RunBlinkyTest(string msg)
{
while (msg == "GO" && (MASS_STOP == 0))
{
await Task.Delay(500);
lbl_TimerTest.ForeColor =
lbl_TimerTest.ForeColor == Color.Red ?
Color.Black :
Color.Red;
if (msg == "STOP" && (MASS_STOP == 1)) { return; }
}
}
private void btn_TimeTest_Click(object sender, EventArgs e)
{
if (btn_TimeTest.Text == "GO")
{
this.lbl_TimerTest.Show();
MASS_STOP = 0;
RunBlinkyTest("GO");
btn_TimeTest.Text = "STOP";
return;
}
if (btn_TimeTest.Text == "STOP")
{
MASS_STOP = 1;
RunBlinkyTest("STOP");
this.lbl_TimerTest.ForeColor = Color.Black;
this.lbl_TimerTest.Hide();
btn_TimeTest.Text = "GO";
return;
}
}
}

DispatcherTimer and Button c# conflict

I'm very new to WP8 dev and c#. I'm trying to make a loop that counts up by n on an interval. I want to press a button to increment n.
Here is my code right now:
namespace Petsounds {
public partial class MainPage : PhoneApplicationPage {
float clicks = 0;
float clickers = 0;
float clickerBuyers = 0;
float clickerCost = 5;
float clickerBuyerCost = 500;
long savedTime = DateTime.Now.Ticks / TimeSpan.TicksPerSecond;
bool buyClickerButtonFlag = false;
bool clickButtonFlag = false;
// Constructor
public MainPage() {
InitializeComponent();
//
DispatcherTimer t = new DispatcherTimer();
t.Interval = TimeSpan.FromMilliseconds(10);
t.Tick += (s, e) => startLoop();
t.Start();
}
private void clickButtonOnClick(object sender, RoutedEventArgs e) {
clickButtonFlag = true;
System.Diagnostics.Debug.WriteLine("clicked!" + clicks);
}
private void buyClickerButtonOnClick(object sender, RoutedEventArgs e) {
buyClickerButtonFlag = true;
}
private void startLoop() {
if (true) {
long nowTime = savedTime;
long timePassed = nowTime - savedTime;
//user input
if (clickButtonFlag) {
clickButtonFlag = false;
clicks++;
System.Diagnostics.Debug.WriteLine("clicked!" + clicks);
}
if (buyClickerButtonFlag) {
buyClickerButtonFlag = false;
if (clicks > clickerCost) {
clickers++;
clicks -= clickerCost;
clickerCost *= 1.6F;
}
System.Diagnostics.Debug.WriteLine("clicker bought!" + clickers);
}
//update vars
if (timePassed > TimeSpan.TicksPerSecond) {
savedTime = nowTime;
nowTime = DateTime.Now.Ticks / TimeSpan.TicksPerSecond;
clicks += clickers;
}
//update display
clickCount.Text = clicks.ToString();
buyClickerButtonCost.Text = "Cossst " + clickerCost.ToString();
}
}
}
}
My button's are inconsistent, and if I remove the thread, the buttons are responsive (but of course the counter doesn't work.)
EDIT:
I've changed
DispatcherTimer t = new DispatcherTimer();
t.Interval = TimeSpan.FromMilliseconds(10);
t.Tick += (s, e) => startLoop();
t.Start();
to
Timer myTimer = new Timer(startLoop);
myTimer.Change(1000, 10);
And now get an error:
A first chance exception of type 'System.UnauthorizedAccessException' occurred in System.Windows.ni.dll
on line
clickCount.Text = clicks.ToString();
First of all... you will quickly find that 10ms is not really 10ms... It might not even be that close... If you did 1000ms... that would be expected to be more accurate.
Also, a DispatcherTimer is going to queue up a function call to the GUI thread each interval... which means you are flooding the GUI thread with startLoop() calls. This doesn't give the thread much time to update anything else... like your buttons.
There is a different approach you might want to consider.
If your task is to increment a numeric value when a user touches a button (and have the numbers increase at a steady pace) consider using the RepeatButton.
RepeatButton: Represents a control that raises its Click event repeatedly from the time it is pressed until it is released.
XAML
<!--
Delay: The time, in milliseconds, the RepeatButton waits
when it is pressed before it starts repeating the click action.
Interval: The time, in milliseconds, between repetitions
of the click action, as soon as repeating starts.
-->
<RepeatButton Content='Buy'
Interval='50' Delay='100'
Click='RepeatButton_Click' />
Code
private float buyCounter = 0;
private void RepeatButton_Click(object sender, RoutedEventArgs e) {
buyCounter += 1;
buyClickerButtonCost.Text = buyCounter.ToString();
}
It's like #Andrew said - DispatcherTimer works on UI thread and with so small intervall you are blocking it.
If you want such a small interval you can use Timer on different Thread:
public MainPage()
{
InitializeComponent();
System.Threading.Timer myTimer = new Timer(MyTimerCallback);
myTimer.Change(1000, 10);
}
private static int value = 0;
private static void MyTimerCallback(object state)
{
value++;
}
But you must remember that you use it on different Thread - this Timer has no access to your UI elements (buttons and so on).
EDIT
You convinced me to check it:
static float clicks = 0;
static float clickers = 0;
static float clickerCost = 5;
static long savedTime = DateTime.Now.Ticks / TimeSpan.TicksPerSecond;
static bool buyClickerButtonFlag = false;
static bool clickButtonFlag = false;
public MainPage()
{
InitializeComponent();
first.Click += ShowCounter;
DispatcherTimer t = new DispatcherTimer();
t.Interval = TimeSpan.FromSeconds(5);
t.Tick += ShowCounter;
t.Start();
System.Threading.Timer myTimer = new Timer(MyTimerCallback);
myTimer.Change(10, 10);
}
private void ShowCounter(object sender, EventArgs e)
{
textBlck.Text = clicks.ToString();
}
private static void MyTimerCallback(object state)
{
clicks++; // added to check running
if (true)
{
long nowTime = savedTime;
long timePassed = nowTime - savedTime;
//user input
if (clickButtonFlag)
{
clickButtonFlag = false;
clicks++;
System.Diagnostics.Debug.WriteLine("clicked!" + clicks);
}
if (buyClickerButtonFlag)
{
buyClickerButtonFlag = false;
if (clicks > clickerCost)
{
clickers++;
clicks -= clickerCost;
clickerCost *= 1.6F;
}
System.Diagnostics.Debug.WriteLine("clicker bought!" + clickers);
}
//update vars
if (timePassed > TimeSpan.TicksPerSecond)
{
savedTime = nowTime;
nowTime = DateTime.Now.Ticks / TimeSpan.TicksPerSecond;
clicks += clickers;
}
}
}
I tested it on the device and buttons works.
On the other hand - what's the point of putting a method that waits for a flag buton click, when you can put the job easily to button click event. Let it happen when user clicked button - don't check buton state all the time.

Timer + display messages + Different time intervals

How to display some messages on a C# form application with different time intervals with buttons?
Something like:
private void button1_Click(object sender, EventArgs e)
{
label1.Text = "string1";
[wait 3 seconds]
label1.Text = "string2";
[wait 5 sec]
label1.text="string 3";
[end]
}
You can create a new thread, change the label text, sleep that thread and so on so forth:
using System.Threading;
// Somewhere in your Form, for example in Form_Load event:
new Thread(new ThreadStart(delegate {
var d = new setLabelTextDelegate(setLabelText);
label1.Invoke(d, new object[] { "string 1" });
Thread.Sleep(3000); // sleep 3 seconds
label1.Invoke(d, new object[] { "string 2" });
Thread.Sleep(5000); // sleep 5 seconds
label1.Invoke(d, new object[] { "string 3" });
})).Start();
private delegate void setLabelTextDelegate(string text);
private void setLabelText(string text)
{
this.label1.Text = text;
}
Use a Timer with an interval of X milliseconds and update the UI each Timer Tick. Keep track of the number of Timer Ticks received so you'll know which string to use. After each update has been processed stop the Timer.
Other solutions posted might be wiser, but this one is pretty simple.
Form1 contains a simple Label called Label1 and a button called Button1
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
t.Interval = 100;
t.Tick += new EventHandler(t_Tick);
t.Start();
}
Timer t = new Timer();
int counter = 0;
private void Button1_Click(object sender, EventArgs e)
{
t.Start();
}
void t_Tick(object sender, EventArgs e)
{
try
{
t.Enabled = false; //Disable timer so we don't start t_Tick when t_Tick is still runnnig
if (counter == 0)
{
label1.Text = "string1";
t.Interval = 3000;
}
if (counter == 1)
{
label1.Text = "string2";
t.Interval = 5000;
}
if (counter == 2)
{
label1.Text = "string3";
t.Stop(); //Stop timer
}
else
{
t.Enabled = true; //Resume timer
}
counter++;
}
catch (Exception ex)
{
MessageBox.Show("Never throw exception from timer..." + ex.Message);
}
}
To do exactly as your pseudo code suggest simply use Thread.Sleep() in place of your [wait x] lines. Note that it will likely make UI unresponsive for the duration of waiting.
Alternatively you can create a thread that does the same thing but doesn't block the UI thread. The only issue there is that you have to define delegate in UI thread otherwise it wont work.
private void button1_Click(object sender, EventArgs e)
{
ThreadPool.QueueUserWorkItem(delegate()
{
label1.Text = "string1";
Thread.Sleep(3000);
label1.Text = "string2";
Thread.Sleep(5000);
//etc...
});
}
private void button1_Click(object sender, EventArgs e)
{
label1.Text = "string1";
System.Threading.Thread.Sleep(3*1000);
label1.Text = "string2";
System.Threading.Thread.Sleep(5*1000);
label1.text="string 3";
}
Thank you sooo much.
Oh i love stakoverflow.com
I did something like this.:)
Thank you all gentle mens :)
Please comment how did i do? I actually wanted it all happen in click of button.
public partial class mainForm : Form {
public mainForm()
Timer myTimer = new Timer();
private void button1_Click(object sender, EventArgs e)
{
myTimer.Tick += new EventHandler(myTimer_Tick);
myTimer.Interval = 2000;
myTimer.Start();
}
int counter=0;
void myTimer_Tick(object sender, EventArgs e)
{
if (counter == 0)
{
label4.Text = "string1";
myTimer.Interval = 2000;
}
if (counter == 1)
{
label4.Text = "string2";
myTimer.Interval = 2000;
}
if (counter == 2)
{
label4.Text = "string3";
myTimer.Stop();
}
else
{
myTimer.Enabled = true;
}
counter++;
} }
I made it all work with valuable examples you all provided . i put it all together and got it working as i wanted.
Once again Thank you all :)

How to implement a blinking label on a form

I have a form that displays queue of messages and number this messages can be changed. Really I want to blink label (queue length) when the number of messages were increased to improve form usability.
Should I implement custom control and use additional thread or timer to change color of label? Has anybody implemented so functionality? What is the best solution (less resources and less performance degradation) to implement so behaviour?
SOLUTION:
Form's component with timer that can restrict number of animations per second and implement fade out effect to external control background color.
The following is blinking using async and await
private async void Blink(){
while (true){
await Task.Delay(500);
label1.BackColor = label1.BackColor == Color.Red ? Color.Green : Color.Red;
}
}
I know this is a really old post, but anyone looking for something a little more versatile than the Boolean solutions posted may get some use out of the following:
using System.Diagnostics;
using System.Threading.Tasks;
private async void SoftBlink(Control ctrl, Color c1, Color c2, short CycleTime_ms, bool BkClr)
{
var sw = new Stopwatch(); sw.Start();
short halfCycle = (short)Math.Round(CycleTime_ms * 0.5);
while (true)
{
await Task.Delay(1);
var n = sw.ElapsedMilliseconds % CycleTime_ms;
var per = (double)Math.Abs(n - halfCycle) / halfCycle;
var red = (short)Math.Round((c2.R - c1.R) * per) + c1.R;
var grn = (short)Math.Round((c2.G - c1.G) * per) + c1.G;
var blw = (short)Math.Round((c2.B - c1.B) * per) + c1.B;
var clr = Color.FromArgb(red, grn, blw);
if (BkClr) ctrl.BackColor = clr; else ctrl.ForeColor = clr;
}
}
Which you can call like such:
SoftBlink(lblWarning, Color.FromArgb(30, 30, 30), Color.Red,2000,false);
SoftBlink(lblSoftBlink, Color.FromArgb(30, 30, 30), Color.Green, 2000,true);
Timer timer = new Timer();
timer.Interval = 500;
timer.Enabled = false;
timer.Start();
if( messagesNum > oldMessagesNum)
timer.Tick += new EventHandler( timer_Tick );
else
timer.Tick -= timer_Tick;
void timer_Tick( object sender, EventArgs e )
{
if(messageLabel.BackColor == Color.Black)
messageLabel.BackColor = Color.Red;
else
messageLabel.BackColor = Color.Black;
}
Here is a pretty simple implementation that would work inside your form. You could also create a custom control with the same code and just throw the Timer.Start() into a method for that control.
Create your own UserControl for this, one that inherits from Label instead of from Control directly. Add a StartBlinking method, in which you start a Timer object whose tick event alters the style of the label (changing the BackgroundColor and ForegroundColor properties each time to create the blink effect).
You could also add a StopBlinking method to turn it off, or you could have your Timer stop itself after 5 seconds, perhaps.
You can create a custom component and events to start blinking --which I think is a good solution. The Blinking you can implement with a timer.
Can you use an animated .gif instead (perhaps as the background of the number)? it would make it look like old school web pages, but it might work.
You can use Timer class here.
Here what I have implemented.
Label color blinking on Button_click Event.
//click event on the button to change the color of the label
public void buttonColor_Click(object sender, EventArgs e)
{
Timer timer = new Timer();
timer.Interval = 500;// Timer with 500 milliseconds
timer.Enabled = false;
timer.Start();
timer.Tick += new EventHandler(timer_Tick);
}
void timer_Tick(object sender, EventArgs e)
{
//label text changes from 'Not Connected' to 'Verifying'
if (labelFirst.BackColor == Color.Red)
{
labelFirst.BackColor = Color.Green;
labelFirst.Text = "Verifying";
}
//label text changes from 'Verifying' to 'Connected'
else if (labelFirst.BackColor == Color.Green)
{
labelFirst.BackColor = Color.Green;
labelFirst.Text = "Connected";
}
//initial Condition (will execute)
else
{
labelFirst.BackColor = Color.Red;
labelFirst.Text = "Not Connected";
}
}
this is how i ended up doing it
public partial class MemberDisplay : Form
{
public string Input;
public int MASS_STOP = 1;
public MemberDisplay(string msg)
{
InitializeComponent();
State_Entry();
Input = msg;
}
public void State_Entry()
{
this.SpecialFocus.Select();
this.lbl_TimerTest.Hide();
}
private async void RunBlinkyTest(string msg)
{
while (msg == "GO" && (MASS_STOP == 0))
{
await Task.Delay(500);
lbl_TimerTest.ForeColor =
lbl_TimerTest.ForeColor == Color.Red ?
Color.Black :
Color.Red;
if (msg == "STOP" && (MASS_STOP == 1)) { return; }
}
}
private void btn_TimeTest_Click(object sender, EventArgs e)
{
if (btn_TimeTest.Text == "GO")
{
this.lbl_TimerTest.Show();
MASS_STOP = 0;
RunBlinkyTest("GO");
btn_TimeTest.Text = "STOP";
return;
}
if (btn_TimeTest.Text == "STOP")
{
MASS_STOP = 1;
RunBlinkyTest("STOP");
this.lbl_TimerTest.ForeColor = Color.Black;
this.lbl_TimerTest.Hide();
btn_TimeTest.Text = "GO";
return;
}
}
}

Categories

Resources