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 DoubleFromTest
{
    public partial class Form1 : Form
    {
        Form2 frm;
        public Form1()
        {
            InitializeComponent();
        }
        Thread MyThreadOne;
        private void Form1_Load(object sender, EventArgs e)
        {
            MyThreadOne = new Thread(new ThreadStart(ThreadOne));
            MyThreadOne.Start();
        }
        public void ThreadOne()
        {
            frm = new Form2();
            frm.ShowDialog();
            //定义线程一
            //           MessageBox.Show("这是线程一产生的对话框,你可以不关闭它就可以执行程序的其他功能!", "信息提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
        }        private void button1_Click(object sender, EventArgs e)
        {
            if (frm != null)
            {
                if (frm.BackColor != Color.Black)
                {                    frm.BackColor = Color.Black;
                }
                else
                {
                    frm.BackColor = Color.Blue;
                }
            }
        }        private void button2_Click(object sender, EventArgs e)
        {
            if (frm == null)
            {
                return;
            }
            else
            {
                if (frm.InvokeRequired)
                {                    this.backgroundWorker1.RunWorkerAsync();
                }
                else
                {
                    frm.GetMessage = Form2.FrmMessages.MESSAGE_FULLSCREEN; ;
                }
            }
            
        }        private void button3_Click(object sender, EventArgs e)
        {
            if (frm == null)
            {
                return;
            }
            else
            {
                frm.GetMessage = Form2.FrmMessages.MESSAGE_MINIWINSTATE;
            }
        }        private void backgroundWorker1_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
        {
            frm.GetMessage = Form2.FrmMessages.MESSAGE_FULLSCREEN;
        }
    }
}///////////////////////////
/////////////
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 DoubleFromTest
{
    public partial class Form2 : Form
    {
        public enum FrmMessages
        {
            MESSAGE_FULLSCREEN,
            MESSAGE_NULL,
            MESSAGE_MINIWINSTATE,
        }        private FrmMessages MyGetMessage;
        public FrmMessages GetMessage
        {
            get
            {
                return MyGetMessage;
            }
            set
            {
                MyGetMessage = value;
                switch (MyGetMessage)
                {
                    case FrmMessages.MESSAGE_FULLSCREEN:
                        {
                            this.WindowState = FormWindowState.Maximized;
                            break;
                        }
                    case FrmMessages.MESSAGE_MINIWINSTATE:
                        {
                            this.WindowState = FormWindowState.Minimized;
                            break;
                        }
                    case FrmMessages.MESSAGE_NULL:
                        {
                            break;
                        }
                    default:
                        {
                            break;
                        }
                }
            }
        }
        public void SetOurMesaage(FrmMessages mes)
        {
            GetMessage = mes;
        }        public Form2()
        {
            InitializeComponent();
        }        private void Form2_Load(object sender, EventArgs e)
        {
            
        }
        public void ThreadOne()
        {
           Form2 frm = new Form2();
            frm.ShowDialog();
            //定义线程一
 //           MessageBox.Show("这是线程一产生的对话框,你可以不关闭它就可以执行程序的其他功能!", "信息提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
        }
    }
}调用button2_Click的时候提示异常:InvalidOperationException那个大哥给点帮助。

解决方案 »

  1.   

    你的线程中是没办法show一个form的.只能在主线程中.线程要切换.
    看invokerequired
      

  2.   

    首先你要申明一个代理用来处理窗体显示
    public delegate void SetLabelText(Form2 fm);   //定义委托 
    private void InvokeControl(Form2 fm) 

    if (this.InvokeRequired) 
    {
    this.Invoke(new SetLabelText(fm)); 
    }
    else 
    {
    fm.show();
    }

      

  3.   

    创建窗体时没有问题的。因为是创建一个窗体,委托它创建窗口from2.关键是在窗体起创建之后,通过From1的操作来控制From2。这个例子的操作有最大化,最小化和关闭。而且操作的可控性在自己from1中实现。
      

  4.   


     //Close Form;
                    ISynchronizeInvoke synchronizer;
                    synchronizer = this;
                    if (synchronizer.InvokeRequired)
                    {
                        MethodInvoker invoker = new
                        MethodInvoker(this.Close);
                        synchronizer.Invoke(invoker, null);
                    }
                    else
                    { this.Close(); }