c#在windows form开发时多线程对UI…

来源:互联网 发布:网页游戏数据修改 编辑:程序博客网 时间:2024/06/06 04:12

windows form开发的时候,如果使用了多线程,则新线程无法实现对原有控件的操作。

解决方案可以是:

利用form对象自带的BeginInvoke方法,实例代码

this . BeginInvoke
(
new MethodInvoker
(
delegate ( )
{
this . Cursor = Cursors . WaitCursor;
listView收件箱 . Items . Clear ( );
foreach ( var mail in e )
{
ListViewItem currentItem = new ListViewItem ( );

currentItem . Tag = mail . BodyText;
//邮件编号
currentItem .Text = mail . MessageID;
//发件人
currentItem . SubItems . Add ( string . IsNullOrEmpty ( mail . From[ 0 ] . Address ) ? @"<none>" : mail. From [ 0 ] . Address );
//收件时间
currentItem . SubItems . Add ( mail . Date . ToString ( "yyyy.MM.ddHH:mm:ss" ) );
//邮件标题
currentItem . SubItems . Add ( mail . Subject );
//是否查询
currentItem . SubItems . Add ( "是的" );
listView收件箱 . Items . Add ( currentItem );
}
this . Cursor = Cursors . Default;
}
)
);

0 0
原创粉丝点击