Form.Show()为“无响应..."; [英] Form.Show() is "Not Responding..."

查看:92
本文介绍了Form.Show()为“无响应...";的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好.我的应用程序中有一个小问题.我有一个对话框(d1),它继承自Form.在Main,如果我打电话给我

Hello. I have a little trivial problem in my application. I have a dialog (d1) that inherits from Form. And in Main, if I call

d1.ShowDialog()



,效果很好.但是打电话



,it works fine. But calling

d1.Show()



打开一个带有浅色标签和文本框的错误表单,当我尝试拖动该窗口表单时,它显示无响应".
这是怎么回事?我认为我需要显示"而不是"ShowDialog",因为用户应该同时处理几种形式.

谢谢.

这是CS文件的代码.它基本上是一个简单的表单,包含两个文本框,一个用于用户名和密码.没什么可看的...它仍然在Show中冻结.



opens a bad form with pale labels and textboxes, and when I try to drag that window form it says "Not Responding."
What''s going on? I think I need "Show" and not "ShowDialog," because the user should be dealing with several simultaneous forms.

Thanks.

Here''s the code for the CS file. It''s basically a simple form that contains two textboxes, one for username and password. Nothing fancy at all... It still freezes with Show.

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Reflection;
using System.Threading;
using System.Security.Cryptography.X509Certificates;

namespace MyConnection
{
    public partial class LoginDlg : Form
    {
        #region Constructors
        public LoginDlg()
        {
            InitializeComponent();
        }
        #endregion

        #region Private Fields
		String username;
		String password;
        #endregion

        #region Public Interface
		/// 
		/// Used for getting the username and password in a GUI environment.
		/// Directly modifies the username and password when the user clicks the OK button.
		/// Returns whether the user proceeds with the login process (clicks OK) or not (clicks Cancel).
		/// 
		public Boolean getUsernameAndPassword(ref String user, ref String pass)
		{
			if (ShowDialog() == DialogResult.OK)
			{
				user = username;
				pass = password;

				return true;
			}

			DialogResult = DialogResult.Cancel;
			return false;
		}
		#endregion

		#region Event Handlers
		private void OkBTN_Click(object sender, EventArgs e)
        {
            try
            {
				//String username = (String)UserNameCB.SelectedItem;
				username = (String)UserNameCB.SelectedItem;

				if (String.IsNullOrEmpty(username))
					username = UserNameCB.Text;

				//String password = PasswordTB.Text;
				password = PasswordTB.Text;

				DialogResult = DialogResult.OK;
            }
            catch (Exception exception)
            {
                GuiUtils.HandleException(this.Text, MethodBase.GetCurrentMethod(), exception);
            }
		#endregion
		}
    }
}

推荐答案

调用Show()后,您正在执行的操作会阻塞UI线程,这意味着此子窗体也将看起来仍然冻结. />

----------------
好的,这显然是行不通的.调用Show ,然后控制转到下一行,在该行中调用Thread.Sleep,该行在主线程(与创建并显示对话框的线程相同)上执行.所有UI都需要普通WinForms框架提供的某种消息泵,但是由于您使用控制台应用程序且未使用Application 类,因此您将需要实现消息泵或使用ShowDialog (实现内部消息泵).
After you call Show(), you are doing something that blocks the UI thread, which means this child form will remain seemingly frozen too.


----------------
Okay, it''s obvious that this will not work. You call Show and then control goes to the next line where you call Thread.Sleep which is executed on the main thread (the same thread that the dialog is created and shown in). All UI needs some kind of message pump which is provided by the normal WinForms framework, but since you use a console app and do not use the Application class, you will either need to implement a message pump or use ShowDialog (which implements an internal message pump).


好吧,他是执行此操作的另一种方法.您可以从控制台应用程序创建无模式表单,然后调用Application.Run.这是一个最小的代码片段,显示了如何完成此操作.

Alright he''s an alternate way to do this. You create your modeless forms from your console app, and then call Application.Run. Here''s a minimal code snippet that shows how this can be done.

namespace Test
{
    class Form1 : Form
    {
        public Form1()
        {
            Text = "This is form1";
        }
    }

    class Form2 : Form
    {
        public Form2()
        {
            Text = "This is form2";
        }
    }

    class Program
    {
        static void Main(string[] args)
        {
            new Form1().Show();
            new Form2().Show();
            Application.Run();
        }

    }
}


这篇关于Form.Show()为“无响应...";的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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