C#泥服务器/线程关闭问题 [英] c# mud server / thread closing question

查看:87
本文介绍了C#泥服务器/线程关闭问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在研究从Hammer Mud派生的Mud服务器.我添加了一个GUI
并尝试添加多线程.在跟踪了许多示例之后,我发现了一些可以启动新线程的方法.
我的问题是,一旦单击停止"按钮,便试图关闭该线程.

这是我的代码,如果您发现我可能犯的任何其他错误,请告诉我我正在学习的内容.是的,大多数代码都来自msdn教程,所有功劳将在应有的位置给出.

这是代码,我不确定哪一部分是相关的,因此我将其全部发布给表单加载器.

I am currently working on a mud server that is derived from Hammer Mud. I added a GUI
and tried adding multi-threading. After following many examples, I found several that worked on starting a new thread.
My problem is trying to shut down said thread once the stop button is clicked.

Here is my code, if you see any other mistakes I may have made, please tell me as I am just learning. Yes most of the code for this is from a msdn tutorial, All credit will be given where it is due.

Here is the code, I am not sure what part is relevant, so I am posting it all for the form loader.

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.Threading;

namespace Lima
{
    public partial class Form1 : Form
    {
        //Thread Closing Variables
        int numThreads = 0;
        bool closeRequested = false;
        int counter = 0;

        //For Console to Box
        TextWriter _writer = null;

        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            if (this.numThreads >= 1)
                return;

            // Display the thread creation status
            Console.WriteLine("Creating Thread {0}",
                this.numThreads + 1);

            // Start a new thread and increment the count
            this.numThreads++;
            Thread thread = new Thread(new ThreadStart(this.ThreadProc));
            thread.Start();
}
        private void CloseMe(object o, EventArgs e)
        {
            // Decrease number of threads and shutdown
            this.numThreads--;
            this.Close();
        }

        protected override void OnClosing(CancelEventArgs e)
        {
            if (this.numThreads > 0)
            {
                // If there are no secondary threads running then shut down
                // the application
                e.Cancel = true;
                Console.WriteLine("Closing Thread {0}",
                    this.numThreads + 1);
            }
            else
            {
                // If there are secondary threads running then cancel
                // the application shutdown
                e.Cancel = false;
                Console.WriteLine("Closing Main Thread");
            }

            // A close request is now pending
            this.closeRequested = true;
        }

        public void ThreadProc()
        {
            // Use the UpdateCount function to update the display
            EventHandler threadDelegate =
                new EventHandler(this.UpdateCounter);

            //Calls the function to run the server
            HammerMUDServer.Program.Server.loading();

            // Run until the application tries to shutdown
            do
            {
                // Call the counter updating function
                this.Invoke(threadDelegate);
                Thread.Sleep(200);
            }
            while (!this.closeRequested);

            // Close the thread
            this.Invoke(new EventHandler(this.CloseMe));
        }
              

        private void Form1_Load(object sender, EventArgs e)
        {
            //sed for Console to Box
            _writer = new TextBoxStreamWriter(txtConsole);
           Console.SetOut(_writer);
           }

        private void buttonExit_Click(object sender, EventArgs e)
        {
            //Exits Application and closes all threads Strated and running From the Application
            Environment.Exit(0);
        }

        private void UpdateCounter(object o, EventArgs e)
{
        String.Format("Threads({0}) Counting: {1}",
        (this.numThreads), this.counter++);
}

        private void buttonStop_Click(object sender, EventArgs e)
        {
           // this.closeRequested = true;
        }
    }
}

推荐答案

我取消了msdn中的所有内容,并使用了abort命令.我现在正在编写一个命令,以在发出about命令之前踢所有玩家.否则,服务器不会关闭.
I undid every thing from the msdn and used the abort command. I am now writing a command to kick all players before the about command is issued. Other wise the server does not close.


这篇关于C#泥服务器/线程关闭问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆