博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
C# 挂起恢复运行线程代码
阅读量:6787 次
发布时间:2019-06-26

本文共 4354 字,大约阅读时间需要 14 分钟。

//添加引用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;            }        }//线程调用函数///         /// 线程调用的监听转换数据表约束到简体版函数        ///         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(), "异常");                    }                }            }        }

 

转载地址:http://uqigo.baihongyu.com/

你可能感兴趣的文章
Java中ConcurrentHashMap的实现
查看>>
如何从零开始学习hadoop?
查看>>
有关tomcat的性能调优【待完善】
查看>>
QA和软件测试员的区别
查看>>
windows 批处理常用指令 -- 持续更新
查看>>
Jenkins+Shell+Docker实现自动化CI/CD发布Java项目
查看>>
【转】Java经典问题:传值与传引用?
查看>>
xp扩容C盘后盘符丢失的资料怎么找到
查看>>
CVE-2017-12617
查看>>
SSH管理服务
查看>>
废旧行业迎来春天 环保部重启绿色GDP研究
查看>>
监控——JVM监控安装
查看>>
Working with System Properties
查看>>
为什么我墙裂建议大家使用枚举来实现单例
查看>>
记一次阿里云服务器安装Python的血泪史
查看>>
c++函数模块与其相关内容
查看>>
ssh防止暴力破解之fail2ban
查看>>
LNMP基础安装配置
查看>>
借助Gradle Plugin解决模块化开发中模块如何对外暴露接口
查看>>
深圳车牌识别助力汽车检测,颠覆传统方式
查看>>