C# 挂起恢复运行线程代码

//C# 挂起恢复运行线程代码 暂时没找到和测试出替换Thread.Resume()恢复运行和Thread.Suspend()暂停挂起的函数

//添加引用

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;

using System.Threading;
using System.Collections;

//定义变量

private Thread convertThread;
private Mutex mutex = new Mutex();
Queue myThreadQueue=new Queue();

//按钮事件

private void btnConstraintToBig5_Click(object sender, EventArgs e)
        {
            this.btnConstraintToBig5.Enabled = false;
           
           
            //测试数据库连接
            this.richTextBox1.Text = "";
            CurrentServerName = this.txtServerName.Text.Trim();
            CurrentDatabaseName = this.txt_DatabaseName.Text.Trim();
            CurrentUserName = this.txt_UserName.Text.Trim();
            CurrentUserPassword = this.txt_Password.Text.Trim();
            CurrentConnectionString = "Data Source=" + CurrentServerName + ";Initial Catalog=Master;User ID=" + CurrentUserName + ";password=" + CurrentUserPassword;

            DataTable myDataTable = new DataTable();
            try
            {
                SqlConnection thisConnection = new SqlConnection(CurrentConnectionString);
                SqlDataAdapter da = new SqlDataAdapter();
                da.SelectCommand = new SqlCommand("select * from sysservers", thisConnection);
                DataSet ds = new DataSet();
                da.Fill(ds, "Temp");
                myDataTable = ds.Tables["Temp"];
                thisConnection.Close();
                if (myDataTable.Rows.Count > 0)
                {
                    this.richTextBox1.Text += "/r/n建立到数据库连接成功";
                    CurrentConnectionString = "Data Source=" + CurrentServerName + ";Initial Catalog=" + CurrentDatabaseName + ";User ID=" + CurrentUserName + ";password=" + CurrentUserPassword;
                    convertThread = new Thread(new ThreadStart(StartConvertConstraintToBig5));
                    convertThread.IsBackground = true;
                    convertThread.Start();
                }
                else
                {
                    this.richTextBox1.Text += "/r/n建立到数据库连接失败";
                }
            }
            catch (Exception ex)
            {
                this.richTextBox1.Text += "/r/n建立到数据库连接出错" + ex.Message;
            }
        }

//线程调用函数

/// <summary>
        /// 线程调用的监听转换数据表约束到简体版函数
        /// </summary>
        private void StartConvertConstraintToBig5()
        {
            lock (myThreadQueue)
            {
                mutex.WaitOne();
                myDataBaseConverter.BeginConvertingMessage += new DataBaseEventHandler(OnBeginMessageReceived);
                myDataBaseConverter.EndConvertingMessage += new DataBaseEventHandler(OnEndMessageReceived);
                myDataBaseConverter.CurrentConvertingMessage += new DataBaseEventHandler(OnCurrentMessageReceived);
                myDataBaseConverter.ConvertingErrorMessage += new DataBaseEventHandler(OnErrorMessageReceived);
                myDataBaseConverter.ConvertTableConstraintToBig5(CurrentConnectionString);
                mutex.ReleaseMutex();
            }
        }      

//暂停恢复线程运行

private void btnPauseConverting_Click(object sender, EventArgs e)
        {
            if (convertThread != null)
            {
                Console.WriteLine("当前线程状态名称:" + convertThread.ThreadState.ToString());
                Console.WriteLine("当前线程状态编号:" + Convert.ToInt32(convertThread.ThreadState).ToString());
               
                if (this.btnPauseConverting.Text == "继续转换" && Convert.ToInt32(convertThread.ThreadState) == 68)
                {
                    try
                    {
                        this.btnPauseConverting.Text = "暂停转换";
                        convertThread.Resume();
                    }
                    catch (Exception msg)
                    {
                        MessageBox.Show(msg.ToString(), "异常");
                    }
                }
                else if (this.btnPauseConverting.Text == "暂停转换" && Convert.ToInt32(convertThread.ThreadState) == 36)
                {
                    try
                    {
                        this.btnPauseConverting.Text = "继续转换";
                        convertThread.Suspend();
                    }
                    catch (Exception msg)
                    {
                        MessageBox.Show(msg.ToString(), "异常");
                    }
                }
            }
        }

原文地址:https://www.cnblogs.com/xqf222/p/3306813.html