ACE
ACE

Reputation: 387

Trying MultiThread in C#

I just wanted to try MultiThreading applications and check for how the performance would be if use multithread.

But I don't know either I did it wrong or I misunderstand it! I'm amateur or even beginner in Programming.

Because in Normal Mode(Without using Thread) it takes lower time to finish the process! for example:

With Using Thread: 02.8500253 But Without Using Thread: 02.5455425

Sometimes larger difference!

my question is: Have I done it wrong or i misunderstood multithreading or etc. i wanted to know what's wrong? why without thread is faster here!?

Here's whole Code:

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();
    }

}
}

Upvotes: 1

Views: 167

Answers (1)

kennyzx
kennyzx

Reputation: 12993

Multithreads does not necessarily run faster than a single thread, remember creating/scheduling threads takes many CPU cycles. For example, if you run your code in a single core CPU, multithreads actually makes it slower - although single core CPU is not common on modern PCs.

Adding 20000 strings into mylist takes only a few milliseconds, 99% of CPU time is spent on listBox1.Invoke.

In your code, you call listBox1.Invoke to marshal to the call to UI thread, so the code listBox1.Items.Add(i); from both threads is eventually running on the same UI thread, in this way there is no significant improvement (if any) over running on a single thread.

You can try this listBox1.Items.AddRange(mylist), this is only called once, instead of 20000 times.

Upvotes: 4

Related Questions