Click here to Skip to main content
15,891,204 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hello, I'm generate password by therading but I don't know why it is not working,If i run this code without threading it is working otherwise i use thread like below it is not working properly, please guide me how can i use thread in this code.....


Thread th;

  private static string MakePassword(int pl)
        {
pl=50;
            string possibles =
            "0123456789";
            char[] passwords = new char[50];
            Random rd = new Random();

            {
                for (int i = 0; i<50; i++)
                {
                    passwords[i] = possibles[rd.Next(0, possibles.Length)];
                }
            }
            return new string(passwords);
        }


        public void ch()
        {
            try
            {

                            
                string temp = "";
                listView2.View = View.Details;
                listView2.Items.Clear();
                for (int j = 0; j<50; j++)
                {
                    if (j == 0)
                    {
                        temp = MakePassword(6);

                        listView2.GridLines = true;
                        listView2.Items.Add(temp.ToString());
                    }
                    else
                    {

                    g:
                        string k = MakePassword(6);
                        ListViewItem obj = listView2.FindItemWithText(k);


                        if (obj != null)
                        {
                            Refresh();
                            goto g;

                        }
                        else
                        {
                            if (listView2.Items.Count<50)
                            {

                                listView2.Items.Add(k.ToString());
                            }


                        }

                    }
                }
            }
            catch (NullReferenceException)
            { }
        }
        


      

        private void button1_Click(object sender, EventArgs e)
        {
           CheckForIllegalCrossThreadCalls = false;
            th = new Thread(ch);
            th.Start();
           
        }
    }
Posted
Updated 23-Mar-11 1:21am
v2

1 solution

Look, you aren't ready for threading yet: you don't understand enough about the normal code.
As JSOP and I explained yesterday, this is not suitable for threading.

Your new "threaded" version is particularly bad: you cannot access GUI objects from anywhere other than the thread than created them. So, you can't add items to a ListView from your thread without invoking them. I'll give you the code, but you won't understand it!

C#
private void AddNewString(string text)
    {
    if (InvokeRequired)
        {
        Invoke(new MethodInvoker(delegate { AddNewString(text); }));
        }
    else
        {
        myListView.Items.Add(text);
        }
    }
 
Share this answer
 
Comments
situ21 23-Mar-11 8:30am    
thanks sir
Sergey Alexandrovich Kryukov 23-Mar-11 17:46pm    
Good explanation and sample, a 5.
--SA
situ21 24-Mar-11 0:08am    
but sir it is not working,in my code
Sergey Alexandrovich Kryukov 24-Mar-11 0:12am    
It's not working because it's bad. Stop using goto and misusing threads. You code should not be improved but re-written.
--SA
situ21 24-Mar-11 0:59am    
how sir help me, I have to finish this today.

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