Windows窗体进入无响应状态 [英] Windows forms entering not responding state

查看:56
本文介绍了Windows窗体进入无响应状态的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好。我在Windows窗体应用程序C#中有一个深嵌套循环(大约30深)。

当这个程序运行时,它进入Not Responding状态。我在64位机器上运行它。



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.

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.



What I have tried:

If i run it in multi thread, text of richTextBox will not true.

推荐答案

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.
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.


这篇关于Windows窗体进入无响应状态的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆