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 永远不会被触发,stopped 和 Request 也是如此。我现在如何运行这个课程?

4

1 回答 1

0

The lifespan of your conn object ends when button1_Click has returned. You should declare conn in the class scope.

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 itself does nothing. You should explicitly call main() (please rename the function to something understandable)

于 2012-10-30T09:04:40.577 回答