为什么不能在另一个线程里打开一个新的Form?窗口闪了一下就消失了。
我在另一个线程中用f.Show()打开一个窗体,但这个窗体闪了一下就消失了。为什么?
如果我用f.ShowDialog()就是好的,但我想这不是正确的解决办法,请赐教!
问题点数:100、回复次数:10Top
1 楼yarshray(saga jion(心飘情落))回复于 2003-03-03 12:42:36 得分 20
f.Show()
表示,在另一个消息循环中打开
你的线程结束所以就消失
f.ShowDialog()
是和主线程在同一个循环中
所谓正确的解决办法是
你在主线程中调用Show()方法,你新的窗体就和主线程序的消息循环断开了
你可以在新窗体中处理独立的事物。Top
2 楼luckbird(luckbird)回复于 2003-03-03 12:58:07 得分 0
我开始是象你说的那样做的。但当我在新打开的Form中又用f.ShowDialog()打开另一个Form时,我的主窗口就不能操作了,必须把最后打开的这个Form关闭才能继续操作。如何解决这个问题?谢谢!Top
3 楼littleweed(万花丛中一根草)回复于 2003-03-03 13:02:17 得分 0
用ShowDialog()不就可以了么,还是你有什么特殊要求,也可以用用模态对话框Top
4 楼lin9703(LOVE[C#])回复于 2003-03-03 13:07:30 得分 20
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
namespace WindowsApplication8
{
/// <summary>
/// Form1 的摘要说明。
/// </summary>
public class Form1 : System.Windows.Forms.Form
{
/// <summary>
/// 必需的设计器变量。
/// </summary>
private System.ComponentModel.Container components = null;
public Form1()
{
//
// Windows 窗体设计器支持所必需的
//
InitializeComponent();
ShowSplash() ;
//让快闪程序出现
// TODO: 在 InitializeComponent 调用后添加任何构造函数代码
////
}
/// <summary>
/// 清理所有正在使用的资源。
/// </summary>
protected override void Dispose( bool disposing )
{
if( disposing )
{
if (components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}
#region Windows Form Designer generated code
/// <summary>
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
/// 此方法的内容。
/// </summary>
private void InitializeComponent()
{
//
// Form1
//
this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
this.ClientSize = new System.Drawing.Size(752, 437);
this.KeyPreview = true;
this.Name = "Form1";
this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen;
this.Text = "Form1";
}
#endregion
/// <summary>
/// 应用程序的主入口点。
/// </summary>
[STAThread]
static void Main()
{
Form local0;
// IAppStartupForm local1;
Form local2;
local0 = Form1.ShowSplash();
local2 = new Form1();
local2.Show();
local0.Owner = local2;
Application.DoEvents();
local0.Close();
local2.Enabled = true;
Application.DoEvents();
Application.Run(local2);
}
private static Form ShowSplash()
{
Form frmSplash;
PictureBox myPic;
Form frmSplashL;
frmSplash = new Form();
myPic = new PictureBox();
myPic.Dock = System.Windows.Forms.DockStyle.Fill ;
myPic.Image = Image.FromFile("E:\\Program Files\\JustSoft\\Xlsb.net\\Data\\Images\\splash.jpg");//你该个路径
frmSplash.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen;
frmSplash.FormBorderStyle = 0;
frmSplash.Size = myPic.Image.Size;
frmSplash.Controls.Add(myPic);
frmSplash.Show();
Application.DoEvents();
frmSplashL = frmSplash;
return frmSplashL;
}
}
}
这是我做的一个Top
5 楼lin9703(LOVE[C#])回复于 2003-03-03 13:10:28 得分 0
你主要看MAIN过程就行了!Top
6 楼QQ86087516(剑网)回复于 2003-03-03 16:14:30 得分 0
private void button1_Click(object sender, System.EventArgs e)
{
Thread m_thread1= new Thread(new System.Threading.ThreadStart(show));
m_thread1.Start();
}
public void show()
{
simple_dataAccess.Form4 fr4 =new simple_dataAccess.Form4();
fr4.ShowDialog();
}
这是我的代码,没有出什么问题啊,开多少个页面也可以,也没有象你说得那样,在新开的页面中在生成一个页面,主页面就不能动的问题.Top
7 楼QQ86087516(剑网)回复于 2003-03-03 16:27:14 得分 60
唉,老兄终于帮你搞定了
要加上Application.Doevent()
就像上面一样,新的线程中调用show()来显示一个新的form
在show中因该加上
while(1==1)
{
Application.DoEvent();
}
来防止子线程终止,这样就一切ok了,用form的show()方法就不会有你的那种现象了Top
8 楼accesine960(Transbuerg Tian)回复于 2003-03-03 16:31:40 得分 0
人: QQ86087516(快看!那里有只鸟._Oh Shit_')
你好,
#region 销售开单
//public System.Threading.Thread TDCurrent ;
private void mnuItemOrderManager_Click(object sender, System.EventArgs e)
{
using ( new UI.Common.GeneralMod.WaitCursor() )
{
Thread m_thread1= new Thread(new System.Threading.ThreadStart(OpenOrderManager));
m_thread1.Start();
}
}
private void OpenOrderManager()
{
Application.DoEvents();
Form frm=new UI.SellManager.frmOrderManager();
frm.MdiParent=this;
frm.Show();
}
#endregion
我的代码中有:
frm.MdiParent=this;
这句话不能执行,
你认为什么原因?
从理论上分析一下Top
9 楼QQ86087516(剑网)回复于 2003-03-03 16:52:12 得分 0
我用自己的form试了一下,用form.Midparent或是form.Parent=this
都没有问题
我觉的应该是UI.SellManager.frmOrderManager的定义有问题.你在检查一下Top
10 楼luckbird(luckbird)回复于 2003-03-04 08:33:12 得分 0
我觉得还是另建一个线程,用showdialog开窗口的方法比较好,按上面老兄说的用
while(1==1)
{
Application.DoEvent();
}
这样的方法,我觉得会增加系统的开销,影响系统的性能。Top




