gpt4 book ai didi

c# - 连接时如何放置取消按钮

转载 作者:行者123 更新时间:2023-11-30 17:28:46 25 4
gpt4 key购买 nike

我使用登录名和密码在 C# 中建立连接,我想如果连接太多,请放置一个按钮以取消与数据库的连接长。

我想知道怎么做,如果可能的话把它放在一个线程中。

代码如下:

private void btncon_Click(object sender, EventArgs e)
{
string strLogin = tblogin.Text.Trim();
string pass = tbpwd.Text;
bool success = false;

if (String.IsNullOrWhiteSpace(strLogin) || String.IsNullOrWhiteSpace(pass))
{
MessageBox.Show("Veuillez remplir tous les champs SVP ");
}
else if(String.IsNullOrEmpty(strLogin) || String.IsNullOrEmpty(pass)){
MessageBox.Show("Veuillez remplir tous les champs SVP ");
}
else
{
model.Connexion cm = new model.Connexion();
pass = Snippets.SHA1Util.SHA1HashStringForUTF8String(pass).ToString();
string[] user = cm.login(strLogin, pass);

if(user[0] != null)
{
Int32.TryParse(user[0], out iduser);
Int32.TryParse(user[1], out idGrp);
Int32.TryParse(user[2], out idbtq);
nom = user[3];

if (idGrp != 3)
success = true;
else
{
MessageBox.Show("Accès Non autorisé , Veuillez contacter l'administrateur");
success = false;
}
}
else
{
MessageBox.Show("Email ou mot de passe incorrect.");
success = false;
}
}

if (success)
{
main Principale = new main();
Principale.Show();
Hide();`
}
}

最佳答案

问题是,当用户点击Cancel 按钮时,无法正常取消正在执行的cm.login() 方法。您可以使用 Thread.Abort() 强制终止登录,但这是不安全的并且强烈建议不要这样做,因为正确执行需要在另一个 AppDomain 中执行代码并且会使代码非常复杂。

幸运的是,如果满足以下条件,您仍然可以实现 Cancel 按钮:

  • 在另一个线程上调用cm.login()是安全的
  • cm.login() 在用户点击 Cancel 按钮后在后台(在另一个线程上)完成不会产生不良影响。

此代码还假设它是一个 Winform 应用程序(但 WPF 应用程序的解决方案非常相似)。它还假设主窗体有一个隐藏的按钮 btnCancel (Visible=false) 并且它是 Click 事件处理程序设置为 btnCancel_Click 方法。

TaskCompletionSource<Object> CancelLoginTcs = new TaskCompletionSource<Object>();

// Make button click handler method async
private async void btncon_Click(object sender, EventArgs e)
{
try
{
// Make Cancel button visible, so that user can click on it
btnCancel.Visible = true;

// Prepare everything needed to start login
//var strLogin = ...;
//var pass = ...;
//model.Connexion cm = new model.Connexion();
// ...

// Start login on another thread
var loginTask = Task<string[]>.Run(() => cm.login(strLogin, pass));

// Create task that is used to wake-up main thread, when user clicks
// on the Cancel button before login finishes.
CancelLoginTcs = new TaskCompletionSource<Object>();

// Wait for login task or cancel task to complete, whichever finishes first
await Task.WhenAny(loginTask, CancelLoginTcs.Task);

if (CancelLoginTcs.Task.IsCanceled)
{
// User clicked on the Cancel button.
// Login method will be running in the background, until it
// finishes. This assumes that it is safe to do so.
// Here you should do neccessary clean-up, inform user, etc.
// ...
}
else
{
// Login finished and user did NOT click on the Cancel button.

try
{
// Simply read result of login. If an Exception occured during login,
// it will be rethrow now, so you should handle it appropriatelly
var user = loginTask.Result;

// Here program continues in a usual way
// ...
}
catch(Exception E)
{
// Handle login exception
// ...
}
}
}
finally
{
// Hide Cancel button again
btnCancel.Visible = false;
CancelLoginTcs = null;
}
}

private void btnCancel_Click(object sender, EventArgs e)
{
// Set cancel task to cancelled state.
// This will wake-up main thread and let it continue
CancelLoginTcs.SetCanceled();
}

关于c# - 连接时如何放置取消按钮,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52142247/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com