Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. Problem IN use Of Application.DoEvents() [modified]

Problem IN use Of Application.DoEvents() [modified]

Scheduled Pinned Locked Moved C#
csharplinqgraphicshelpquestion
5 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • S Offline
    S Offline
    shahramkeyboard
    wrote on last edited by
    #1

    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

    L A 2 Replies Last reply
    0
    • S shahramkeyboard

      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

      L Offline
      L Offline
      Luc Pattyn
      wrote on last edited by
      #2

      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.


      1 Reply Last reply
      0
      • S shahramkeyboard

        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

        A Offline
        A Offline
        Alan N
        wrote on last edited by
        #3

        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.

        S 1 Reply Last reply
        0
        • A Alan N

          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.

          S Offline
          S Offline
          shahramkeyboard
          wrote on last edited by
          #4

          What Is The Problem?

          L 1 Reply Last reply
          0
          • S shahramkeyboard

            What Is The Problem?

            L Offline
            L Offline
            Luc Pattyn
            wrote on last edited by
            #5

            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.


            1 Reply Last reply
            0
            Reply
            • Reply as topic
            Log in to reply
            • Oldest to Newest
            • Newest to Oldest
            • Most Votes


            • Login

            • Don't have an account? Register

            • Login or register to search.
            • First post
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • World
            • Users
            • Groups