LOGO OA教程 ERP教程 模切知識(shí)交流 PMS教程 CRM教程 開發(fā)文檔 其他文檔  
 
網(wǎng)站管理員

[點(diǎn)晴永久免費(fèi)OA]C#多線程的啟動(dòng)與停止

admin
2022年11月25日 15:17 本文熱度 2926
文章簡(jiǎn)介:線程在開發(fā)中作用很大,也經(jīng)常用到,本文以Winfrom演示Redis隊(duì)列的案例,來看一看線程的啟動(dòng)和停止,已經(jīng)線程中操作winform的控件。

首先畫個(gè)winform界面


先看代碼

using System;
using System.Threading;
using System.Windows.Forms;
using EastWestWalk.NetFrameWork.Redis;
namespace Producer
{
    public partial class FrmMain : Form
    {
        private static string queueid = "MqId001";//隊(duì)列id
        private static bool IsStrat = false;//是否繼續(xù)生產(chǎn)信息
        private Thread StartThread = null;//生產(chǎn)線程
        private static bool IsEnd = false;//是否繼續(xù)消費(fèi)信息
        private Thread EndThread = null;//消費(fèi)線程
        public FrmMain()
        {
            InitializeComponent();
        }
        /// <summary>
        ///開始生產(chǎn)按鈕事件
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="EventArgs"/> instance containing the event data.</param>
        private void btn_start_Click(object sender, EventArgs e)
        {
            if (btn_start.Text == "開始生產(chǎn)")
            {
                IsStrat = true;
                StartThread = new Thread(EnqueueRun);
                StartThread.Start();
                btn_start.Text = "正在生產(chǎn)";
            }
            else
            {
                IsStrat = false;
                Thread.Sleep(50);//這里很重要 不然線程任務(wù)還沒結(jié)束 會(huì)報(bào)錯(cuò)
                if (StartThread != null && StartThread.ThreadState == ThreadState.Running)
                {
                    StartThread.Abort();
                    StartThread = null;
                }
                btn_start.Text = "開始生產(chǎn)";
            }
        }
        /// <summary>
        ///開始消費(fèi)按鈕事件
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="EventArgs"/> instance containing the event data.</param>
        private void btn_end_Click(object sender, EventArgs e)
        {
            if (btn_end.Text == "開始消費(fèi)")
            {
                IsEnd = true;
                EndThread = new Thread(DequeueRun);
                EndThread.Start();
                btn_end.Text = "正在消費(fèi)";
            }
            else
            {
                IsEnd = false;
                Thread.Sleep(50);
                if (EndThread != null && EndThread.ThreadState == ThreadState.Running)
                {
                    EndThread.Abort();
                    EndThread = null;
                }
                btn_end.Text = "開始消費(fèi)";
            }
        }
        /// <summary>
        /// 批量生產(chǎn)
        /// </summary>
        private void EnqueueRun()
        {
            using (var client = new DoRedisString(RedisUtility.RedisConfig))
            {
                int i = 0;
                while (IsStrat)
                {
                    string str = i + DateTime.Now.Ticks.ToString();
                    client.Core.EnqueueItemOnList(queueid, str);
                    txt_log.BeginInvoke(new Action(() =>
                    {
                        txt_log.AppendText($"生產(chǎn):{str}\n");
                        txt_log.selectionStart = txt_log.TextLength;
                        txt_log.ScrollToCaret();
                    }));
                    i++;
                    Thread.Sleep(20);
                }
            }
        }
        /// <summary>
        ///批量消費(fèi)
        /// </summary>
        private void DequeueRun()
        {
            using (var client = new DoRedisString(RedisUtility.RedisConfig))
            {
                while (IsEnd)
                {
                    if (client.Core.GetListCount(queueid) > 0)
                    {
                        string result = client.Core.DequeueItemfromList(queueid);
                        txt_log.BeginInvoke(new Action(() =>
                        {
                            txt_log.AppendText($"消費(fèi):{result}\n");
                            txt_log.selectionStart = txt_log.TextLength;
                            txt_log.ScrollToCaret();
                        }));
                        RedisUtility.SetRedis(result, $"消費(fèi)成功:{result}", DateTime.Now.AddSeconds(30));
                        Thread.Sleep(20);
                    }
                    else
                    {
                        //如果當(dāng)前隊(duì)列為空,掛起1s
                        Thread.Sleep(1000);
                        client.Core.EnqueueItemOnList(queueid, "0");
                    }
                }
            }
        }
    }
}

兩個(gè)線程,可以互不干擾的進(jìn)行快速生產(chǎn)和消費(fèi),而且不會(huì)卡主線程

跨線程操作控件

                        txt_log.BeginInvoke(new Action(() =>
                        {
                            txt_log.AppendText($"消費(fèi):{result}\n");
                            txt_log.selectionStart = txt_log.TextLength;
                            txt_log.ScrollToCaret();
                        }));

該文章在 2022/11/25 15:18:02 編輯過
關(guān)鍵字查詢
相關(guān)文章
正在查詢...
點(diǎn)晴ERP是一款針對(duì)中小制造業(yè)的專業(yè)生產(chǎn)管理軟件系統(tǒng),系統(tǒng)成熟度和易用性得到了國(guó)內(nèi)大量中小企業(yè)的青睞。
點(diǎn)晴PMS碼頭管理系統(tǒng)主要針對(duì)港口碼頭集裝箱與散貨日常運(yùn)作、調(diào)度、堆場(chǎng)、車隊(duì)、財(cái)務(wù)費(fèi)用、相關(guān)報(bào)表等業(yè)務(wù)管理,結(jié)合碼頭的業(yè)務(wù)特點(diǎn),圍繞調(diào)度、堆場(chǎng)作業(yè)而開發(fā)的。集技術(shù)的先進(jìn)性、管理的有效性于一體,是物流碼頭及其他港口類企業(yè)的高效ERP管理信息系統(tǒng)。
點(diǎn)晴WMS倉(cāng)儲(chǔ)管理系統(tǒng)提供了貨物產(chǎn)品管理,銷售管理,采購(gòu)管理,倉(cāng)儲(chǔ)管理,倉(cāng)庫(kù)管理,保質(zhì)期管理,貨位管理,庫(kù)位管理,生產(chǎn)管理,WMS管理系統(tǒng),標(biāo)簽打印,條形碼,二維碼管理,批號(hào)管理軟件。
點(diǎn)晴免費(fèi)OA是一款軟件和通用服務(wù)都免費(fèi),不限功能、不限時(shí)間、不限用戶的免費(fèi)OA協(xié)同辦公管理系統(tǒng)。
Copyright 2010-2025 ClickSun All Rights Reserved

黄频国产免费高清视频,久久不卡精品中文字幕一区,激情五月天AV电影在线观看,欧美国产韩国日本一区二区
亚洲综合网站精品一区二区 | 在线播放亚洲欧美日韩 | 亚洲无线乱码字幕观看 | 亚洲欧美日韩在线综合久 | 亚洲国产日韩欧美高清不卡 | 亚洲日韩欧美一区 |