Skip to main content
 首页 » 编程设计

c#之在 C# 中尝试多线程

2024年02月06日13jillzhang

我只是想尝试多线程应用程序并检查使用多线程时性能如何。

但是我也不知道是我做错了还是理解错了!我是编程的业余爱好者,甚至是初学者。

因为在普通模式下(不使用线程)它需要更少的时间来完成这个过程!例如:

使用线程:02.8500253 但不使用线程:02.5455425

有时差异更大!

我的问题是:我是不是做错了,或者我误解了多线程等等。我想知道哪里出了问题?为什么这里没有线程更快!?

完整代码如下:

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; 
using System.Threading; 
using System.Diagnostics; 
namespace WindowsFormsApplication57 
{ 
public partial class Form1 : Form 
{ 
    public Form1() 
    { 
        InitializeComponent(); 
    } 
 
    Stopwatch sw = new Stopwatch(); 
 
    private void button1_Click(object sender, EventArgs e) 
    { 
    //    System.Windows.Forms.Timer mytimer = new System.Windows.Forms.Timer(); 
   //     mytimer.Interval = 1; 
        sw.Reset(); 
 
        sw.Start(); 
 
        Thread thread1 = new Thread(new ThreadStart(myfunction)); 
        thread1.Start(); 
        Thread thread2 = new Thread(new ThreadStart(myfunction)); 
        thread2.Start(); 
 
       // sw.Stop(); 
      //  for (int i = 0; i < mylist.Count; i++)  
     //   { 
    //        listBox1.Items.Add(mylist[i]); 
    //    } 
        //listBox1.Items.Add 
        //label1.Text = string.Format("Elapsed Time Using MultiThread= {0}", sw.Elapsed); 
 
   //     mytimer.Enabled = true; 
    } 
 
    List<string> mylist = new List<string>(); 
 
    void myfunction() 
    {     
 
        for (int i = 0; i < 10000; i++)  
        { 
            mylist.Add(i.ToString()); 
 
     //       listBox1.Items.Add(i); 
            if (listBox1.InvokeRequired) 
            { 
                listBox1.Invoke(new MethodInvoker(delegate { listBox1.Items.Add(i); })); 
            } 
            else { listBox1.Items.Add(i); } 
        } 
        if (mylist.Count == 20000) 
        { 
            sw.Stop(); 
            if (label1.InvokeRequired) 
            { 
                label1.Invoke(new MethodInvoker(delegate { label1.Text = string.Format("Elapsed Time Using MultiThread= {0}", sw.Elapsed); })); 
            } 
            else 
            { 
                label1.Text = string.Format("Elapsed Time Using MultiThread= {0}", sw.Elapsed); 
            } 
        } 
    } 
 
    private void button2_Click(object sender, EventArgs e) 
    { 
        //Stopwatch sw = new Stopwatch(); 
        sw.Reset(); 
        sw.Start(); 
 
        myfunction(); 
        myfunction(); 
 
        //sw.Stop(); 
    //    for (int i = 0; i < mylist.Count; i++) 
    //    { 
   //         listBox1.Items.Add(mylist[i]); 
   //     } 
        label1.Text = string.Format("Elapsed Time WITHOUT MultiThread= {0}", sw.Elapsed); 
    } 
 
    private void button3_Click(object sender, EventArgs e) 
    { 
        mylist.Clear(); 
        listBox1.Items.Clear(); 
    } 
 
} 
} 

请您参考如下方法:

多线程不一定比单线程运行得更快,记住创建/调度线程需要很多 CPU 周期。例如,如果您在单核 CPU 中运行代码,多线程实际上会使它变慢 - 尽管单核 CPU 在现代 PC 上并不常见。

将 20000 个字符串添加到 mylist 只需要几毫秒,99% 的 CPU 时间花在 listBox1.Invoke 上。

在您的代码中,您调用 listBox1.Invoke 来编码对 UI 线程的调用,因此来自两个线程的代码 listBox1.Items.Add(i);最终在同一个 UI 线程上运行,与在单个线程上运行相比,这种方式没有显着改进(如果有的话)。

你可以试试这个 listBox1.Items.AddRange(mylist),它只被调用一次,而不是 20000 次。