Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Rhinorhino on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Threadding

Status
Not open for further replies.

Ju1c3

MIS
Joined
Apr 3, 2007
Messages
43
Location
US
i have been using a program called angry ip scanner for awhile now to help me when i am working on networks. well i like it and i dont. i would like to make my own with a feature that angry doesnt have. i have the ping working and everything, i just would like to make it multithreaded so it will run faster. currently it takes 5 seconds for it to do 1 ip address so you can imagine what it takes to do 254. so, i am calling a class that does the pinging, i just would like to create like 30 threads or so so i am scanning more then one at a time. this is the code i use to call the class, now i would like to know how to make it threaded. thanks in advance. gold star to whoever helps me.

Code:
        private void button3_Click(object sender, EventArgs e)
        {

            int f4int = Convert.ToInt32(F4.Text);
            int t4int = Convert.ToInt32(T4.Text);

            for (int z = f4int; z <= t4int; z++)
            {
                Class1 cls = new Class1();

                string tmp;
\\where i call the class
                tmp = cls.Trd(z, F1.Text.ToString(), F2.Text.ToString(), F3.Text.ToString(), f4int, t4int);

                textBox1.AppendText(tmp);

                progressBar2.Increment(2);
            }
        }
 
i agree the background threads seem to be the best bet. i have been playing with them and googling all day trying to figure this out and i still cant seem to come up with an answer for it. i created the backgroundWorker in the for statement, trying to create multiples, and when i create it i call the trd thing to do my function. i am beginning to realise that the way i wrote this program in the first place may not lend itself easily to being multi-threaded, but its just so slow. i have turned the timeout down real far and everything and still its slow. hence what i am trying to do. i dont want you to rite the code for me, just maybe tell me how to do it and i will figure it out. i dont understand the concept for the doWork method. do i need to use it?



thanks man
 
I think you only need 1 BWP object. once you configure the start/change/completed events you keeps calling those functions instead of creating a new BWP each time you want to call a function async.

Jason Meckley
Programmer
Specialty Bakers, Inc.
 
am i way off line with this? i tried to put the call to the function in the () when i start the background worker but it only wants an object and not a function.

Code:
private void button3_Click(object sender, EventArgs e)
        {
            int line = 0;
            int f4int = Convert.ToInt32(F4.Text);
            int t4int = Convert.ToInt32(T4.Text);
            BackgroundWorker bk = new BackgroundWorker();

            for (int z = f4int; z <= t4int; z++)
            {

                Class1 cls = new Class1();
                string[] tmp = new string[2];
                BackgroundWorker bw = new BackgroundWorker();

                bk.RunWorkerAsync(); 
                tmp = cls.Trd(z, F1.Text.ToString(), F2.Text.ToString(), F3.Text.ToString(), f4int, t4int);


                    if (tmp[0].Equals("1"))
                    {
                        richTextBox1.AppendText(tmp[1]);
                        richTextBox1.Find(richTextBox1.Lines[line]);
                        richTextBox1.SelectionFont = new Font("Veranda", 10, FontStyle.Underline);
                        richTextBox1.SelectionColor = Color.Blue;
                        richTextBox1.Refresh();
                    }
                    else
                    {
                        richTextBox1.AppendText(tmp[1]);
                        richTextBox1.Find(richTextBox1.Lines[line]);
                        richTextBox1.SelectionFont = new Font("Veranda", 10, FontStyle.Underline);
                        richTextBox1.SelectionColor = Color.Red;
                        richTextBox1.Refresh();
                    }

                    line++;


            }
        }
 
here are some problems I see at first glance
1. BackgroundWorker bk = new BackgroundWorker();
declared but never used.
2. BackgroundWorker bw = new BackgroundWorker();
within loop
declared but never used.
3. neither varible has the event configured.

here are some links that may help.
ms example []
blog example []
more examples []

Jason Meckley
Programmer
Specialty Bakers, Inc.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top