日期:2014-05-19  浏览次数:20873 次

C#的线程中如何操作控件?
C#的线程中如何操作控件?比如progressbar,text什么的?谢谢

------解决方案--------------------
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Threading;
using System.Data.SqlClient;
using System.Collections;

namespace AutoMessager
{
delegate void myDelegate();
delegate void SetTextCallback(string text);

public partial class frmAutoMsg : Form
{
event myDelegate myEvent;
string connStr = string.Empty;
Thread thd;
//private Icon eyeIcon;

//private NotifyIconEx notifyIconA;
//private NotifyIconEx notifyIconB;
private bool canClosed = false;

public frmAutoMsg()
{
this.ShowInTaskbar = false;
InitializeComponent();
//eyeIcon = new Icon(GetType(), "EYE.ICO ");
notifyIcon1.ContextMenu = contextMenuB;
}

private void SetText(string text)
{
// InvokeRequired required compares the thread ID of the
// calling thread to the thread ID of the creating thread.
// If these threads are different, it returns true.
if (this.txtMsgStatus.InvokeRequired)
{
SetTextCallback d = new SetTextCallback(SetText);
this.Invoke(d, new object[] { text });
}
else
{
this.txtMsgStatus.Text += text;
}
}


private void frmAutoMsg_Load(object sender, EventArgs e)
{
connStr = System.Configuration.ConfigurationManager.AppSettings[ "ConnString "];
thd = new Thread(new ThreadStart(doEvent));
thd.IsBackground = true;
thd.Start();
//doEvent();
//notifyIcon1.Visible = true;
}

/// <summary>
///
/// </summary>
void UpUserState()
{
SqlConnection conn = null;
DateTime now = DateTime.Now;
SqlTransaction tran = null;
SqlDataReader dr = null;
try
{
//--- 数据操作忽略

SetText( "\r\n系统提示: 职员合同消息更新成功!\r\n ");
SetText( "执行时间: " + now.ToString( "yyyy-MM-dd HH:mm:ss ") + "\r\n ");


}
catch (Exception ex)
{
dr.Close();
tran.Rollback();
SetText( "\r\n系统错误: 职员合同消息更新错误: " + ex.Message + "\r\n ");
SetText( "执行时间: " + now.ToString( "yyyy-MM-dd HH:mm:ss ") + "\r\n ");
}
finally
{
dr.Close();
conn.Close();
conn.Dispose();
}
}
/// <summary>
///
/// </summary>
void UpCaiGou()
{
SqlConnection conn = null;
DateTime now = DateTime.Now;
SqlTransaction tran = null;
SqlDataReader dr = null;
try
{