2012-10-30 34 views
0

我想我自己的類開始,但它似乎沒有工作。創建一個自己的類,並啓動它

我的班級是:

namespace Ts3_Movearound 
{ 
    class TS3_Connector 
    { 
     public class ccmove : EventArgs 
     { 
      public ccmove(int clid, int cid) 
      { 
       this.clid = clid; 
       this.cid = cid; 
      } 
      public int clid; 
      public int cid; 
     } 

     public event EventHandler runningHandle; 
     public event EventHandler stoppedHandle; 
     public event EventHandler RequestMove; 

     bool running = true; 
     public Main() 
     { 

      using (QueryRunner queryRunner = new QueryRunner(new SyncTcpDispatcher("127.0.0.1", 25639))) // host and port 
      { 
       this.runningHandle(this, new EventArgs()); 
       while (running == true) 
       { 
        this.RequestMove(this, new EventArgs()); 
        System.Threading.Thread.Sleep(1000); 
       } 
       this.stoppedHandle(this, new EventArgs()); 
      } 
      Console.ReadLine(); 
     } 
    } 
} 

和我這樣調用它:

private void button1_Click(object sender, EventArgs e) 
    { 
     TS3_Connector conn = new TS3_Connector(); 

     conn.runningHandle += new EventHandler(started); 
     conn.stoppedHandle += new EventHandler(stopped); 
    } 

,但似乎從未類可以正常啓動。 runningEvent永遠不會觸發,也停止和請求。我現在該如何運行這個類?

+0

您爲什麼認爲它應該起作用?你剛剛創建了一個實例。 'Main'方法永遠不會被調用。 –

+0

該死的..你是對的... – Styler2go

回答

0

當button1_Click返回時,conn對象的生命週期結束。你應該在類作用域中聲明conn。

TS3_Connector conn = null; 

private void button1_Click(object sender, EventArgs e) 
{ 
    conn = new TS3_Connector(); 

    conn.runningHandle += new EventHandler(started); 
    conn.stoppedHandle += new EventHandler(stopped); 
} 

TS3_Connector本身沒有做什麼。您應該明確地致電main()(請將功能重新命名爲可理解的東西)

+0

呃..你能告訴我怎麼在自己的線程中運行這個類嗎? – Styler2go

相關問題