alejandro carnero
alejandro carnero

Reputation: 1784

exit with escape key in for loop

I have this loop and is working :

  for (int x = 0; x < nombres.Length; x++)
            {
                ValidXX.Text = x.ToString(); ValidXY.Text = nombres.Length.ToString();

                origen = nombres[x];
                cambia = nombres[x];
                pedrito = control.ValidarDocumentoXML(cambia);
                if (pedrito == true)
                { }
                else
                  /*  File.Move (origen , destino );*/
                try
                { }
                catch(IOException iox)
                {  MessageBox.Show(iox.Message); }
                { /* corrupto[x] = cambia; */ MessageBox.Show("malo" + cambia); }
            } 

I want o break this loop with the escape key , i have try with this :

    private void Importar2_KeyDown(object sender, KeyEventArgs e)
    {
        if (e.KeyCode == Keys.Escape) { }   
    }

but i can not write this key_down into the for loop.

Upvotes: 0

Views: 2037

Answers (1)

Dilshod
Dilshod

Reputation: 3311

you could do like this:

    bool escPressed = false;

        ....
        //run your loop in a different thread
        Thread thread = new Thread(new ThreadStart(MyLoop));
        thread.Start();
        ....        


    void MyLoop()
    {
        for (int x = 0; x < nombres.Length; x++)
        {
            if(escPressed) break;
            ValidXX.Text = x.ToString(); ValidXY.Text = nombres.Length.ToString();

            origen = nombres[x];
            cambia = nombres[x];
            pedrito = control.ValidarDocumentoXML(cambia);
            if (pedrito == true)
            { }
            else
              /*  File.Move (origen , destino );*/
            try
            { }
            catch(IOException iox)
            {  MessageBox.Show(iox.Message); }
            { /* corrupto[x] = cambia; */ MessageBox.Show("malo" + cambia); }
        } 
    }

    private void Importar2_KeyDown(object sender, KeyEventArgs e)
    {
        if (e.KeyCode == Keys.Escape) { escPressed = true;}   
    }

Upvotes: 1

Related Questions