winform 的 show 和 ShowDialog 的问题, 高分求解 我觉得你的问题出在了在不同的线程里打开了窗体而使窗体的显示出现了异常的情况,你如果要在另一个线程里打开窗体,可以用this.Invoke的方法执行打开窗体的那个方法,具体你可以参见MSDN对Invoke的说明. 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 对窗体主线程用公共委托:public delegate void DDD(string returnVal);在代码里:sting[] args=new string[]{"11"};DDD UIDe=new DDD(yourF);this.Invoke(UIDe,args);...void yourF(string result){} 写了个例子,跟你描述的差不多,只是用Thread.Sleep(5000) 来代替了你的Socket callusing 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"); } } }} 用了一个syncRoot object 来防止ShowDialog 的时候,Socket 完成了也要Show new Form, 这种情况的话,要等MessageBox 完成了, new Form 才会出现 DotNetFreak() 写的不错,改一下应该就可以了,不行就获得窗体句柄做判断,或者遍历主线程上实例化的类. “Aceess数据库”和“Ria WCF Service” datatable赋值时索引超出范围 C# 数组问题 初学C#,请教如何运用命令行编译cs文件 求助!一个小问题,不知道能不能实现?? 帮忙看看这段代码 怎么用socket发送指定的stream里的东西??? remoting可以象ftp那样传文件吗? 帮看看哪里出错了 大虾们 救救小菜 C#怎么生成带参数的构造方法 WinForm中大数据量的显示问题 智能客户端自动更新的问题。请教大家!
public delegate void DDD(string returnVal);
在代码里:
sting[] args=new string[]{"11"};
DDD UIDe=new DDD(yourF);this.Invoke(UIDe,args);
...void yourF(string result)
{}
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");
}
}
}
}