楼主是想实现循环一次显示一次吧 for (int i = 0; i < 10; i++) { textBox1.Text = i.ToString(); Thread.Sleep(1000) } 这个for循环不执行完是textBox1不会显示的 开个线程是个办法不过我估计还要在开的线程里边Thread.Sleep下不然估计也会因为更新的太快而 做不出效果
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; using System.Threading;namespace WindowsFormsApplication6 { public partial class Form1 : Form { private delegate void TextDelegate(string a); public Form1() { InitializeComponent(); } /// <summary> /// 计数器开始 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void button1_Click(object sender, EventArgs e) { Thread th1 = new Thread(new ThreadStart(SetText)); th1.Start(); } private void SetText() { for (int i = 0; i < 10; i++) { this.SetTest(i.ToString()); Thread.Sleep(1000); }
} private void SetTest(string a) { if (this.textBox1.InvokeRequired) { TextDelegate h = new TextDelegate(SetTest); this.Invoke(h,new object[]{a}); } else { this.textBox1.Text = a; } } } }
你这样对winform的控件赋值是不安全的
多線程處理,委托來更改控件狀態 拖一個Buttion 和 一個textbox 到 form 然后后臺演示代碼如下,自己去擴充:Imports System.Threading Public Class Form1 Public i As Integer '委托 Delegate Sub SetValeCallback(ByVal [Value] As Integer) '線程 Private thdProgress As Thread Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click If IsNothing(thdProgress) = False Then If thdProgress.IsAlive Then thdProgress.Abort() End If End If thdProgress = New Thread(AddressOf AddInt) thdProgress.Start() End Sub Private Sub AddInt() For Me.i = 0 To 999 i = i + 1 SetText(i) Thread.Sleep(10) Next End Sub '控制TXT顯示 Private Sub SetText(ByVal [Value] As Integer) If Me.InvokeRequired Then Dim d As New SetValeCallback(AddressOf SetText) Me.Invoke(d, New Object() {[Value]}) Else TextBox1.Text = i.ToString End If End Sub '釋放線程 Private Sub Form1_FormClosing(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosingEventArgs) Handles Me.FormClosing If IsNothing(thdProgress) = False Then If thdProgress.IsAlive Then thdProgress.Abort() End If End If End Sub End Class
因为lz的处理语句在窗体线程中,Thread.Sleep(1000); 把窗体线程让权了,窗体会假死,直到全部执行完毕,窗体不假死了,也显示完毕了...
你要什么? Thread.Sleep(1000);
有什么用。。
只是放大间隙用来看中间过程的
for (int i = 0; i < 10; i++)
{
textBox1.Text = i.ToString();
Thread.Sleep(1000)
}
这个for循环不执行完是textBox1不会显示的
开个线程是个办法不过我估计还要在开的线程里边Thread.Sleep下不然估计也会因为更新的太快而
做不出效果
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Threading;namespace WindowsFormsApplication6
{
public partial class Form1 : Form
{
private delegate void TextDelegate(string a);
public Form1()
{
InitializeComponent();
} /// <summary>
/// 计数器开始
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void button1_Click(object sender, EventArgs e)
{
Thread th1 = new Thread(new ThreadStart(SetText));
th1.Start();
} private void SetText()
{
for (int i = 0; i < 10; i++)
{
this.SetTest(i.ToString());
Thread.Sleep(1000);
}
} private void SetTest(string a)
{
if (this.textBox1.InvokeRequired)
{
TextDelegate h = new TextDelegate(SetTest);
this.Invoke(h,new object[]{a});
}
else
{
this.textBox1.Text = a;
}
}
}
}
拖一個Buttion 和 一個textbox 到 form
然后后臺演示代碼如下,自己去擴充:Imports System.Threading
Public Class Form1
Public i As Integer '委托
Delegate Sub SetValeCallback(ByVal [Value] As Integer) '線程
Private thdProgress As Thread Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
If IsNothing(thdProgress) = False Then
If thdProgress.IsAlive Then
thdProgress.Abort()
End If
End If
thdProgress = New Thread(AddressOf AddInt)
thdProgress.Start()
End Sub Private Sub AddInt()
For Me.i = 0 To 999
i = i + 1
SetText(i)
Thread.Sleep(10)
Next
End Sub
'控制TXT顯示
Private Sub SetText(ByVal [Value] As Integer)
If Me.InvokeRequired Then
Dim d As New SetValeCallback(AddressOf SetText)
Me.Invoke(d, New Object() {[Value]})
Else
TextBox1.Text = i.ToString
End If
End Sub '釋放線程
Private Sub Form1_FormClosing(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosingEventArgs) Handles Me.FormClosing
If IsNothing(thdProgress) = False Then
If thdProgress.IsAlive Then
thdProgress.Abort()
End If
End If
End Sub
End Class