再Form1中加入一個全局變量, 點擊Form2的確定後先對Form1的那個全局變量進行賦值,在Form1中的那個需要傳遞的參數就用這個全局變量來代替,不就好了,不過耗時線程建議不要放在Form中

解决方案 »

  1.   

    那请教线程应该放在哪里?如何控制?我窗口用this.closed()后线程还能存在吗?
      

  2.   

    private void InitializeComponent()
    {
    this.textBox1 = new System.Windows.Forms.TextBox();
    this.button1 = new System.Windows.Forms.Button();
    this.SuspendLayout();
    // 
    // textBox1
    // 
    this.textBox1.Location = new System.Drawing.Point(80, 80);
    this.textBox1.Name = "textBox1";
    this.textBox1.TabIndex = 0;
    this.textBox1.Text = "textBox1";
    // 
    // button1
    // 
    this.button1.Location = new System.Drawing.Point(88, 136);
    this.button1.Name = "button1";
    this.button1.TabIndex = 1;
    this.button1.Text = "button1";
    this.button1.Click += new System.EventHandler(this.button1_Click);
    // 
    // frmMain
    // 
    this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
    this.ClientSize = new System.Drawing.Size(292, 273);
    this.Controls.Add(this.button1);
    this.Controls.Add(this.textBox1);
    this.Name = "frmMain";
    this.Text = "Form1";
    this.ResumeLayout(false);
    me.OnMessage += new frmNext.Message(this.txtMessage); }
    #endregion /// <summary>
    /// 应用程序的主入口点。
    /// </summary>
    [STAThread]
    static void Main() 
    {
    Application.Run(new frmMain());
    } private void txtMessage(string str)
    {
    this.textBox1.Text = str;
    } private void button1_Click(object sender, System.EventArgs e)
    {
    me.writeMessage("hello from main");
    me.Show();
    } }
    以上为Form1 private void button1_Click(object sender, System.EventArgs e)
    {
    this.OnMessage("Hello");
    }
    public void writeMessage(string str)
    {
    this.textBox1.Text = str;
    } private void frmNext_Closing(object sender, System.ComponentModel.CancelEventArgs e)
    {
    e.Cancel = true;;
    this.Hide();
    }
    //form2
      

  3.   

    對於線程的控制,我個人認為一個另外寫一個Class , 線程要執行的函數就放在這個Class中,函數重要用到的變量全部定義為全局變量,例如
    public class WorkThread
    {
      //下面定義一些變量
      public int nPort = -1;
      public WorkThread()
        {
         }
      //下面是工作函數
      public void Work()
       { 
          //Some work code    
        }
    }//下面是調用
    WorkThread oWork = new WorkThread();
    oWork = 10;
    Thread t = new Thread(new ThreadStart(oWork.Work)
    t.IsBackGround = true;
    t.Start();這樣在Work函數要耗掉很多CPU時間的情況下,你的主程序介面刷新也不會受到影響
      

  4.   

    华仔的FORM1中:me.OnMessage += new frmNext.Message(this.txtMessage);
    没看懂!还请指教!