创建一个自己的类并启动它

时间:2012-10-30 08:59:49

标签: c#

我想让自己的班级开始,但似乎没有工作。

我的班级是:

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

但似乎Class永远不会正确启动。 runningEvent永远不会被解雇,也就是停止和请求。我现在该怎么办这个班?

1 个答案:

答案 0 :(得分: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()(请将函数重命名为可理解的内容)

相关问题