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


    //添加引用
    
    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(), "异常");
                        }
                    }
                }
            }
  • 相关阅读:
    webpack搭建vue项目,实现脚手架功能
    【学习笔记】JS设计模式总结
    【学习笔记】JS经典异步操作,从闭包到async/await
    【学习笔记】深入理解async/await
    【学习笔记】剖析MVVM框架,简单实现Vue数据双向绑定
    【学习笔记】node.js重构路由功能
    使用react-app-rewired和customize-cra 个性化配置
    axios 生产环境和开发环境 ip 切换(修改文件配置)
    socketio 更改requsturl 实现nginx 代理多个服务端
    vue-json-editor 简单实现
  • 原文地址:https://www.cnblogs.com/maijin/p/2824184.html
Copyright © 2020-2023  润新知