C#中在线程中访问主Form控件的问题
C#不允许直接从线程中访问Form里的控件,比如希望在线程里修改Form里的一个TextBox的内容等等,唯一的做法是使用Invoke方法,下面是一个MSDN里的Example,很说明问题:<div style="padding-right: 5.4pt; padding-left: 5.4pt; background: #e6e6e6; padding-bottom: 4px; width: 95%; padding-top: 4px;">http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gifusingSystem;
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gifusingSystem.Drawing;
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gifusingSystem.Windows.Forms;
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gifusingSystem.Threading;
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gifpublicclassMyFormControl:Form
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gifhttp://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedBlock.gif...{
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gifpublicdelegatevoidAddListItem(StringmyString);
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gifpublicAddListItemmyDelegate;
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gifprivateButtonmyButton;
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gifprivateThreadmyThread;
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gifprivateListBoxmyListBox;
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gifpublicMyFormControl()
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gifhttp://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif...{
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gifmyButton=newButton();
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gifmyListBox=newListBox();
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gifmyButton.Location=newPoint(72,160);
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gifmyButton.Size=newSize(152,32);
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gifmyButton.TabIndex=1;
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gifmyButton.Text="Additemsinlistbox";
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gifmyButton.Click+=newEventHandler(Button_Click);
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gifmyListBox.Location=newPoint(48,32);
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gifmyListBox.Name="myListBox";
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gifmyListBox.Size=newSize(200,95);
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gifmyListBox.TabIndex=2;
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gifClientSize=newSize(292,273);
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gifhttp://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gifControls.AddRange(newControl[]...{myListBox,myButton});
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gifText="'Control_Invoke'example";
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gifmyDelegate=newAddListItem(AddListItemMethod);
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif}
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gifstaticvoidMain()
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gifhttp://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif...{
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gifMyFormControlmyForm=newMyFormControl();
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gifmyForm.ShowDialog();
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif}
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gifpublicvoidAddListItemMethod(StringmyString)
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gifhttp://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif...{
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gifmyListBox.Items.Add(myString);
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif}
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gifprivatevoidButton_Click(objectsender,EventArgse)
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gifhttp://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif...{
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gifmyThread=newThread(newThreadStart(ThreadFunction));
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gifmyThread.Start();
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif}
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gifprivatevoidThreadFunction()
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gifhttp://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif...{
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gifMyThreadClassmyThreadClassObject=newMyThreadClass(this);
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gifmyThreadClassObject.Run();
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif}
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockEnd.gif}
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gifpublicclassMyThreadClass
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gifhttp://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedBlock.gif...{
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gifMyFormControlmyFormControl1;
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gifpublicMyThreadClass(MyFormControlmyForm)
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gifhttp://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif...{
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gifmyFormControl1=myForm;
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif}
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gifStringmyString;
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gifpublicvoidRun()
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gifhttp://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif...{
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.giffor(inti=1;i<=5;i++)
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gifhttp://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif...{
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gifmyString="Stepnumber"+i.ToString()+"executed";
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gifThread.Sleep(400);
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif//Executethespecifieddelegateonthethreadthatowns
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif//'myFormControl1'control'sunderlyingwindowhandlewith
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif//thespecifiedlistofarguments.
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gifmyFormControl1.Invoke(myFormControl1.myDelegate,
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gifhttp://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gifnewObject[]...{myString});
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif}
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif}
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockEnd.gif}
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif
页:
[1]