winform 的 show 和 ShowDialog 的问题, 高分求解

jcaomao 2004-12-22 03:14:50
我的程序里面用socket异步通讯,其中数据到达的时候出了问题。

我首先定义了一个事件, 然后在主线程窗体的formload里面把自己的一个方法注册到这个事件中,然后在socket的异步通讯线程里面数据到达的时候 激活这个事件。

主线程窗体里的那个方法只是 new 了一个空窗体。

可是如果事件激活的时候,我的主窗体正用ShowDialog打开另外一个窗体, 这样那个方法里面的new出来的窗体就好像是死循环一样。 如果那个方法里面new 出来的窗体也是用 ShowDialog 方法打开的话居然不起作用,主窗体还能够访问,并且新打开的窗体里面的控件绑定都失效了。

那位大侠能救救我啊?
...全文
285 14 打赏 收藏 转发到动态 举报
写回复
用AI写文章
14 条回复
切换为时间正序
请发表友善的回复…
发表回复
cxyPioneer 2004-12-24
  • 打赏
  • 举报
回复
up
51106354 2004-12-24
  • 打赏
  • 举报
回复
DotNetFreak() 写的不错,改一下应该就可以了,不行就获得窗体句柄做判断,或者遍历主线程上实例化的类.
ClampHammer 2004-12-24
  • 打赏
  • 举报
回复
学习了
DotNetFreak 2004-12-22
  • 打赏
  • 举报
回复
用了一个syncRoot object 来防止ShowDialog 的时候,Socket 完成了也要Show new Form, 这种情况的话,要等MessageBox 完成了, new Form 才会出现
DotNetFreak 2004-12-22
  • 打赏
  • 举报
回复
写了个例子,跟你描述的差不多,只是用Thread.Sleep(5000) 来代替了你的Socket call

using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using System.Threading;

namespace WindowsApplication1
{
public delegate void MyHandler(string msg);

/// <summary>
/// Summary description for Form1.
/// </summary>
public class Form1 : System.Windows.Forms.Form
{
private System.Windows.Forms.Button button1;
private System.Windows.Forms.Button button2;
public event MyHandler OnMsgArrived;

/// <summary>
/// Required designer variable.
/// </summary>
private System.ComponentModel.Container components = null;

public Form1()
{
//
// Required for Windows Form Designer support
//
InitializeComponent();

this.OnMsgArrived += new MyHandler(Form1_OnMsgArrived);

del = new EventHandler(SimulateSocketLongOperation);
}

/// <summary>
/// Clean up any resources being used.
/// </summary>
protected override void Dispose( bool disposing )
{
if( disposing )
{
if (components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}

#region Windows Form Designer generated code
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this.button1 = new System.Windows.Forms.Button();
this.button2 = new System.Windows.Forms.Button();
this.SuspendLayout();
//
// button1
//
this.button1.Location = new System.Drawing.Point(56, 24);
this.button1.Name = "button1";
this.button1.TabIndex = 0;
this.button1.Text = "button1";
this.button1.Click += new System.EventHandler(this.button1_Click);
//
// button2
//
this.button2.Location = new System.Drawing.Point(64, 72);
this.button2.Name = "button2";
this.button2.TabIndex = 1;
this.button2.Text = "button2";
this.button2.Click += new System.EventHandler(this.button2_Click);
//
// Form1
//
this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
this.ClientSize = new System.Drawing.Size(292, 266);
this.Controls.Add(this.button2);
this.Controls.Add(this.button1);
this.Name = "Form1";
this.Text = "Form1";
this.ResumeLayout(false);

}
#endregion

/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main()
{
Application.Run(new Form1());
}

private object syncRoot = new object();
private EventHandler del;

private void Form1_OnMsgArrived(string msg)
{
lock( syncRoot )
{
this.Invoke(new MyHandler(NewFormInvoke), new object[]{msg});
}
}

private void NewFormInvoke(string msg)
{
Console.WriteLine(msg);

new Form().Show();
}

private void button1_Click(object sender, System.EventArgs e)
{
del.BeginInvoke(this,EventArgs.Empty,new AsyncCallback(CallBack), "Hello");
}

private void CallBack(IAsyncResult ar)
{
del.EndInvoke(ar);

if( OnMsgArrived!=null )
OnMsgArrived(ar.AsyncState.ToString());
}

private void SimulateSocketLongOperation(object sender, EventArgs e)
{
Thread.Sleep(5000);
}

private void button2_Click(object sender, System.EventArgs e)
{
lock( syncRoot )
{
MessageBox.Show("Show MsgBox");
}
}
}
}
xiaoslong 2004-12-22
  • 打赏
  • 举报
回复
up
egxsun 2004-12-22
  • 打赏
  • 举报
回复
对窗体主线程用公共委托:
public delegate void DDD(string returnVal);
在代码里:
sting[] args=new string[]{"11"};
DDD UIDe=new DDD(yourF);

this.Invoke(UIDe,args);
...

void yourF(string result)
{

}
luoshulin 2004-12-22
  • 打赏
  • 举报
回复
哇200分,可是我会,学习ing
jin_xiaolu2000 2004-12-22
  • 打赏
  • 举报
回复
学习ING,不是很清楚
xiaomatian 2004-12-22
  • 打赏
  • 举报
回复
学习
北京的雾霾天 2004-12-22
  • 打赏
  • 举报
回复
我觉得你的问题出在了在不同的线程里打开了窗体而使窗体的显示出现了异常的情况,你如果要在另一个线程里打开窗体,可以用this.Invoke的方法执行打开窗体的那个方法,具体你可以参见MSDN对Invoke的说明.
cendrillon 2004-12-22
  • 打赏
  • 举报
回复
没看明白,说得再具体点
fellowcheng 2004-12-22
  • 打赏
  • 举报
回复
up
kong19 2004-12-22
  • 打赏
  • 举报
回复
up

110,538

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧