C# 为什么for循环不能正常工作?像这样的代码需要显示name";穆贝;很多时候提到的是健康状况 线程t; 字符串mystring=“”; 私有无效按钮2\u单击(对象发送者,事件参数e) { t=新线程(写入); t、 Start(); 而(t.IsAlive); textBox1.Text=mystring; } 无效写入() { int i; 对于(i=0;i

C# 为什么for循环不能正常工作?像这样的代码需要显示name";穆贝;很多时候提到的是健康状况 线程t; 字符串mystring=“”; 私有无效按钮2\u单击(对象发送者,事件参数e) { t=新线程(写入); t、 Start(); 而(t.IsAlive); textBox1.Text=mystring; } 无效写入() { int i; 对于(i=0;i,c#,C#,您编写for循环的方法是错误的,如果要执行多条指令,则需要将代码放在“{}”中,并删除放在for循环旁边的分号 像这样: Thread t; string mystring = ""; private void button2_Click(object sender, EventArgs e) { t = new Thread(write); t.Start(); while (t.IsAlive) ;

您编写for循环的方法是错误的,如果要执行多条指令,则需要将代码放在“{}”中,并删除放在for循环旁边的分号

像这样:

    Thread t;
    string mystring = "";
    private void button2_Click(object sender, EventArgs e)
    {

        t = new Thread(write);
        t.Start();
        while (t.IsAlive) ;
        textBox1.Text = mystring;

    }

    void write()
    {
        int i;
        for ( i=0; i<1000 ;i++ ) ;
        mystring+= "mubeen" +i.ToString() + "\r\n";


    }
线程t;
字符串mystring=“”;
私有无效按钮2\u单击(对象发送者,事件参数e)
{
t=新线程(写入);
t、 Start();
而(t.IsAlive);
textBox1.Text=mystring;
}
无效写入()
{
int i;

对于(i=0;i而言,将输出设置为mubeen1000的原因在于循环语法

Thread t;
string mystring = "";
private void button2_Click(object sender, EventArgs e)
{

    t = new Thread(write);
    t.Start();
    while (t.IsAlive) ;
    textBox1.Text = mystring;

}

void write()
{
    int i;
    for ( i=0; i<1000 ;i++ ) {
      mystring+= "mubeen" +i.ToString() + "\r\n";
    }


}

问题是在
for
循环的末尾包含了一个错误的分号
。这意味着
for
循环的主体是一个空语句,而不是您实际希望的字符串连接

循环变量最好在循环本身中声明,而不是在循环外部可访问。我怀疑您一开始是这样做的,但在以后无法访问时移动了它。当
I
在循环中声明时,
I
不能被该代码使用这一事实应该是一个线索对你来说,那行代码不在循环中

其次,通常使用busylop是一个非常糟糕的主意(
while(t.IsAlive);
),甚至更糟糕的是在UI线程上对其执行操作,在您的工作完成时阻止UI线程执行任何操作。您应该异步执行工作,让UI线程在此期间处理其他与UI相关的任务,然后在完成后对其进行更新。另外,最好避免在所有用户之间共享字段线程,并且与此活动无关的代码是活动的和可访问的;最好将状态保持在尽可能窄的范围内:

while (t.IsAlive) 
{
    textBox1.Text = mystring;
}
private async void按钮2\u单击(对象发送方,事件参数e)
{
字符串myString=wait Task.Run(()=>Write());
textBox1.Text=myString;
}
私有字符串写入()
{
StringBuilder输出=新的StringBuilder();
对于(int i=0;i<1000;i++)
输出。附录行(“mube”+i);
返回output.ToString();
}

请注意,在循环中连接字符串也不能很好地扩展;最好使用
StringBuilder
或其他类似的工具来设计大量动态字符串连接。

for后面的分号。for循环的工作方式与您设计的一样:不做任何事情循环1000次不显示输出s:mubeen1000,,但在条件结束之前,此代码应将输出显示为,,,mubeen1,mubeen2,mubeen3等等。请帮助我解决很多问题。我刚刚删除了“for”后面的分号..-Dogeam Azed请写下您所更改的内容,并尝试格式化代码,以便op能够理解他刚才所做的错误。在我准备答案时错误地提交了它。感谢您指出您不应该有这样的BusyLop,但尤其不应该在UI线程中。括号不需要,因为它是一个语句t在for循环中。@DogeAmazed我知道,但如果op想添加另一行代码,这是一个很好的做法。我在这里也是新来的,从一开始就学习c。。我仍然有相同的问题,比如。。这段代码在条件结束前应该显示输出为,,,mubeen1,mubeen2,mubeen3等等。帮我解决pls@MubeenKhan编辑问题以显示您的输出ut是理想的,您根本不应该有这样的BusyLop,尤其是在UI线程中。
Thread t;
string mystring = "";
private void button2_Click(object sender, EventArgs e)
{

    t = new Thread(write);
    t.Start();
    while (t.IsAlive) ;
    textBox1.Text = mystring;

}

void write()
{
    int i;
    for ( i=0; i<1000 ;i++ ) {
      mystring+= "mubeen" +i.ToString() + "\r\n";
    }


}
int i;
// since you don't specify the method to loop through, 
// this is basically looping through doing nothing other than incrementing i, resulted in i = 1000, in which therefore exiting the loop
for (i=0; i<1000; i++); 

// this is the next statement which is execute, which is setting mystring to mubeen1000
mystring+= "mubeen" +i.ToString() + "\r\n"; 
// since you expect it starts with mubeen1, start with i=1 and the condition i<=1000 (instead of i<1000)
int i;
for (i=1; i<=1000; i++) 
// specify what you want to do in the body of the loop
{
     mystring+= "mubeen" + i.ToString() + "\r\n"; 
}
while (t.IsAlive) 
{
    textBox1.Text = mystring;
}
private async void button2_Click(object sender, EventArgs e)
{
    string myString = await Task.Run(() => Write());
    textBox1.Text = myString;
}

private string Write()
{
    StringBuilder output = new StringBuilder();
    for (int i = 0; i < 1000; i++)
        output.AppendLine("mubeen" + i);

    return output.ToString();
}