c#怎样用this.Invoke()方法 c#怎样利用this.Invoke()方法,用委托调用带参数的方法?new ParameterizedThreadStart()除外 。兄弟帮帮忙。 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 Invoke一般是用在跨线程调用的时候使用using System;using System.Drawing;using System.Windows.Forms;using System.Threading; public class MyFormControl : Form { public delegate void AddListItem(String myString); public AddListItem myDelegate; private Button myButton; private Thread myThread; private ListBox myListBox; public MyFormControl() { myButton = new Button(); myListBox = new ListBox(); myButton.Location = new Point(72, 160); myButton.Size = new Size(152, 32); myButton.TabIndex = 1; myButton.Text = "Add items in list box"; myButton.Click += new EventHandler(Button_Click); myListBox.Location = new Point(48, 32); myListBox.Name = "myListBox"; myListBox.Size = new Size(200, 95); myListBox.TabIndex = 2; ClientSize = new Size(292, 273); Controls.AddRange(new Control[] {myListBox,myButton}); Text = " 'Control_Invoke' example "; myDelegate = new AddListItem(AddListItemMethod); } static void Main() { MyFormControl myForm = new MyFormControl(); myForm.ShowDialog(); } public void AddListItemMethod(String myString) { myListBox.Items.Add(myString); } private void Button_Click(object sender, EventArgs e) { myThread = new Thread(new ThreadStart(ThreadFunction)); myThread.Start(); } private void ThreadFunction() { MyThreadClass myThreadClassObject = new MyThreadClass(this); myThreadClassObject.Run(); } } public class MyThreadClass { MyFormControl myFormControl1; public MyThreadClass(MyFormControl myForm) { myFormControl1 = myForm; } String myString; public void Run() { for (int i = 1; i <= 5; i++) { myString = "Step number " + i.ToString() + " executed"; Thread.Sleep(400); // Execute the specified delegate on the thread that owns // 'myFormControl1' control's underlying window handle with // the specified list of arguments. myFormControl1.Invoke(myFormControl1.myDelegate, new Object[] {myString}); } } } Invoke(new A(B), new Object[] { ""}); http://topic.csdn.net/u/20100704/12/964493ac-547f-4b54-addb-bf014a459bc1.html this.Invoke((EventHandler)delegate{ //随便你调用什么,有没参数都行,委托不用了。直接调用函数即可。当然直接委托也行。 委托(参数);}); 这个一般是用来保持界面响应速度的,就是使用多线程来防止界面假死。给你个例子using System;using System.Collections.Generic;using System.ComponentModel;using System.Data;using System.Drawing;using System.Text;using System.Windows.Forms;using System.Threading;namespace PanelSwitch{ public class Form1 : Form { #region 窗体设计 private System.ComponentModel.IContainer components = null; protected override void Dispose(bool disposing) { if (disposing && (components != null)) { components.Dispose(); } base.Dispose(disposing); } private void InitializeComponent() { this.textBox1 = new System.Windows.Forms.TextBox(); this.textBox2 = new System.Windows.Forms.TextBox(); this.btnStart = new System.Windows.Forms.Button(); this.btbStop = new System.Windows.Forms.Button(); this.SuspendLayout(); // // textBox1 // this.textBox1.Location = new System.Drawing.Point(47, 65); this.textBox1.Name = "textBox1"; this.textBox1.Size = new System.Drawing.Size(210, 21); this.textBox1.TabIndex = 0; // // textBox2 // this.textBox2.Location = new System.Drawing.Point(47, 65); this.textBox2.Name = "textBox2"; this.textBox2.Size = new System.Drawing.Size(210, 21); this.textBox2.TabIndex = 1; this.textBox2.Visible = false; // // btnStart // this.btnStart.Location = new System.Drawing.Point(100, 133); this.btnStart.Name = "btnStart"; this.btnStart.Size = new System.Drawing.Size(75, 23); this.btnStart.TabIndex = 2; this.btnStart.Text = "开始"; this.btnStart.UseVisualStyleBackColor = true; this.btnStart.Click += new System.EventHandler(this.btnStart_Click); // // btbStop // this.btbStop.Location = new System.Drawing.Point(182, 133); this.btbStop.Name = "btbStop"; this.btbStop.Size = new System.Drawing.Size(75, 23); this.btbStop.TabIndex = 3; this.btbStop.Text = "停止"; this.btbStop.UseVisualStyleBackColor = true; this.btbStop.Click += new System.EventHandler(this.btnStop_Click); // // Form1 // this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 12F); this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font; this.ClientSize = new System.Drawing.Size(310, 250); this.Controls.Add(this.btbStop); this.Controls.Add(this.btnStart); this.Controls.Add(this.textBox2); this.Controls.Add(this.textBox1); this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedSingle; this.MaximizeBox = false; this.MinimizeBox = false; this.Name = "Form1"; this.Text = "动态切换"; this.FormClosing += new System.Windows.Forms.FormClosingEventHandler(this.Form1_FormClosing); this.ResumeLayout(false); this.PerformLayout(); } private System.Windows.Forms.TextBox textBox1; private System.Windows.Forms.TextBox textBox2; private System.Windows.Forms.Button btnStart; private System.Windows.Forms.Button btbStop; #endregion #region 逻辑代码 private volatile bool _shouldStop; public Form1() { InitializeComponent(); _shouldStop = false; } private void btnStart_Click(object sender, EventArgs e) { this.textBox1.Visible = true; this.textBox2.Visible = false; _shouldStop = false; Thread getNum = new Thread(new ThreadStart(GetRandomNum)); getNum.Start(); } public delegate void SetValue(int num); private void SetTxtBoxConten(int num) { this.textBox1.Text = num.ToString(); } private void GetRandomNum() { while (!_shouldStop) { Random r = new Random(); int num = r.Next(1000000, 9999999); this.textBox1.Invoke(new SetValue(SetTxtBoxConten), new object[] { num }); Thread.Sleep(100); } } private void Form1_FormClosing(object sender, FormClosingEventArgs e) { _shouldStop = true; Environment.Exit(0); } private void btnStop_Click(object sender, EventArgs e) { _shouldStop = true; this.textBox1.Visible = false; this.textBox2.Visible = true; this.textBox2.Text = string.Format("你选择了{0}",this.textBox1.Text); } #endregion [STAThread] static void Main() { Application.EnableVisualStyles(); Application.SetCompatibleTextRenderingDefault(false); Application.Run(new Form1()); } }} 用回车控制光标焦点,小问题 C#泛型类如何读取不同类型的数据 再问gridview的问题 dataTable.select(...)写法问题 急,急,毕业设计急需解决的问题,控件透明问题。 DataGrid样式的问题,在线等! 呜呜...这段代码怎么改都改不正确.路过的好心人帮我看看.今晚弄不出来就挂了 TCPclient传输文件,丢包,不得不使用sleep,请求解决方案! C#如何写伪代码 有那么一点点奇怪的问题 请教下怎么用VS2005在SQL数据库里面读取出图片并且点击按钮“上一张”“下一张”读取 线程访问数据库的问题
using System.Drawing;
using System.Windows.Forms;
using System.Threading; public class MyFormControl : Form
{
public delegate void AddListItem(String myString);
public AddListItem myDelegate;
private Button myButton;
private Thread myThread;
private ListBox myListBox;
public MyFormControl()
{
myButton = new Button();
myListBox = new ListBox();
myButton.Location = new Point(72, 160);
myButton.Size = new Size(152, 32);
myButton.TabIndex = 1;
myButton.Text = "Add items in list box";
myButton.Click += new EventHandler(Button_Click);
myListBox.Location = new Point(48, 32);
myListBox.Name = "myListBox";
myListBox.Size = new Size(200, 95);
myListBox.TabIndex = 2;
ClientSize = new Size(292, 273);
Controls.AddRange(new Control[] {myListBox,myButton});
Text = " 'Control_Invoke' example ";
myDelegate = new AddListItem(AddListItemMethod);
}
static void Main()
{
MyFormControl myForm = new MyFormControl();
myForm.ShowDialog();
}
public void AddListItemMethod(String myString)
{
myListBox.Items.Add(myString);
}
private void Button_Click(object sender, EventArgs e)
{
myThread = new Thread(new ThreadStart(ThreadFunction));
myThread.Start();
}
private void ThreadFunction()
{
MyThreadClass myThreadClassObject = new MyThreadClass(this);
myThreadClassObject.Run();
}
}
public class MyThreadClass
{
MyFormControl myFormControl1;
public MyThreadClass(MyFormControl myForm)
{
myFormControl1 = myForm;
}
String myString; public void Run()
{
for (int i = 1; i <= 5; i++)
{
myString = "Step number " + i.ToString() + " executed";
Thread.Sleep(400);
// Execute the specified delegate on the thread that owns
// 'myFormControl1' control's underlying window handle with
// the specified list of arguments.
myFormControl1.Invoke(myFormControl1.myDelegate,
new Object[] {myString});
}
}
}
http://topic.csdn.net/u/20100704/12/964493ac-547f-4b54-addb-bf014a459bc1.html
{
//随便你调用什么,有没参数都行,委托不用了。直接调用函数即可。当然直接委托也行。
委托(参数);
});
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Threading;namespace PanelSwitch
{
public class Form1 : Form
{ #region 窗体设计
private System.ComponentModel.IContainer components = null;
protected override void Dispose(bool disposing)
{
if (disposing && (components != null))
{
components.Dispose();
}
base.Dispose(disposing);
}
private void InitializeComponent()
{
this.textBox1 = new System.Windows.Forms.TextBox();
this.textBox2 = new System.Windows.Forms.TextBox();
this.btnStart = new System.Windows.Forms.Button();
this.btbStop = new System.Windows.Forms.Button();
this.SuspendLayout();
//
// textBox1
//
this.textBox1.Location = new System.Drawing.Point(47, 65);
this.textBox1.Name = "textBox1";
this.textBox1.Size = new System.Drawing.Size(210, 21);
this.textBox1.TabIndex = 0;
//
// textBox2
//
this.textBox2.Location = new System.Drawing.Point(47, 65);
this.textBox2.Name = "textBox2";
this.textBox2.Size = new System.Drawing.Size(210, 21);
this.textBox2.TabIndex = 1;
this.textBox2.Visible = false;
//
// btnStart
//
this.btnStart.Location = new System.Drawing.Point(100, 133);
this.btnStart.Name = "btnStart";
this.btnStart.Size = new System.Drawing.Size(75, 23);
this.btnStart.TabIndex = 2;
this.btnStart.Text = "开始";
this.btnStart.UseVisualStyleBackColor = true;
this.btnStart.Click += new System.EventHandler(this.btnStart_Click);
//
// btbStop
//
this.btbStop.Location = new System.Drawing.Point(182, 133);
this.btbStop.Name = "btbStop";
this.btbStop.Size = new System.Drawing.Size(75, 23);
this.btbStop.TabIndex = 3;
this.btbStop.Text = "停止";
this.btbStop.UseVisualStyleBackColor = true;
this.btbStop.Click += new System.EventHandler(this.btnStop_Click);
//
// Form1
//
this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 12F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(310, 250);
this.Controls.Add(this.btbStop);
this.Controls.Add(this.btnStart);
this.Controls.Add(this.textBox2);
this.Controls.Add(this.textBox1);
this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedSingle;
this.MaximizeBox = false;
this.MinimizeBox = false;
this.Name = "Form1";
this.Text = "动态切换";
this.FormClosing += new System.Windows.Forms.FormClosingEventHandler(this.Form1_FormClosing);
this.ResumeLayout(false);
this.PerformLayout(); }
private System.Windows.Forms.TextBox textBox1;
private System.Windows.Forms.TextBox textBox2;
private System.Windows.Forms.Button btnStart;
private System.Windows.Forms.Button btbStop;
#endregion #region 逻辑代码
private volatile bool _shouldStop;
public Form1()
{
InitializeComponent();
_shouldStop = false;
}
private void btnStart_Click(object sender, EventArgs e)
{
this.textBox1.Visible = true;
this.textBox2.Visible = false;
_shouldStop = false;
Thread getNum = new Thread(new ThreadStart(GetRandomNum));
getNum.Start();
}
public delegate void SetValue(int num);
private void SetTxtBoxConten(int num)
{
this.textBox1.Text = num.ToString();
}
private void GetRandomNum()
{
while (!_shouldStop)
{
Random r = new Random();
int num = r.Next(1000000, 9999999);
this.textBox1.Invoke(new SetValue(SetTxtBoxConten), new object[] { num });
Thread.Sleep(100);
}
}
private void Form1_FormClosing(object sender, FormClosingEventArgs e)
{
_shouldStop = true;
Environment.Exit(0);
}
private void btnStop_Click(object sender, EventArgs e)
{
_shouldStop = true;
this.textBox1.Visible = false;
this.textBox2.Visible = true;
this.textBox2.Text = string.Format("你选择了{0}",this.textBox1.Text);
}
#endregion [STAThread]
static void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new Form1());
}
}
}