Vs2003向Vs2005迁移之Windows窗体篇

引用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 });
                   } 
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值