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