7 Replies - 695 Views - Last Post: 28 January 2013 - 10:16 AM Rate Topic: -----

#1 istore221  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 136
  • Joined: 11-December 11

MultiThreading Problem ?

Posted 28 January 2013 - 02:17 AM

private void button1_Click(object sender, EventArgs e)
        {


            Thread t_Artist = new Thread(loadArtist);
            Thread t_albums = new Thread(LoadAlbums);

            t_Artist.Start();
            t_albums.Start();



        }




        public void loadArtist()
        {

            SqlConnection sqlcon = new SqlConnection("Data Source=.;Initial Catalog=Local_System;Integrated Security=True");
            
            sqlcon.Open();

            SqlCommand com = new SqlCommand("SELECT Artist_Name FROM Artists", sqlcon);
            SqlDataReader reader;

            reader = com.ExecuteReader();

       

            


            while (reader.Read())
            {

                // You cant do like this
                //  comboBox1.Items.Add(reader.GetString(0))
                // because combobox1 is accses by another thread called UI thread (Main thread)
                // you can not use any control without using force invoke method

                comboBox1.Invoke(new MethodInvoker(delegate { comboBox1.Items.Add(reader.GetString(0));}));


            }


            sqlcon.Close();
        }



        public void LoadAlbums()
        {

            SqlConnection sqlcon = new SqlConnection("Data Source=.;Initial Catalog=Local_System;Integrated Security=True");

            sqlcon.Open();

            SqlCommand com = new SqlCommand("SELECT Album_Name from Albums", sqlcon);
            SqlDataReader reader;

            reader = com.ExecuteReader();

            while (reader.Read())
            {

                comboBox2.Invoke(new MethodInvoker(delegate { comboBox2.Items.Add(reader.GetString(0)); }));
                

            }

            sqlcon.Close();
        }
      






This is simple (Test) application created by me to load Artist Name and Albums from the database using Threads

i want to know what is actually threading does ?

i have created two threads

one thread for load artist name from database to combobox1

second thread for load Album name from database to combobox2

will this increase my program performance?

Will this two methods running at the same time ? (NOT one after another)

if it is not running at the same time how to do that ?

i think the threading capable of doing this

Method 1 --------------end
Method 2 --------------end




thank you in advance

This post has been edited by istore221: 28 January 2013 - 02:20 AM


Is This A Good Question/Topic? 0
  • +

Replies To: MultiThreading Problem ?

#2 Momerath  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1012
  • View blog
  • Posts: 2,444
  • Joined: 04-October 09

Re: MultiThreading Problem ?

Posted 28 January 2013 - 03:46 AM

Threading creates ... well ... threads that can be executed at the same time. If you have multiple cores (or processors) on your machine it will do so. This may increase your performance, decrease your performance or not change it at all (depends on a lot of factors about what the threads are doing and accessing).

It will attempt to run them at the same time, but again, there are other factors that come into play based on what you are having the threads do.

A good example of what looks like it is a good subject for multi-threading is the Quicksort algorithm. Since it partitions the items into two sections you'd think once it has done that, you can run two threads to deal with each section. The problem is that the time it takes the system to create and destroy threads makes the routine run slower (there is probably a place in the recursion where you can tell it to stop making threads because it's faster to just sort the small list you are working on, but that's a discussion for another thread :))
Was This Post Helpful? 0
  • +
  • -

#3 andrewsw  Icon User is offline

  • It's just been revoked!
  • member icon

Reputation: 3833
  • View blog
  • Posts: 13,580
  • Joined: 12-December 12

Re: MultiThreading Problem ?

Posted 28 January 2013 - 04:40 AM

Just to clarify that multi-threading does not require multiple-core processors, but without them you are less likely to see a benefit.

Quote

Threads running on the same core are not technically parallel. They only appear to be executed in parallel, as the CPU switches between them very fast (for us, humans). This switch is what is called context switch. Now, threads executing on different cores are executed in parallel.

Multi-threading on a single-core processor is, I suppose, more correctly referred to as multi-tasking (a term that has fallen out of fashion; well, in IT it has).

This post has been edited by andrewsw: 28 January 2013 - 04:47 AM

Was This Post Helpful? 0
  • +
  • -

#4 istore221  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 136
  • Joined: 11-December 11

Re: MultiThreading Problem ?

Posted 28 January 2013 - 05:29 AM

Okey thank you pros :)
Was This Post Helpful? 0
  • +
  • -

#5 Skydiver  Icon User is offline

  • Code herder
  • member icon

Reputation: 3662
  • View blog
  • Posts: 11,480
  • Joined: 05-May 12

Re: MultiThreading Problem ?

Posted 28 January 2013 - 05:58 AM

Although with a single core processor, you may not see the performance (aka speed) benefit of multiple threads, there are at times an inherent benefit where the code is easier to read and understand. Usually this applies when what a "thread" is doing is a single job or process that can be run independently.

This post has been edited by Skydiver: 28 January 2013 - 05:59 AM

Was This Post Helpful? 2
  • +
  • -

#6 andrewsw  Icon User is offline

  • It's just been revoked!
  • member icon

Reputation: 3833
  • View blog
  • Posts: 13,580
  • Joined: 12-December 12

Re: MultiThreading Problem ?

Posted 28 January 2013 - 06:57 AM

So, on this subject, we can say:

An application runs in a process;
(usually/initially in a single thread)
A process may have (spawn) many threads;
There can be a number of tasks;
These tasks can run in different threads;
Tasks can be moved between threads.

This post has been edited by andrewsw: 28 January 2013 - 07:37 AM

Was This Post Helpful? 0
  • +
  • -

#7 tlhIn`toq  Icon User is offline

  • Please show what you have already tried when asking a question.
  • member icon

Reputation: 5677
  • View blog
  • Posts: 12,208
  • Joined: 02-June 10

Re: MultiThreading Problem ?

Posted 28 January 2013 - 09:26 AM

Quote

Q:...do multi-threading? Having a problem with cross-threading...


Q: ...Why my multi-thread throws a cross-threading error when it tries to update my GUI?
A: Your thread shouldn't be trying to affect any GUI at all: Ever.
Spoiler

Was This Post Helpful? 0
  • +
  • -

#8 Curtis Rutland  Icon User is offline

  • (╯□)╯︵ (~ .o.)~
  • member icon


Reputation: 4577
  • View blog
  • Posts: 8,019
  • Joined: 08-June 10

Re: MultiThreading Problem ?

Posted 28 January 2013 - 10:16 AM

I realize this is linked in tlhIn`toq's post, but I want to highlight it here:

http://www.dreaminco...ui-environment/

The quality of this article is excellent. It shows you better ways to multithread, especially in a GUI. Creating your own tasks is doable, but tedious and often better left to the system to determine what tasks get threads. Read through the article and you should get a better understanding of more modern parallel programming.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1