最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

詳解C#如何解決程序卡頓的問(wèn)題(多線(xiàn)程初步學(xué)習(xí))

 更新時(shí)間:2024年04月19日 11:49:36   作者:漸暖°  
在編寫(xiě)程序的時(shí)候,有時(shí)候難免會(huì)出現(xiàn)后臺(tái)運(yùn)行時(shí)間過(guò)長(zhǎng)的問(wèn)題,這個(gè)時(shí)候就要考慮多線(xiàn)程的操作了,所以本文給大家介紹了C#解決程序卡頓問(wèn)題的方法,需要的朋友可以參考下

正文

不帶參數(shù)的多線(xiàn)程實(shí)現(xiàn)

第一步 建立控制臺(tái)應(yīng)用

在這里插入圖片描述

第二步 引用System.Threading.Thread

using System.Threading;

在 C# 中,System.Threading.Thread 類(lèi)用于線(xiàn)程的工作。它允許創(chuàng)建并訪(fǎng)問(wèn)多線(xiàn)程應(yīng)用程序中的單個(gè)線(xiàn)程。進(jìn)程中第一個(gè)被執(zhí)行的線(xiàn)程稱(chēng)為主線(xiàn)程。

第三步:完成代碼

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Threading;

namespace 多線(xiàn)程Test
{
    class Program
    {
        static void Main(string[] args)
        {
            int num = 100;
            for (int i = 0; i < num; i++)
            {
                //無(wú)參的多線(xiàn)程
                noParmaThread();
               
            }
        }
         private static void StartThread()

        {
            Console.WriteLine("------開(kāi)始了新線(xiàn)程------");
            Thread.Sleep(2000);//wait
            Console.WriteLine("------線(xiàn)程結(jié)束------");
        }

        /// <summary>
        ///不需要傳遞參數(shù)
        /// </summary>
        private static void noParmaThread()
        {
            ThreadStart threadStart = new ThreadStart(StartThread);
            var thread = new Thread(threadStart);
            thread.Start();//開(kāi)始線(xiàn)程
        }
    }
}

運(yùn)行結(jié)果

在這里插入圖片描述

拓展:C#多線(xiàn)程刷新界面卡死測(cè)試

背景

在上位機(jī)程序開(kāi)發(fā)過(guò)程中,不可避免的會(huì)用到多線(xiàn)程,如處理Socket通信、PLC讀取、界面數(shù)據(jù)實(shí)時(shí)刷新等。在某個(gè)項(xiàng)目中由于開(kāi)啟的線(xiàn)程很多,出現(xiàn)了不定期界面卡死狀況,但后臺(tái)線(xiàn)程還在運(yùn)行,日志還在輸出。為了解決這個(gè)問(wèn)題,特寫(xiě)了模擬程序進(jìn)行問(wèn)題復(fù)現(xiàn)。順便把過(guò)程分享一下。

要點(diǎn)

1、區(qū)分Control.BeginInvoke和Control.Invoke的用法

2、區(qū)分System.Timers.Timer、System.Threading.ThreadPool、System.Threading.Thread

Demo

創(chuàng)建一個(gè)WinForm應(yīng)用程序,把工程屬性的輸出類(lèi)型設(shè)置為控制臺(tái)應(yīng)用程序,方便運(yùn)行時(shí)查看日志。

關(guān)鍵代碼

BasicInvoker

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace WinApp
{
    /// <summary>
    /// 調(diào)用器
    /// </summary>
    public class BasicInvoker
    {
        public delegate void Invoke(); 

        private Form form = null;
        private TextBox txt = null;
        private object value = String.Empty;

        public BasicInvoker(Form form, TextBox txt, object value)
        {
            this.form = form;
            this.txt = txt;
            this.value = value;
        }

        public void SetValue()
        {
            if (this.form != null && !this.form.IsDisposed)
            {
                if (this.form.InvokeRequired)
                {
                    Delegate d = new Invoke(this.DoWork);
                    try
                    {
                        this.form.Invoke(d, null);
                        //this.form.BeginInvoke(d);
                    }
                    catch(Exception ex)
                    {
                        Console.WriteLine(ex.Message);
                    }
                }
                else
                {
                    this.DoWork();
                }
            }
        }

        public void DoWork()
        {
            if (this.txt != null)
            {
                this.txt.Text = this.value.ToString();
                Console.WriteLine(String.Format("{0:yyyy-MM-dd HH:mm:ss.fff}", DateTime.Now) + "  " +  this.value.ToString() + "...1");
                System.Threading.Thread.Sleep(200);
                Console.WriteLine(String.Format("{0:yyyy-MM-dd HH:mm:ss.fff}", DateTime.Now) + "  " + this.value.ToString() + "...2");
            }
        }
    }
}

FrmTest

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace WinApp
{
    public partial class FrmTester : Form
    {
        #region 變量定義

        private System.Timers.Timer timer1 = null;
        private System.Timers.Timer timer2 = null;
        private System.Timers.Timer timer3 = null;

        private System.Threading.Thread thread1 = null;
        private System.Threading.Thread thread2 = null;
        private System.Threading.Thread thread3 = null;

        #endregion

        #region 構(gòu)造方法

        public FrmTester()
        {
            InitializeComponent();
        }

        #endregion

        #region 事件處理

        private void Form1_Load(object sender, EventArgs e)
        {
            
        }

        private void button1_Click(object sender, EventArgs e)
        {
            if (timer1 == null)
            {
                timer1 = new System.Timers.Timer();
                timer1.Interval = 500;
                timer1.Elapsed += t1_Elapsed;
                timer1.Start();
            }
        }

        private void button2_Click(object sender, EventArgs e)
        {
            if (timer2 == null)
            {
                timer2 = new System.Timers.Timer();
                timer2.Interval = 500;
                timer2.Elapsed += t2_Elapsed;
                timer2.Start();
            }
        }        

        private void button3_Click(object sender, EventArgs e)
        {
            if (timer3 == null)
            {
                timer3 = new System.Timers.Timer();
                timer3.Interval = 500;
                timer3.Elapsed += t3_Elapsed;
                timer3.Start();
            }
        }

        private void button4_Click(object sender, EventArgs e)
        {
            System.Threading.ThreadPool.QueueUserWorkItem(new System.Threading.WaitCallback(this.CallBack1));
        }

        private void button5_Click(object sender, EventArgs e)
        {
            System.Threading.ThreadPool.QueueUserWorkItem(new System.Threading.WaitCallback(this.CallBack2));
        }

        private void button6_Click(object sender, EventArgs e)
        {
            System.Threading.ThreadPool.QueueUserWorkItem(new System.Threading.WaitCallback(this.CallBack3));
        }

        private void button7_Click(object sender, EventArgs e)
        {
            if (this.thread1 == null)
            {
                this.thread1 = new System.Threading.Thread(new System.Threading.ThreadStart(this.CallBack1));
                this.thread1.Start();
            }
        }

        private void button8_Click(object sender, EventArgs e)
        {
            if (this.thread2 == null)
            {
                this.thread2 = new System.Threading.Thread(new System.Threading.ThreadStart(this.CallBack2));
                this.thread2.Start();
            }
        }

        private void button9_Click(object sender, EventArgs e)
        {
            if (this.thread3 == null)
            {
                this.thread3 = new System.Threading.Thread(new System.Threading.ThreadStart(this.CallBack3));
                this.thread3.Start();
            }
        }

        private void Form1_FormClosing(object sender, FormClosingEventArgs e)
        {
            Console.WriteLine("FormClosing...");
            
            if (this.timer1 != null)
            {
                this.timer1.Stop();
                this.timer1.Dispose();
            }
            if (this.timer2 != null)
            {
                this.timer2.Stop();
                this.timer2.Dispose();
            }
            if (this.timer3 != null)
            {
                this.timer3.Stop();
                this.timer3.Dispose();
            }

            if (this.thread1 != null)
            {
                //if (this.thread1.ThreadState == System.Threading.ThreadState.Running)
                {
                    this.thread1.Abort();
                }
            }

            if (this.thread2 != null)
            {
                //if (this.thread2.ThreadState == System.Threading.ThreadState.Running)
                {
                    this.thread2.Abort();
                }
            }

            if (this.thread3 != null)
            {
                //if (this.thread3.ThreadState == System.Threading.ThreadState.Running)
                {
                    this.thread3.Abort();
                }
            }
        }

        private void Form1_FormClosed(object sender, FormClosedEventArgs e)
        {
            Console.WriteLine("FormClosed...");
        }  

        #endregion

        #region 定時(shí)處理方法定義

        private void t1_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
        {
            //lock(Global.PublicVar.Instance.Locker1)
            lock (String.Empty)
            {
                BasicInvoker invoker = new BasicInvoker(this, this.textBox1, Guid.NewGuid().ToString());
                invoker.SetValue();
            }
        }

        private void t2_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
        {
            lock (Global.PublicVar.Instance.Locker1)
            lock (String.Empty)
            {
                BasicInvoker invoker = new BasicInvoker(this, this.textBox1, "AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA");
                invoker.SetValue();
            }
        }

        private void t3_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
        {
            //lock (Global.PublicVar.Instance.Locker1)
            lock (String.Empty)
            {
                BasicInvoker invoker = new BasicInvoker(this, this.textBox1, "BBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBB");
                invoker.SetValue();
            }
        }

        public void CallBack1(object state)
        {
            while(true)
            {
                //lock (Global.PublicVar.Instance.Locker1)
                lock(String.Empty)
                {
                    BasicInvoker invoker = new BasicInvoker(this, this.textBox1, Guid.NewGuid().ToString());
                    invoker.SetValue();
                }
                System.Threading.Thread.Sleep(500);
            }
        }

        public void CallBack2(object state)
        {
            while (true)
            {
                //lock (Global.PublicVar.Instance.Locker1)
                lock(String.Empty)
                {
                    BasicInvoker invoker = new BasicInvoker(this, this.textBox1, "AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA");
                    invoker.SetValue();
                }
                System.Threading.Thread.Sleep(500);
            }
        }

        public void CallBack3(object state)
        {
            while (true)
            {
                //lock (Global.PublicVar.Instance.Locker1)
                lock(String.Empty)
                {
                    BasicInvoker invoker = new BasicInvoker(this, this.textBox1, "BBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBB");
                    invoker.SetValue();
                }
                System.Threading.Thread.Sleep(500);
            }
        }

        public void CallBack1()
        {
            while (true)
            {
                //lock (Global.PublicVar.Instance.Locker1)
                lock (String.Empty)
                {
                    BasicInvoker invoker = new BasicInvoker(this, this.textBox1, Guid.NewGuid().ToString());
                    invoker.SetValue();
                }
                System.Threading.Thread.Sleep(500);
            }
        }

        public void CallBack2()
        {
            while (true)
            {
                //lock (Global.PublicVar.Instance.Locker1)
                lock (String.Empty)
                {
                    BasicInvoker invoker = new BasicInvoker(this, this.textBox1, "AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA");
                    invoker.SetValue();
                }
                System.Threading.Thread.Sleep(500);
            }
        }

        public void CallBack3()
        {
            while (true)
            {
                //lock (Global.PublicVar.Instance.Locker1)
                lock (String.Empty)
                {
                    BasicInvoker invoker = new BasicInvoker(this, this.textBox1, "BBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBB");
                    invoker.SetValue();
                }
                System.Threading.Thread.Sleep(500);
            }
        }

        #endregion
    }
}

運(yùn)行圖:

運(yùn)行結(jié)果:

一、使用Control.BeginInvoke的情況

public void SetValue()
        {
            if (this.form != null && !this.form.IsDisposed)
            {
                if (this.form.InvokeRequired)
                {
                    Delegate d = new Invoke(this.DoWork);
                    try
                    {
                        //this.form.Invoke(d, null);
                        this.form.BeginInvoke(d);
                    }
                    catch(Exception ex)
                    {
                        Console.WriteLine(ex.Message);
                    }
                }
                else
                {
                    this.DoWork();
                }
            }
        }

1.1、System.Timers.Timer開(kāi)啟2以上很快界面卡死,日志正常輸出。

1.2、System.Threading.ThreadPool開(kāi)啟3個(gè)時(shí),運(yùn)行一會(huì)界面卡死,日志正常輸出。

1.3、System.Threading.Thread開(kāi)啟3個(gè)時(shí),運(yùn)行一會(huì)界面卡死,日志正常輸出。

二、使用Control.Invoke的情況

public void SetValue()
        {
            if (this.form != null && !this.form.IsDisposed)
            {
                if (this.form.InvokeRequired)
                {
                    Delegate d = new Invoke(this.DoWork);
                    try
                    {
                        this.form.Invoke(d, null);
                        //this.form.BeginInvoke(d);
                    }
                    catch(Exception ex)
                    {
                        Console.WriteLine(ex.Message);
                    }
                }
                else
                {
                    this.DoWork();
                }
            }
        }

2.1、System.Timers.Timer開(kāi)啟3個(gè)時(shí)很快界面卡死,日志正常輸出。

2.2、System.Threading.ThreadPool開(kāi)啟3個(gè)時(shí),界面正常操作,日志正常輸出。

2.3、System.Threading.Thread開(kāi)啟3個(gè)時(shí),界面正常操作,日志正常輸出。

測(cè)試總結(jié):

1、System.Threading.ThreadPool與System.Threading.Thread運(yùn)行效果基本相同。

2、在多線(xiàn)程刷新界面時(shí)盡量通過(guò)Control.Invoke調(diào)用委托實(shí)現(xiàn)。

小結(jié)

到此這篇關(guān)于詳解C#如何解決程序卡頓的問(wèn)題(多線(xiàn)程初步學(xué)習(xí))的文章就介紹到這了,更多相關(guān)C#解決程序卡頓內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

最新評(píng)論

江油市| 保康县| 镇平县| 吴旗县| 建昌县| 安庆市| 内丘县| 永登县| 辉南县| 姚安县| 内江市| 彩票| 攀枝花市| 安徽省| 西吉县| 全椒县| 临汾市| 娱乐| 西丰县| 城步| 民权县| 巴里| 莲花县| 潜山县| 南京市| 门头沟区| 罗定市| 新余市| 吐鲁番市| 梅州市| 岱山县| 永和县| 临城县| 海阳市| 宜城市| 报价| 湖州市| 乌海市| 张家港市| 桓台县| 临夏县|