登录界面在无法连接服务器时,界面会假死,如何解决?
解决方案 »
- C#中关于映射网络驱动
- 我想把一个大的文件存入Image类型的字段中,要分断段读取,免得内存占用太多,怎么办,给个代码吧
- 怎样才能把编译后的C#.NET程序转到别的PC机上运行?
- 这样的winform怎么做得特别绚丽花哨
- 急救急救,PrintDocument转换报错
- 怎样得到一个变量的数据类型?
- 对datagrid中显示的值直接进行修改,然后保存到数据库该怎么操作?
- c#写了一个windows服务程序,弹出“应用程序生成了一个未未处理的异常”
- (急)使用sql2005做的vs2005项目在用户那用安装算sql2005吗。
- 开始学C#.net 装上了六张盘后,开始学习,可是.........
- DataGridView 新加行的默认值的设定
- c#写的oracle操作类库,里面的方法要返回什么类型的值才能被c++程序使用呢
举个例子:connectiontimeout=5
Data Source=localhost;Initial Catalog=DataClient;User ID=aaa;Password=aaa;connectiontimeout=5
能否提供点源码?
WinForm程序,登录成功后需要show出主界面,怎么处理的?
if (login.ShowDialog() == DialogResult.OK)
{
Application.Run(new MainWindow());
}
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
} public event EventHandler timeOutEvent;
private System.Windows.Forms.Timer timer = null;
private string user = null;
private string pwd = null;
Thread loginThread = null;
private bool loginSucceed = false; //登陆按钮
private void button1_Click(object sender, EventArgs e)
{
this.user = textBox1.Text;
this.pwd = textBox2.Text;
loginThread = new Thread(new ThreadStart(CheckLogin));
loginThread.IsBackground = true;
loginThread.Start();
timer.Start();
} private void Form1_Load(object sender, EventArgs e)
{
this.timeOutEvent += new EventHandler(LoginResult);
timer = new System.Windows.Forms.Timer();
timer.Interval = 10000;//10秒
timer.Tick += new EventHandler(TimeOutEvent);
} private void LoginResult(object sender, EventArgs e)
{
this.label1.Text = loginSucceed ? "登陆成功" : "登陆失败";
} private void TimeOutEvent(object sender, EventArgs e)
{
timer.Stop();
if (loginThread.IsAlive)
{
loginThread.Abort();
} this.loginSucceed = false;
FireEvent();
} /// <summary>
/// 引发登陆操作完成的事件
/// </summary>
private void FireEvent()
{
if (timeOutEvent != null)
{
Delegate[] list = timeOutEvent.GetInvocationList();
foreach (Delegate c in list)
{
//如果是窗体的,则Inovke实现
Control control = c.Target as Control;
if (control != null)
{
control.Invoke(c, new object[] {this, EventArgs.Empty });//如果有参数,可以在object[]{}括号里面
}
else c(this, EventArgs.Empty);
}
}
} private void CheckLogin()
{
//Check login here,if true, success
if (SomeFuc(user, pwd))
{
this.loginSucceed = true;
}
else loginSucceed = false;
FireEvent();
}
}
}
/// <summary>
/// 引发登陆操作完成的事件
/// </summary>
private void FireEvent()
{
if (timeOutEvent != null)
{
Delegate[] list = timeOutEvent.GetInvocationList();
foreach (Delegate c in list)
{
//如果是窗体的,则Inovke实现
Control control = c.Target as Control;
if (control != null)
{
control.Invoke(c, new object[] {this, EventArgs.Empty });//如果有参数,可以在object[]{}括号里面
}
else timeOutEvent(this, EventArgs.Empty);
}
}
}
异步好了。例如这个csdn,你刚打开的时候它显示“游客”(其实可以显示“....正在登录”),过几秒钟才变成你的登录名。
终止线程的时候,需要比较长的一段时间,此时界面仍然假死。怎么回事啊?
无厘头。我的例子中不是已经开启了一个线程了吗,
private void CheckLogin()
{
//Check login here,if true, success
if (SomeFuc(user, pwd))
{
this.loginSucceed = true;
}
else loginSucceed = false;
FireEvent();
}
这里不就是线程做的事情么。你的主界面已经空出来了,你想做什么 都可以了。
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
} public event EventHandler timeOutEvent;//登陆操作的事件(包括超时、登陆失败或者成功)
private System.Windows.Forms.Timer timer = null;//计时器,设置超时用的
private string user = null;//额外数据,登陆用户名
private string pwd = null;//额外数据,登陆用户的密码
Thread loginThread = null;//登陆操作用的线程
private bool loginSucceed = false;//登陆是否成功 //登陆按钮
private void button1_Click(object sender, EventArgs e)
{
this.user = textBox1.Text;
this.pwd = textBox2.Text;
loginThread = new Thread(new ThreadStart(CheckLogin));
loginThread.IsBackground = true;
loginThread.Start();//登陆操作启用线程
timer.Start();//开始计时
} private void Form1_Load(object sender, EventArgs e)
{
this.timeOutEvent += new EventHandler(LoginResult);
timer = new System.Windows.Forms.Timer();
timer.Interval = 10000;//设置10秒超时
timer.Tick += new EventHandler(TimeOutEvent);
} private void LoginResult(object sender, EventArgs e)
{
this.label1.Text = loginSucceed ? "登陆成功" : "登陆失败";//这里是登陆成功或者失败后的结果
if (loginSucceed)
{
//验证成功后do something
MessageBox.Show("用户验证成功!");
}
else MessageBox.Show("用户名或者密码错误,或者连接超时!");//验证失败后
} private void TimeOutEvent(object sender, EventArgs e)
{
timer.Stop();
if (loginThread.IsAlive)
{
loginThread.Abort();
} this.loginSucceed = false;
FireEvent();//超时引发事件通知超时
} /// <summary>
/// 引发登陆操作完成的事件
/// </summary>
private void FireEvent()
{
if (timeOutEvent != null)
{
Delegate[] list = timeOutEvent.GetInvocationList();
foreach (Delegate c in list)
{
//如果是窗体的,则Inovke实现
Control control = c.Target as Control;
if (control != null)
{
control.Invoke(c, new object[] {this, EventArgs.Empty });//如果有参数,可以在object[]{}括号里面
}
else timeOutEvent(this, EventArgs.Empty);
}
}
} //线程调用的方法,也就是你要验证用户的过程
private void CheckLogin()
{
if (SomeFuc(user, pwd))//把你的数据库操作写在这里,SomeFuc是一个模拟方法,具体实现你自己写
{
this.loginSucceed = true;//验证成功
}
else loginSucceed = false;//验证失败
FireEvent();//此处是线程启动的登陆操作完成,引发事件
}
}
}
code=C#]
public static DataSet RunProcedure(string storedProcName, IDataParameter[] parameters)
{
try
{
using (SqlConnection connection = new SqlConnection(connectingstring))
{
DataSet dataSet = new DataSet();
connection.Open();
SqlDataAdapter sqlDA = new SqlDataAdapter();
sqlDA.SelectCommand = BuildQueryCommand(connection, storedProcName, parameters);
sqlDA.Fill(dataSet, "ds");
connection.Close();
return dataSet;
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message.ToString());
return null;
}
[/code]
public static DataSet RunProcedure(string storedProcName, IDataParameter[] parameters)
{
try
{
using (SqlConnection connection = new SqlConnection(connectingstring))
{
DataSet dataSet = new DataSet();
connection.Open();
SqlDataAdapter sqlDA = new SqlDataAdapter();
sqlDA.SelectCommand = BuildQueryCommand(connection, storedProcName, parameters);
sqlDA.Fill(dataSet, "ds");
connection.Close();
return dataSet;
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message.ToString());
return null;
}