Click here to Skip to main content
15,890,282 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
hi,
I just trying to print the 1 to 100 numbers in two list boxes parallel.But...After the first listbox only the second list box starts working.Is my coding has any problem..or multithreading is like this?Please rectify my mistakes in the coding..

C#
private void button1_Click(object sender, EventArgs e)
        {
            int i = 0;
            for (i = 0; i < 100; i++)
            {
                listBox1.Items.Add(i);
                Thread.Sleep(10);
            }
            int j = 0;
            for (j = 0; j < 100;j++)
            {
                listBox2.Items.Add(j);
                Thread.Sleep(10);
            }
          

        }

Thanks
Posted
Comments
Prerak Patel 20-Sep-11 7:11am    
This is not multithreading.
Anil Honey 206 20-Sep-11 7:14am    
i agree

What you are doing is not multithreading.
Check out the links to learn more about it
http://msdn.microsoft.com/en-us/library/aa645740(v=vs.71).aspx[^]
Multithreading in .NET[^]
Multithreading Concepts in C#[^]
 
Share this answer
 
Comments
Manfred Rudolf Bihy 20-Sep-11 7:14am    
Nice links!
Prerak Patel 20-Sep-11 7:17am    
Thanks :)
Sergey Alexandrovich Kryukov 20-Sep-11 18:04pm    
Good links, my 5.
However, OPs confusion is too deep (see my comment to the solution by Manfred :-).
I also added my solution to explain Invoke, please see.
--SA
Your two for loops are executed sequentially. Thread.Sleep only waits 10 ms in between adding two items. This has nothing to do with multithreaded programming. For that you'd have to produce two separate Threads and do the adding from there. Please make sure to use Control.Invoke when accessing GUI elements from within a non GUI thread.

Best Regards,

—MRB
 
Share this answer
 
Comments
Sergey Alexandrovich Kryukov 20-Sep-11 18:03pm    
Yes, it was a funny question. As if one could write some code, clap hands and chant "Multithreading, multithreading...". :-)
Hope you explained things. You also should have mentioned BeginInvoke and perhaps Dispatcher.

Anyway, I added some detail to it, please see my solution.
--SA
In addition to the solution by Manfred: please see my recent solution where I explain invocation using both Control and also Dispatcher: Is this correct coding?[^].

—SA
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900