2012-10-30 99 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

相关问题