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. loop break on KeyDown [modified]

loop break on KeyDown [modified]

Scheduled Pinned Locked Moved C#
graphicshelpquestion
4 Posts 4 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.
  • E Offline
    E Offline
    erfi
    wrote on last edited by
    #1

    please can someboby solve this problem for ever?! this is making me crazy. i googled but nothing was found. there is a "for loop" in my program and i want to break the loop on KeyDown event. my code is like this :

    private bool EXIT;
    private void Form1_KeyDown(object sender, KeyEventArgs e)
    {
    if (e.KeyCode == Keys.Escape)
    {
    EXIT = true;
    }
    }
    private void Function()
    {
    for (int x = 0; x < Max; x++)
    {
    if(EXIT)
    {
    break;
    }

         // work on image...
         OnPaintBackground(new PaintEventArgs(this.CreateGraphics(),new Rectangle(0,0,Width,eight)));
     }
    

    }
    protected override void OnPaintBackground(PaintEventArgs e)
    {
    e.Graphics.DrawImage( image, 0, 0, Width, Height);
    }

    when the Esc key is pressed nothing happens. what i have to do?

    sometimes 0 can be 1

    H P F 3 Replies Last reply
    0
    • E erfi

      please can someboby solve this problem for ever?! this is making me crazy. i googled but nothing was found. there is a "for loop" in my program and i want to break the loop on KeyDown event. my code is like this :

      private bool EXIT;
      private void Form1_KeyDown(object sender, KeyEventArgs e)
      {
      if (e.KeyCode == Keys.Escape)
      {
      EXIT = true;
      }
      }
      private void Function()
      {
      for (int x = 0; x < Max; x++)
      {
      if(EXIT)
      {
      break;
      }

           // work on image...
           OnPaintBackground(new PaintEventArgs(this.CreateGraphics(),new Rectangle(0,0,Width,eight)));
       }
      

      }
      protected override void OnPaintBackground(PaintEventArgs e)
      {
      e.Graphics.DrawImage( image, 0, 0, Width, Height);
      }

      when the Esc key is pressed nothing happens. what i have to do?

      sometimes 0 can be 1

      H Offline
      H Offline
      Harvey Saayman
      wrote on last edited by
      #2

      i dont see anything wrong there... but thinking about it a little more, the for loop MIGHT be holding up the UI thread, which MIGHT mean that the Form1_KeyDown handler only gets called after the for loop is finished executing.

      Harvey Saayman - South Africa Junior Developer .Net, C#, SQL

      you.suck = (you.passion != Programming)

      1 Reply Last reply
      0
      • E erfi

        please can someboby solve this problem for ever?! this is making me crazy. i googled but nothing was found. there is a "for loop" in my program and i want to break the loop on KeyDown event. my code is like this :

        private bool EXIT;
        private void Form1_KeyDown(object sender, KeyEventArgs e)
        {
        if (e.KeyCode == Keys.Escape)
        {
        EXIT = true;
        }
        }
        private void Function()
        {
        for (int x = 0; x < Max; x++)
        {
        if(EXIT)
        {
        break;
        }

             // work on image...
             OnPaintBackground(new PaintEventArgs(this.CreateGraphics(),new Rectangle(0,0,Width,eight)));
         }
        

        }
        protected override void OnPaintBackground(PaintEventArgs e)
        {
        e.Graphics.DrawImage( image, 0, 0, Width, Height);
        }

        when the Esc key is pressed nothing happens. what i have to do?

        sometimes 0 can be 1

        P Offline
        P Offline
        paas
        wrote on last edited by
        #3

        FWIW, when I feel the need to capture command keys in a program, I like to do so via the ProcessCmdKey override because I've had issues with those captures in other available handlers. So, you might want to take a look at ProcessCmdKey and see if that will help.

        1 Reply Last reply
        0
        • E erfi

          please can someboby solve this problem for ever?! this is making me crazy. i googled but nothing was found. there is a "for loop" in my program and i want to break the loop on KeyDown event. my code is like this :

          private bool EXIT;
          private void Form1_KeyDown(object sender, KeyEventArgs e)
          {
          if (e.KeyCode == Keys.Escape)
          {
          EXIT = true;
          }
          }
          private void Function()
          {
          for (int x = 0; x < Max; x++)
          {
          if(EXIT)
          {
          break;
          }

               // work on image...
               OnPaintBackground(new PaintEventArgs(this.CreateGraphics(),new Rectangle(0,0,Width,eight)));
           }
          

          }
          protected override void OnPaintBackground(PaintEventArgs e)
          {
          e.Graphics.DrawImage( image, 0, 0, Width, Height);
          }

          when the Esc key is pressed nothing happens. what i have to do?

          sometimes 0 can be 1

          F Offline
          F Offline
          Frank Horn
          wrote on last edited by
          #4

          What excactly are you trying to achieve? Is it that once the user has pressed the escape key, you want the application to terminate as soon as possible? Or do you have a longish OnPaint method which you want interrupted when the escape key is pressed? The first would be much easier. The second I think cannot work without polling for the EXIT variable within the OnPaint procedure everywhere it could be interrupted, and even then I'm not sure it could work. A call to Application.DoEvents won't help cause in OnPaint you're right within handling the windows message queue and have to finish the loop anyway. The keyboard action only puts a message to the end of your form's message queue, which will not be processed before you leave the OnPaint method. Either you do some crazy unmanaged subclassing, or you split your painting logic into "atomic" operations which you instigate (via this.BeginInvoke) from a different thread. In this thread you can capture the EXIT flag set by the main thread via the escape key and if it's true stop pouring BeginInvoke(PaintNextPart) delegates to the main thread. You need a little more then, of course, and a firm grip of threading and locking mechanisms, or you'll freeze your form.

          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