我无法结束话题

时间:2018-09-25 12:36:16

标签: c# multithreading

我正在尝试使用Thread.Abort(),但它冻结了我的GUI...。它冻结了除statusLabel之外的所有元素...它一直在更新...代码有什么问题... < / p>

我不知道为什么会这样,我认为这可能是try catch块,但是它在线程中...我相信它不会干扰任何事情

代码:

https://paste.lamlam.io/davagokeza.cs#o34Oy3vsWwK95ya0B5bk2leI8R6xweyk

using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Net;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Web;
using System.Windows.Forms;

namespace RunAsWebServer
{
    public partial class Form1 : Form
    {
        WebServer ws;

        List<Proxy> resultList;

        bool continueTask = true;

        public Form1()
        {
            InitializeComponent();

            CheckForIllegalCrossThreadCalls = false;

            CreatServer("127.0.0.1:8080");
        }



        public void CreatServer(string server)
        {
            ws = new WebServer(SendResponse, "http://"+ server +"/");
            ws.Run();
        }


        public string SendResponse(HttpListenerRequest request)
        {
            var query = request.QueryString;
            var ip =  query["ip"];
            if (string.IsNullOrEmpty(ip))
            {
                return "It's working...";
            }
            return new Whoer().Check(ip);
        }

        private void Form1_FormClosing(object sender, FormClosingEventArgs e)
        {
            ws.Stop();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            try
            {
                ws.Stop();
                CreatServer(textBox1.Text.Trim() + ":"+textBox2.Text.Trim());

                MessageBox.Show("Server: " + textBox1.Text + textBox2.Text + " OK!", "Server", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
            catch (Exception ex)
            {
                MessageBox.Show("Server: " + textBox1.Text + textBox2.Text + " Failed!", "Server", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        List<Thread> threads;
        ConcurrentQueue<string> queue;

        private void button3_Click(object sender, EventArgs e)
        {

            if (threads != null)
            {
                foreach (var t in threads)
                {
                    if (t != null)
                    {
                        if (t.IsAlive)
                        {
                            t.Abort();
                            //MessageBox.Show("Wait all threads finish" + " " +t.ManagedThreadId.ToString());
                            //return;
                        }
                    }
                }
            }

            continueTask = true;

            toolStripStatusLabel1.Text = "Starting...";

            queue = new ConcurrentQueue<string>(File.ReadAllLines(textBox3.Text));

            threads = new List<Thread>();

            resultList = new List<Proxy>();

            for (int i = 0; i < numericUpDown1.Value; i++)
            {
                Thread thread = new Thread(() => {

                    while (continueTask)
                    {

                        if (queue.Count == 0)
                        {
                            continueTask = false;
                            break;
                        }

                        var proxy = "";
                        queue.TryDequeue(out proxy);
                        if (proxy != string.Empty)
                        {
                            try
                            {
                                Proxy result = new Whoer().Check2(proxy);

                                resultList.Add(result);

                                toolStripStatusLabel1.Text = result.Ip + "|" + result.Status;
                            }
                            catch (Exception)
                            {
                                //
                            }
                        }
                    }

                });

                thread.Start();
                threads.Add(thread);
            }

        }

        private void button4_Click(object sender, EventArgs e)
        {
            List<string> results = new List<string>();

            foreach (var item in resultList)
            {
                if (item != null)
                {
                    if (item.Status.ToLower() == "live")
                    {
                        if (checkBox1.Checked)
                        {
                            if (item.IsBlackListed == "Yes" || item.IsProxy == "YES")
                                return;
                        }

                        results.Add(item.ToString());
                    }
                }
            }

            File.WriteAllLines("results.txt", results.ToArray());
        }

        private void button2_Click(object sender, EventArgs e)
        {
            var open = new OpenFileDialog()
            {
                ValidateNames = true,
                Filter = "Text|*.txt"
            };

            if(open.ShowDialog() == DialogResult.OK)
            {
                textBox3.Text = open.FileName;
            }
        }

        private void button5_Click(object sender, EventArgs e)
        {
            if (threads != null)
            {
                foreach (var t in threads)
                {
                    if (t != null)
                    {
                        if (t.IsAlive)
                        {
                            t.Abort();
                            //MessageBox.Show("Wait all threads finish" + " " +t.ManagedThreadId.ToString());
                            //return;
                        }
                    }
                }
            }


            //if (threads != null)
            //{
            //    continueTask = false;

            //    toolStripStatusLabel1.Text = "Waiting...";
            //}
        }

        private void Form1_Resize(object sender, EventArgs e)
        {
            if (this.WindowState == FormWindowState.Minimized)
            {
                mynotifyicon.Visible = true;
                mynotifyicon.ShowBalloonTip(3000);
                this.ShowInTaskbar = false;
            }
        }

        private void mynotifyicon_MouseDoubleClick(object sender, MouseEventArgs e)
        {
            this.WindowState = FormWindowState.Normal;
            this.ShowInTaskbar = true;
            mynotifyicon.Visible = false;
        }
    }
}

0 个答案:

没有答案