Problem IN use Of Application.DoEvents() [modified]
-
I use This CODE fOR my Program but i have to Two Click To use Button Whats IS The Problem? My Code Is Hear
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;namespace WindowsFormsApplication13
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}private void label2\_Click(object sender, EventArgs e) { } Boolean bB2ShouldStop = false; Boolean bB1ShouldStop = false; private void button1\_Click(object sender, EventArgs e) { if (label1.Text == "500") { for (int i = 1; (i <= 500) && !bB1ShouldStop; i++) { label1.Text = i.ToString(); System.Threading.Thread.Sleep(10); Application.DoEvents(); if (label2.Text != "500") { bB2ShouldStop = true; label2.Text = (Int16.Parse(label2.Text) + 1).ToString(); } } bB1ShouldStop = false; } } private void button2\_Click(object sender, EventArgs e) { if (label2.Text == "500") { for (int i = 1; (i <= 500) && !bB2ShouldStop; i++) { label2.Text = i.ToString(); System.Threading.Thread.Sleep(10); Application.DoEvents(); if (label1.Text != "500") { bB1ShouldStop = true; label1.Text = (Int16.Parse(label1.Text) + 1).ToString(); } } bB2ShouldStop = false; } } }
}
modified on Tuesday, April 20, 2010 3:48 PM
-
I use This CODE fOR my Program but i have to Two Click To use Button Whats IS The Problem? My Code Is Hear
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;namespace WindowsFormsApplication13
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}private void label2\_Click(object sender, EventArgs e) { } Boolean bB2ShouldStop = false; Boolean bB1ShouldStop = false; private void button1\_Click(object sender, EventArgs e) { if (label1.Text == "500") { for (int i = 1; (i <= 500) && !bB1ShouldStop; i++) { label1.Text = i.ToString(); System.Threading.Thread.Sleep(10); Application.DoEvents(); if (label2.Text != "500") { bB2ShouldStop = true; label2.Text = (Int16.Parse(label2.Text) + 1).ToString(); } } bB1ShouldStop = false; } } private void button2\_Click(object sender, EventArgs e) { if (label2.Text == "500") { for (int i = 1; (i <= 500) && !bB2ShouldStop; i++) { label2.Text = i.ToString(); System.Threading.Thread.Sleep(10); Application.DoEvents(); if (label1.Text != "500") { bB1ShouldStop = true; label1.Text = (Int16.Parse(label1.Text) + 1).ToString(); } } bB2ShouldStop = false; } } }
}
modified on Tuesday, April 20, 2010 3:48 PM
I haven't read all that as it isn't formatted properly; you should use PRE tags instead of CODE tags (you could still edit the message). Anyway, Application.DoEvents() seldom is the solution, it often causes just more problems. And you should not write event handlers that last longer than a few dozen milliseconds, so Thread.Sleep() is not acceptable there at all. :)
Luc Pattyn [Forum Guidelines] [Why QA sucks] [My Articles]
Prolific encyclopedia fixture proof-reader browser patron addict?
We all depend on the beast below.
-
I use This CODE fOR my Program but i have to Two Click To use Button Whats IS The Problem? My Code Is Hear
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;namespace WindowsFormsApplication13
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}private void label2\_Click(object sender, EventArgs e) { } Boolean bB2ShouldStop = false; Boolean bB1ShouldStop = false; private void button1\_Click(object sender, EventArgs e) { if (label1.Text == "500") { for (int i = 1; (i <= 500) && !bB1ShouldStop; i++) { label1.Text = i.ToString(); System.Threading.Thread.Sleep(10); Application.DoEvents(); if (label2.Text != "500") { bB2ShouldStop = true; label2.Text = (Int16.Parse(label2.Text) + 1).ToString(); } } bB1ShouldStop = false; } } private void button2\_Click(object sender, EventArgs e) { if (label2.Text == "500") { for (int i = 1; (i <= 500) && !bB2ShouldStop; i++) { label2.Text = i.ToString(); System.Threading.Thread.Sleep(10); Application.DoEvents(); if (label1.Text != "500") { bB1ShouldStop = true; label1.Text = (Int16.Parse(label1.Text) + 1).ToString(); } } bB2ShouldStop = false; } } }
}
modified on Tuesday, April 20, 2010 3:48 PM
-
The only thing that could prevent the loop executing is the shouldstop variable. I can see that bb1ShouldStop will be true when the button2_Click handler returns. In your code it is reset to false by running the button1 handler. Alan.
What Is The Problem?
-
What Is The Problem?
we don't have a problem, you do. So you should tell us what the intent is and what you are getting that you don't like, so someone could explain the difference. FYI: this is a terrible abuse of Application.DoEvents() as it makes both handlers reentrant, i.e. you could click a button and execute its handler while the same or the other button's handler is still running. X|
Luc Pattyn [Forum Guidelines] [Why QA sucks] [My Articles]
Prolific encyclopedia fixture proof-reader browser patron addict?
We all depend on the beast below.