[求助]C#中多线程中窗体的操作
项目中遇到一个问题,一直没解决,球牛人[code=C#][/code]帮助。
在主窗口线程创建一窗口,并把其隐藏,然都的等待消息,在另外线程中把该窗口显示出,这怎样实现啊,有做过这方面的给点指点,告诉我从那方向下手查资料也行,我都查了一天了。。。
首先在窗体中重新定义了show和hide方法
public void ThreadShow() { if (this.InvokeRequired) { MethodInvoker invoker = new MethodInvoker(this.Show); this.Invoke(invoker, null); } else { this.Show(); } } public void ThreadHide() { if (this.InvokeRequired) { MethodInvoker invoker = new MethodInvoker(this.Hide); this.Invoke(invoker, null); } else { this.Hide(); } }
private void FormMain_Load(object sender, EventArgs e){ fingerIm = new FingerIm(); fingerIm.ThreadShow(); fingerIm.ThreadHide();}在另一线程中显示该窗体
fingerIm.ThreadShow();
namespace Show{ partial class Form1 { /// <summary> /// 必需的设计器变量。 /// </summary> private System.ComponentModel.IContainer components = null; /// <summary> /// 清理所有正在使用的资源。 /// </summary> /// <param name="disposing">如果应释放托管资源,为 true;否则为 false。</param> protected override void Dispose(bool disposing) { if (disposing && (components != null)) { components.Dispose(); } base.Dispose(disposing); } #region Windows 窗体设计器生成的代码 /// <summary> /// 设计器支持所需的方法 - 不要 /// 使用代码编辑器修改此方法的内容。 /// </summary> private void InitializeComponent() { this.button1 = new System.Windows.Forms.Button(); this.SuspendLayout(); // // button1 // this.button1.Location = new System.Drawing.Point(96, 92); this.button1.Name = "button1"; this.button1.Size = new System.Drawing.Size(75, 23); this.button1.TabIndex = 0; this.button1.Text = "线程开始"; this.button1.UseVisualStyleBackColor = true; this.button1.Click += new System.EventHandler(this.button1_Click); // // Form1 // this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 12F); this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font; this.ClientSize = new System.Drawing.Size(292, 266); this.Controls.Add(this.button1); this.Name = "Form1"; this.Text = "Form1"; this.ResumeLayout(false); } #endregion private System.Windows.Forms.Button button1; }}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 Show{ public partial class Form1 : Form { public Form1() { InitializeComponent(); } private void button1_Click(object sender, EventArgs e) { new Thread(delegate() { ShowMsg(); }).Start(); } public void ShowMsg() { if (InvokeRequired) { BeginInvoke(new Action(ShowMsg)); } else { MessageBox.Show("线程调用"); } } }}