日期:2014-05-17  浏览次数:21035 次

[求助]C#中多线程中窗体的操作
项目中遇到一个问题,一直没解决,球牛人[code=C#][/code]帮助。
在主窗口线程创建一窗口,并把其隐藏,然都的等待消息,在另外线程中把该窗口显示出,这怎样实现啊,有做过这方面的给点指点,告诉我从那方向下手查资料也行,我都查了一天了。。。
首先在窗体中重新定义了show和hide方法
C# code

 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();
            }
        }

在主线程中创建该窗体
C# code

private void FormMain_Load(object sender, EventArgs e)
{
            fingerIm = new FingerIm();
            fingerIm.ThreadShow();
            fingerIm.ThreadHide();
}
在另一线程中显示该窗体



 
C# code

fingerIm.ThreadShow();


但是程序运行的结果并不对,但不调试窗口出现了,但是一片白,窗口上的空间没有显示

------解决方案--------------------
windows form就只有一个UI线程,所以你看main方法的入口都有STAThread的attribute
控制UI线程,其它的工作线程是做不到的,这个时候就需要begininvoke方法

其实就是个异步委托,等待UI线程在空闲的时候在响应就可以了

不知道楼主的this是什么,不过可以改成begininvoke试验下
------解决方案--------------------
那this 就是那个窗口的正在运行的实例了, 可以用begininvoke方法替换掉invoke试验下
------解决方案--------------------
C# code

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 Sy