Click here to Skip to main content
15,887,875 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hi. I have a deep nested loop (about 30 deep) in windows form application C#.
when this program run,it goes into a Not Responding state. I am running this on a 64 bit machine.

C#
if (c22.Any())
                                                                                                                                                                                            {
                                                                                                                                                                                                for (int a22 = 0; a22 < c22.Count(); a22++)
                                                                                                                                                                                                {
                                                                                                                                                                                                    if (c23.Any())
                                                                                                                                                                                                    {
                                                                                                                                                                                                        for (int a23 = 0; a23 < c23.Count(); a23++)
                                                                                                                                                                                                        {
                                                                                                                                                                                                            if (c24.Any())
                                                                                                                                                                                                            {
                                                                                                                                                                                                                for (int a24 = 0; a24 < c24.Count(); a24++)
                                                                                                                                                                                                                {
                                                                                                                                                                                                                    if (c25.Any())
                                                                                                                                                                                                                    {
                                                                                                                                                                                                                        for (int a25 = 0; a25 < c25.Count(); a25++)
                                                                                                                                                                                                                        {
                                                                                                                                                                                                                            if (c26.Any())
                                                                                                                                                                                                                            {
                                                                                                                                                                                                                                for (int a26 = 0; a26 < c26.Count(); a26++)
                                                                                                                                                                                                                                {
                                                                                                                                                                                                                                    if (c27.Any())
                                                                                                                                                                                                                                    {
                                                                                                                                                                                                                                        for (int a27 = 0; a27 < c27.Count(); a27++)
                                                                                                                                                                                                                                        {
                                                                                                                                                                                                                                            if (c28.Any())
                                                                                                                                                                                                                                            {
                                                                                                                                                                                                                                                for (int a28 = 0; a28 < c28.Count(); a28++)
                                                                                                                                                                                                                                                {
                                                                                                                                                                                                                                                    if (c29.Any())
                                                                                                                                                                                                                                                    {
                                                                                                                                                                                                                                                        for (int a29 = 0; a8 < c29.Count(); a29++)
                                                                                                                                                                                                                                                        {
                                                                                                                                                                                                                                                            richTextBox1.Text += c0[a0] + "," + c1[a1] + "," + c2[a2] + "," + c3[a3] + "," + c4[a4] + "," + c5[a5] + "," + c6[a6] + "," + c7[a7] + "," + c8[a8] + "," + c9[a9] + "," + c10[a10]
                                                                                                                                                                                                                                                                + c11[a11] + "," + c12[a12] + "," + c13[a13] + "," + c14[a14] + "," + c15[a15] + "," + c16[a16] + "," + c17[a17] + "," + c18[a18] + "," + c19[a19] + "," + c20[a20]
                                                                                                                                                                                                                                                                + c21[a21] + "," + c22[a22] + "," + c23[a23] + "," + c24[a24] + "," + c25[a25] + "," + c26[a26] + "," + c27[a27] + "," + c28[a28] + "," + c29[a29] + "\n";
                                                                                                                                                                                                                                                        }
                                                                                                                                                                                                                                                    }
                                                                                                                                                                                                                                                    else
                                                                                                                                                                                                                                                        richTextBox1.Text += c0[a0] + "," + c1[a1] + "," + c2[a2] + "," + c3[a3] + "," + c4[a4] + "," + c5[a5] + "," + c6[a6] + "," + c7[a7] + "," + c8[a8] + "," + c9[a9] + "," + c10[a10]
                                                                                                                                                                                                                                                                 + c11[a11] + "," + c12[a12] + "," + c13[a13] + "," + c14[a14] + "," + c15[a15] + "," + c16[a16] + "," + c17[a17] + "," + c18[a18] + "," + c19[a19] + "," + c20[a20]
                                                                                                                                                                                                                                                                 + c21[a21] + "," + c22[a22] + "," + c23[a23] + "," + c24[a24] + "," + c25[a25] + "," + c26[a26] + "," + c27[a27] + "," + c28[a28] + "\n";

                                                                                                                                                                                                                                                }
                                                                                                                                                                                                                                            }
                                                                                                                                                                                                                                            else
                                                                                                                                                                                                                                                richTextBox1.Text += c0[a0] + "," + c1[a1] + "," + c2[a2] + "," + c3[a3] + "," + c4[a4] + "," + c5[a5] + "," + c6[a6] + "," + c7[a7] + "," + c8[a8] + "," + c9[a9] + "," + c10[a10]
                                                                                                                                                                                                                                                                 + c11[a11] + "," + c12[a12] + "," + c13[a13] + "," + c14[a14] + "," + c15[a15] + "," + c16[a16] + "," + c17[a17] + "," + c18[a18] + "," + c19[a19] + "," + c20[a20]
                                                                                                                                                                                                                                                                 + c21[a21] + "," + c22[a22] + "," + c23[a23] + "," + c24[a24] + "," + c25[a25] + "," + c26[a26] + "," + c27[a27] + "\n";
                                                                                                                                                                                                                                        }

                                                                                                                                                                                                                                    }
                                                                                                                                                                                                                                    else
                                                                                                                                                                                                                                        richTextBox1.Text += c0[a0] + "," + c1[a1] + "," + c2[a2] + "," + c3[a3] + "," + c4[a4] + "," + c5[a5] + "," + c6[a6] + "," + c7[a7] + "," + c8[a8] + "," + c9[a9] + "," + c10[a10]
                                                                                                                                                                                                                                                                 + c11[a11] + "," + c12[a12] + "," + c13[a13] + "," + c14[a14] + "," + c15[a15] + "," + c16[a16] + "," + c17[a17] + "," + c18[a18] + "," + c19[a19] + "," + c20[a20]
                                                                                                                                                                                                                                                                 + c21[a21] + "," + c22[a22] + "," + c23[a23] + "," + c24[a24] + "," + c25[a25] + "," + c26[a26] + "\n";
                                                                                                                                                                                                                                }
                                                                                                                                                                                                                            }
                                                                                                                                                                                                                            else
                                                                                                                                                                                                                                richTextBox1.Text += c0[a0] + "," + c1[a1] + "," + c2[a2] + "," + c3[a3] + "," + c4[a4] + "," + c5[a5] + "," + c6[a6] + "," + c7[a7] + "," + c8[a8] + "," + c9[a9] + "," + c10[a10]
                                                                                                                                                                                                                                                                 + c11[a11] + "," + c12[a12] + "," + c13[a13] + "," + c14[a14] + "," + c15[a15] + "," + c16[a16] + "," + c17[a17] + "," + c18[a18] + "," + c19[a19] + "," + c20[a20]
                                                                                                                                                                                                                                                                 + c21[a21] + "," + c22[a22] + "," + c23[a23] + "," + c24[a24] + "," + c25[a25] + "\n";
                                                                                                                                                                                                                        }
                                                                                                                                                                                                                    }
                                                                                                                                                                                                                    else
                                                                                                                                                                                                                        richTextBox1.Text += c0[a0] + "," + c1[a1] + "," + c2[a2] + "," + c3[a3] + "," + c4[a4] + "," + c5[a5] + "," + c6[a6] + "," + c7[a7] + "," + c8[a8] + "," + c9[a9] + "," + c10[a10]
                                                                                                                                                                                                                                                                 + c11[a11] + "," + c12[a12] + "," + c13[a13] + "," + c14[a14] + "," + c15[a15] + "," + c16[a16] + "," + c17[a17] + "," + c18[a18] + "," + c19[a19] + "," + c20[a20]
                                                                                                                                                                                                                                                                 + c21[a21] + "," + c22[a22] + "," + c23[a23] + "," + c24[a24] + "\n";
                                                                                                                                                                                                                }

                                                                                                                                                                                                            }
                                                                                                                                                                                                            else
                                                                                                                                                                                                                richTextBox1.Text += c0[a0] + "," + c1[a1] + "," + c2[a2] + "," + c3[a3] + "," + c4[a4] + "," + c5[a5] + "," + c6[a6] + "," + c7[a7] + "," + c8[a8] + "," + c9[a9] + "," + c10[a10]
                                                                                                                                                                                                                                                                 + c11[a11] + "," + c12[a12] + "," + c13[a13] + "," + c14[a14] + "," + c15[a15] + "," + c16[a16] + "," + c17[a17] + "," + c18[a18] + "," + c19[a19] + "," + c20[a20]
                                                                                                                                                                                                                                                                 + c21[a21] + "," + c22[a22] + "," + c23[a23] + "\n";
                                                                                                                                                                                                        }
                                                                                                                                                                                                    }
                                                                                                                                                                                                    else
                                                                                                                                                                                                        richTextBox1.Text += c0[a0] + "," + c1[a1] + "," + c2[a2] + "," + c3[a3] + "," + c4[a4] + "," + c5[a5] + "," + c6[a6] + "," + c7[a7] + "," + c8[a8] + "," + c9[a9] + "," + c10[a10]
                                                                                                                                                                                                                                                                 + c11[a11] + "," + c12[a12] + "," + c13[a13] + "," + c14[a14] + "," + c15[a15] + "," + c16[a16] + "," + c17[a17] + "," + c18[a18] + "," + c19[a19] + "," + c20[a20]
                                                                                                                                                                                                                                                                 + c21[a21] + "," + c22[a22] + "\n";
                                                                                                                                                                                                }


What I have tried:

If i run it in multi thread, text of richTextBox will not true.
Posted
Updated 15-Oct-17 9:09am
Comments
A_Griffin 15-Oct-17 14:55pm    
I imagine it's a memory issue. But seriously... you need to re-think your code design. Whatever you're trying to do here, there is a better way, I promise you.
Ma.Kabiri 15-Oct-17 15:05pm    
@A_Griffin, I have a 2d array[8*30] that contains the letters of the alphabet.
For example if the input is {{a,b,c},{d,e,f},{g,h,i}}
I want to print the output as follows: abc,abf,abi,aec,aef,aei,ahc,ahf,ahi,dbc,dbf,... and etc.

1 solution

Your UI runs on thread, and unless you move your long running loop into a separate thread, the UI cannot respond to events and becomes unresponsive. There is no way to avoid that except to move the loops into a different thread.

But as you have noticed, you can only access UI controls from the UI thread - if you try to access them from a different thread,you will get a cross-thread exception.

There are two way round this:
1) Use Invoke to move the control related code (and only that code) back to the UI thread:
Control.Invoke Method (Delegate) (System.Windows.Forms)[^]
2) Use a thread that allows you to "report back" to the UI thread and let it handle the controls. The BackgroundWorker Class (System.ComponentModel)[^] provides this via progress reporting, which lets you pass an object - the text to add via the event args.

Me? I'd use the BackgroundWorker.
 
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