c#怎么进行异步更新cs的label

c#怎么进行异步更新cs的label,第1张

Label.Text="XXX"直接赋值就是更新 如果你不能实现,相关代码拿上来看看 --------------------- 这里没错,检查方法的调用和Label的ID对应,清空Label1的默认值.

解释名词可能比较枯燥,我还是拿一个异步登录的例子来给你吧:using System

using System.Collections.Generic

using System.ComponentModel

using System.Data

using System.Drawing

using System.Text

using System.Windows.Formsnamespace SysUI

{

public partial class UserLogin : Form

{

public bool IsLogin

private string useName = string.Empty

private string usePassword = string.Emptypublic UserLogin()

{

InitializeComponent()

}private int loginstate = -1

/// <summary>

/// 创建异步调用委托

/// </summary>

/// <returns></returns>

private delegate int AsyncMethodCaller()/// <summary>

/// 取消登录

/// </summary>

/// <param name="sender"></param>

/// <param name="e"></param>

private void button2_Click(object sender, EventArgs e)

{

IsLogin = false

this.Close()

}/// <summary>

/// 验证并返回登录状态[异步方法]

/// </summary>

/// <returns></returns>

private int ToLogin()

{

try

{

DignityVerifiesServer.DignityVerifiesServer LogInObj = new SysUI.DignityVerifiesServer.DignityVerifiesServer()

int loginstate = LogInObj.DignityVerifies(this.useName, this.usePassword)

LogInObj.Dispose()

return loginstate

}

catch

{

return 403

}

}/// <summary>

/// 异步回调函数

/// </summary>

/// <param name="ar"></param>

private void Callback(IAsyncResult ar)

{

AsyncMethodCaller caller = (AsyncMethodCaller)ar.AsyncState

loginstate = (int)caller.EndInvoke(ar)

MethodInvoker UnLoading = new MethodInvoker(this.Unlogin)

UserService.UserService UseId = new SysUI.UserService.UserService()

int userId = -1

Service.Service WriteLog = new SysUI.Service.Service()

switch (loginstate)

{

case 404:

MessageBox.Show("登录失败:\r\r返回状态码:" + loginstate.ToString() + "\r\r错误描述:指定的用户不存在!", "登录失败", MessageBoxButtons.OK, MessageBoxIcon.Error)

break

case 403:

MessageBox.Show("登录失败:\r\r返回状态码:" + loginstate.ToString() + "\r\r错误描述:无法连接到服务器!", "登录失败", MessageBoxButtons.OK, MessageBoxIcon.Error)

break

case 400:

userId = UseId.getSelectedUserId(this.useName)

WriteLog.WriteLog("登录失败:原因,此用户已在使用中。", userId)

MessageBox.Show("用户不可用:\r\r返回状态码:" + loginstate.ToString() + "\r\r错误描述:指定的用户已经登录,无法继续使用此用户!", "登录失败", MessageBoxButtons.OK, MessageBoxIcon.Error)

break

case 500:

userId = UseId.getSelectedUserId(this.useName)

WriteLog.WriteLog("登录失败:原因,密码错误。", userId)

MessageBox.Show("登录失败:\r\r返回状态码:" + loginstate.ToString() + "\r\r错误描述:密码错误!", "登录失败", MessageBoxButtons.OK, MessageBoxIcon.Error)

break

case 100:

userId = UseId.getSelectedUserId(this.useName)

UseId.ReplacementOnLineState(userId, 1)

IsLogin = true

WriteLog.WriteLog("登录成功。", userId)

break

default:

MessageBox.Show("登录失败:\r\r返回非遇期的状态码:" + loginstate.ToString() + "\r\r错误描述:内部应用程序异常!", "应用程序错误", MessageBoxButtons.OK, MessageBoxIcon.Error)

break

}

UseId.Dispose()

WriteLog.Dispose()

this.Invoke(UnLoading)

}/// <summary>

/// 登录事件

/// </summary>

/// <param name="sender"></param>

/// <param name="e"></param>

private void button1_Click(object sender, EventArgs e)

{

this.StartLogIn()

}/// <summary>

/// 执行登录

/// </summary>

private void StartLogIn()

{

if (textBox1.Text == null || textBox1.Text.Trim() == "")

{

MessageBox.Show("请输入用户名!", "错误提示", MessageBoxButtons.OK, MessageBoxIcon.Warning)

this.Unlogin()

textBox1.Focus()

return

}

if (textBox2.Text == null || textBox2.Text.Trim() == "")

{

MessageBox.Show("请输入登录密码!", "错误提示", MessageBoxButtons.OK, MessageBoxIcon.Warning)

this.Unlogin()

textBox2.Focus()

return

}

this.useName = textBox1.Text.Trim()

this.usePassword = textBox2.Text.Trim()

this.logining()

/******************创建异步委托实例*****************/

AsyncMethodCaller caller = new AsyncMethodCaller(this.ToLogin)

IAsyncResult result = caller.BeginInvoke(new AsyncCallback(this.Callback), caller)

//IAsyncResult result = caller.BeginInvoke(textBox1.Text.Trim(), textBox2.Text.Trim(), null, null)

this.logining()

}

/// <summary>

/// 启动登录提示

/// </summary>

private void logining()

{

label1.Visible = false

label2.Visible = false

textBox1.Visible = false

textBox2.Visible = false

button1.Visible = false

button2.Visible = false

btnConfig.Visible = false

label3.Visible = true

LoadingImg.Visible = true

}/// <summary>

/// 关闭登录提示

/// </summary>

private void Unlogin()

{

label3.Visible = false

LoadingImg.Visible = false

label1.Visible = true

label2.Visible = true

textBox1.Visible = true

textBox2.Visible = true

button1.Visible = true

button2.Visible = true

btnConfig.Visible = true

if (loginstate == 100)

this.Close()

}private void button1_KeyPress(object sender, KeyPressEventArgs e)

{

if (e.KeyChar == 13)

{

e.Handled = true

this.StartLogIn()

}

}private void UserLogin_KeyPress(object sender, KeyPressEventArgs e)

{

if (e.KeyChar == 13)

{

e.Handled = true

SendKeys.Send("{TAB}")

}

}/// <summary>

/// 配置按钮触发事件

/// </summary>

/// <param name="sender"></param>

/// <param name="e"></param>

private void btnConfig_Click(object sender, EventArgs e)

{

ConfigEdit frm = new ConfigEdit()

frm.ShowDialog()

}

}

}


欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/sjk/10001072.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-05-04
下一篇 2023-05-04

发表评论

登录后才能评论

评论列表(0条)

保存