通过TCP 发送消息给SERVER 验证合法性,怎么能在Application.Run之前就完成这些验证
如果不合格,用户只能看到一个消息框。我的代码会在load我的程序后 才作出验证,我写在load里面 最后退出的时候 也会闪一下我的 主窗体,怎么避免这样的问题
namespace TCPClient
{
/// <summary>
/// Form1 的摘要说明。
/// </summary>
public class Form1 : System.Windows.Forms.Form
{
private System.Windows.Forms.Button button1;
// private Thread thThread ; 
/// <summary>
/// 必需的设计器变量。
/// </summary>
private System.ComponentModel.Container components = null; public Form1()
{
//
// Windows 窗体设计器支持所必需的
//
InitializeComponent();// thThread = new Thread ( new ThreadStart( confirm ));
// thThread.Start();
//
// TODO: 在 InitializeComponent 调用后添加任何构造函数代码
//
} /// <summary>
/// 清理所有正在使用的资源。
/// </summary>
protected override void Dispose( bool disposing )
{
if( disposing )
{
if (components != null) 
{
components.Dispose();
}
}
base.Dispose( disposing );

} #region Windows 窗体设计器生成的代码
/// <summary>
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
/// 此方法的内容。
/// </summary>
private void InitializeComponent()
{
this.button1 = new System.Windows.Forms.Button();
this.SuspendLayout();
// 
// button1
// 
this.button1.Location = new System.Drawing.Point(170, 114);
this.button1.Name = "button1";
this.button1.Size = new System.Drawing.Size(132, 86);
this.button1.TabIndex = 0;
this.button1.Text = "button1";
this.button1.Click += new System.EventHandler(this.button1_Click);
// 
// Form1
// 
this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
this.ClientSize = new System.Drawing.Size(416, 357);
this.Controls.Add(this.button1);
this.Name = "Form1";
this.Text = "Form1";
this.Load += new System.EventHandler(this.Form1_Load);
this.ResumeLayout(false); }
#endregion /// <summary>
/// 应用程序的主入口点。
/// </summary>
[STAThread]
static void Main() 
{
Application.Run(new Form1());
} private const string hostName = "rock"; private void button1_Click(object sender, System.EventArgs e)
{
} private void confirm()
{
try 
{
TcpClient client = new TcpClient(hostName, 8000); NetworkStream ns = client.GetStream();
string szMsg = "messasge";
byte[] serverBytes = new byte[1024];
string szServerMsg = "";
byte[] bytes = System.Text.Encoding.ASCII.GetBytes(szMsg);

if (ns.CanWrite)
{
ns.Write(bytes,0,bytes.Length);
}
if (ns.CanRead)
{
int nRead = ns.Read(serverBytes,0,serverBytes.Length);
szServerMsg = 
String.Concat(szServerMsg,System.Text.Encoding.ASCII.GetString(serverBytes,0,nRead));
if (szServerMsg == "Accept")
MessageBox.Show("thx");
else
{
MessageBox.Show("sorry");
Application.Exit();
}
}
client.Close();

catch (Exception ex) 
{
Console.WriteLine(ex.ToString());
}
} private void Form1_Load(object sender, System.EventArgs e)
{
try 
{
TcpClient client = new TcpClient(hostName, 8000); NetworkStream ns = client.GetStream();
string szMsg = "messasge";
byte[] serverBytes = new byte[1024];
string szServerMsg = "";
byte[] bytes = System.Text.Encoding.ASCII.GetBytes(szMsg);

if (ns.CanWrite)
{
ns.Write(bytes,0,bytes.Length);
}
if (ns.CanRead)
{
int nRead = ns.Read(serverBytes,0,serverBytes.Length);
szServerMsg = 
String.Concat(szServerMsg,System.Text.Encoding.ASCII.GetString(serverBytes,0,nRead));
if (szServerMsg == "Accept")
MessageBox.Show("thx");
else
{
MessageBox.Show("sorry");
Application.Exit();
}
}
client.Close();

catch (Exception ex) 
{
Console.WriteLine(ex.ToString());
}
}
}
}