C# 退出,退出循环中的退出键
我有这个循环并且正在工作:C# 退出,退出循环中的退出键,c#,visual-studio-2010,C#,Visual Studio 2010,我有这个循环并且正在工作: for (int x = 0; x < nombres.Length; x++) { ValidXX.Text = x.ToString(); ValidXY.Text = nombres.Length.ToString(); origen = nombres[x]; cambia = nombres[x];
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); }
}
但是我无法将此键写入for循环。您可以这样做:
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;}
}
bool escPressed=false;
....
//在不同的线程中运行循环
线程线程=新线程(新线程开始(MyLoop));
thread.Start();
....
void MyLoop()
{
对于(int x=0;x
在线程中运行逻辑或整个for。在KeyDown事件中,告诉线程停止。
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;}
}