引用MSDN Vs2005内容:
Windows 窗体”使用单线程单元 (STA) 模型,因为“Windows 窗体”基于本机 Win32 窗口,而 Win32 窗口从本质上而言是单元线程。STA 模型意味着可以在任何线程上创建窗口,但窗口一旦创建后就不能切换线程,并且对它的所有函数调用都必须在其创建线程上发生。除了 Windows 窗体之外,.NET Framework 中的类使用自由线程模型。
STA 模型要求需从控件的非创建线程调用的控件上的任何方法必须被封送到(在其上执行)该控件的创建线程。为实现此目的,基类 Control 提供了几种方法(Invoke、BeginInvoke 和 EndInvoke)。Invoke 调用同步方法,而BeginInvoke 调用异步方法。
vs2003代码:
///类库
public class SqlHelperMgr
{
protected TextBoxBase oReciver;
///公开属性以便于外部进行设置
public TextBoxBase ConnectionStringReciver{set{this.oReciver = value;}}
public void ConnectionConfig(string sFile)
{
Process configer = new Process();
sConnectionConfig = sFile;
configer.StartInfo = new ProcessStartInfo(sFile,"");
configer.Exited +=new EventHandler(Configer_Exited);
configer.EnableRaisingEvents = true;
configer.Start();
}
protected void Configer_Exited(object sender, EventArgs e)
{
if(!System.IO.File.Exists(sConnectionConfig))System.IO.File.Create(sConnectionConfig);
System.IO.StreamReader reader = new System.IO.StreamReader(sConnectionConfig);
string sOutput = reader.ReadToEnd();
reader.Close();
///直接对外部Windows窗体控件引用的属性进行设置
if (sOutput.Length > 0)
oReciver.Text = sOutput.Substring(sOutput.IndexOf("Provider"));
}
}
///应用界面
public class FrmSqlHelper : System.Windows.Forms.Form
{
private System.Windows.Forms.TextBox txtConnStrFrom;
private SqlHelperMgr sqlMgr;
protected void InitcbDataBase()
{
sqlMgr = SqlHelperMgr.GetInstance();
///对sqlMgr的ConnectionStringReciver属性传递一个引用
sqlMgr.ConnectionStringReciver = this.txtConnStrFrom;
}
}
vs2005代码:
///类库
public class SqlHelperMgr
{
///公开委托
public delegate void OnConnectionSetDone(string sConnectionString);
///公开事件
public event OnConnectionSetDone ConnectionSetDoneHandler;
public void ConnectionConfig(string sFile)
{
Process configer = new Process();
sConnectionConfig = sFile;
configer.StartInfo = new ProcessStartInfo(sFile,"");
configer.Exited +=new EventHandler(Configer_Exited);
configer.EnableRaisingEvents = true;
configer.Start();
}
protected void Configer_Exited(object sender, EventArgs e)
{
if(!System.IO.File.Exists(sConnectionConfig))System.IO.File.Create(sConnectionConfig);
System.IO.StreamReader reader = new System.IO.StreamReader(sConnectionConfig);
string sOutput = reader.ReadToEnd();
reader.Close();
///激活事件
if (sOutput.Length > 0)
ConnectionSetDoneHandler(sOutput.Substring(sOutput.IndexOf("Provider")));
}
}
///应用界面
public class FrmSqlHelper : System.Windows.Forms.Form
{
private System.Windows.Forms.TextBox txtConnStrFrom;
private SqlHelperMgr sqlMgr;
///声明安全委托变量
private delegate void OnConntectionStringSetDone(string sConnectionString);
///声明安全访问委托
private OnConntectionStringSetDone _OnConntectionStringSetDone;
public FrmSqlHelper()
{
//
// Windows 窗体设计器支持所必需的
//
InitializeComponent();
///实例化安全委托对象变量
_OnConntectionStringSetDone = new OnConntectionStringSetDone(ConntectionStringSetDone);
//
// TODO: 在 InitializeComponent 调用后添加任何构造函数代码
//
}
protected void InitcbDataBase()
{
sqlMgr = SqlHelperMgr.GetInstance();
///订阅事件
sqlMgr.ConnectionSetDoneHandler += new SqlHelperMgr.OnConnectionSetDone(sqlMgr_ConnectionSetDoneHandler);
}
private void ConntectionStringSetDone(string sConnectionString)
{
///通过安全封装实现跨线程调用
this.txtConnStrFrom.Text = sConnectionString;
}
///事件响应
void sqlMgr_ConnectionSetDoneHandler(string sConnectionString)
{
///事件响应时启用安全异步访问
BeginInvoke(_OnConntectionStringSetDone, new object[] { sConnectionString });
}
}