Hello!

I have this code:

Thread[] array = new Thread[size];
            Checker[] myclass = new Checker[size];
            ThreadStart[] startth = new ThreadStart[size];

            int limit = totalitem / size;

            for (int k = 0; k < size; k++)
            {

                myclass[k] = new Checker(this);
                myclass[k].pattern = pattern;
               
                startth[k] = delegate { myclass[k].DoChecking(limit * k, url_list, size); };
                array[k] = new Thread(startth[k]);
                array[k].IsBackground = true;
                array[k].Start();
                Thread.Sleep(100);

                if (!array[k].IsAlive)
                {
                    array[k].Join();
                }

            }

But when I run it UI crashes so it's no good. Do you have any idea how I can run same code, but in background so it wont use UI thread and doesn't crash UI.

Recommended Answers

All 8 Replies

You are blocking the GUI thread with the array[k].Join() call. Why are you calling Join() at all?

Yopu can use a BackgrounWorker. See some example on how to here

Hope this helps

Well, if I remove that Join it still doesn't want to work correctly. Also I tried that background worker and somehow it didn't want to work.

You don't really say what happens when you run your code. Does the UI generate an error or just stop responding? What do you mean by "still doesn't want to work correctly"? What does it do, what do you want it to do? How IO intensive is your DoChecking method?

Well, it just stops responding.
I want that I'm able to click "Abort" button, but I can't because UI stops responding.

How IO intensive is dochecking?
I really don't understand that question.

Imagine:

Your size parameter is 1024.

You start a loop to launch 1024 threads.
After each thread is started, you freeze your thread working for 100 miliseconds.
Then you try to finalize the launched thread and freeze yours until the thread finishes.
And start over for 1024 times.

I would suggest:
1) instead of freezing your application for 100 miliseconds, replace it by a System.Windows.Forms.Application.Doevents, to allow others threads to start processing.
2) Move the is alive and join out of the launch loop.
3) start a new loop for joining the threads.
4) before and after the join sentence, put a new doevents.

Hopefully this will 'unfreeze' (almost a little bit) your application.

Hope this helps

Thread[] array = new Thread[size];
            Checker[] myclass = new Checker[size];
            ThreadStart[] startth = new ThreadStart[size];
            
            int limit = totalitem / size;

            for (int k = 0; k < size; k++)
            {
                myclass[k] = new Checker(this);
                myclass[k].pattern = pattern;

                //ParameterizedThreadStart start = new ParameterizedThreadStart(myclass[k].DoChecking(limit * k, url_list, totalitem));                
                startth[k] = delegate { myclass[k].DoChecking(limit * k, url_list, size); };
                array[k] = new Thread(startth[k]);
                array[k].IsBackground = true;
                array[k].Start();
                Thread.Sleep(100);
                Application.DoEvents();
            }

            for (int k = 0; k < size; k++)
            {
                Application.DoEvents();
                array[k].Join();
                Application.DoEvents();
            }

This is what I tried, but UI still freezes :/

Use thread pooling with callbacks.

It seems like Checker runs a linear process that you want the results from when it is completed.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.