Controls created on a thread cannot be the parent of the control created on another thread (transfer)

xiaoxiao2021-04-07  253

Exceptions that may occur when solving multi-thread operation controls: "The control created on a thread cannot be the parent of the control created on another thread."

Posted on 2005-10-25 14:47

HQT reading (218)

Comments (0)

edit

Collect

Collection to 365Key Categories:

.NET

Some Summary About "Multi-Thread Windows Form Control Example" in .NET Framework:

-------------------------------------------------- ----

"Windows Form" uses a single-threaded unit (STA) model because the "Windows Form" is based on the Win32 window, while the Win32 window is in essentially unit thread. The STA model means that the window can be created on any thread, but once the window cannot switch the thread, and all of its function calls must occur on its creation thread. In addition to the Windows Form, the class in the .NET Framework uses the free thread model. For information on threads in .NET Framework, see Thread Processing.

The STA model requires any way to be on the control from the controlled non-creating threads must be sealed (on it) the creation thread of the control. Base class

CONTROL

Some methods are provided for this purpose (

Invoke

,

BeginInvoke

with

Endinvoke

). Invoke generation synchronization method call; BeGinInvoke generates asynchronous method calls.

If you use multi-threads for the task of the resource in the control, the user interface can perform a large amount of occupied resources on the background thread while maintaining a response. -------------------------------------------------- ---- The following directly gives my instance program, a button is the data binding of the DataGrid control directly to the DataGrid control; the other button will be successfully executed through the invoke call.

Using

System;

Using

System.data;

Using

System.Threading;

Using

System.drawing;

Using

System.collections;

Using

System.componentmodel;

Using

System.windows.forms;

Namespace

MultithreadOperateControls

{/ ** ////

/// Form1 Summary Description. /// public class Form1: System.Windows.Forms.Form {private delegate void BindDataGridDelegate (); // create and delegate object delegate private BindDataGridDelegate bindDataGridDelegate; Thread bindGridThread; private System.Windows.Forms.Button btnErrorHandle; Private system.windows.forms.button btnsuccessHandle; Private system.windows.forms.dataGrid DataGrid1; ///// // The required designer variable. /// private System.ComponentModel.Container components = null; public Form1 () {bindDataGridDelegate = new BindDataGridDelegate (BindDataGrid); // instantiate method delegate object and the specified // // call the Windows Forms Designer Support for // itializeComponent (); // // Todo: Add any constructor code //} / ** //// /////////////////} ////////////////////} ///. /// Protected Override Void Dispose (Bool Disposing) {if (disponents! = Null) {Components.dispose ();}} Base.Dispose (DISPOSISING);} Windows Form Designer The generated code #REGION Windows Form Designer Generate / ** //// /// Designer Supports the required method - Do not use the code editor to modify the // / this method.

/// private void InitializeComponent () {this.btnErrorHandle = new System.Windows.Forms.Button (); this.btnSuccessHandle = new System.Windows.Forms.Button (); this.dataGrid1 = new System. Windows.Forms.DataGrid ();. ((System.ComponentModel.ISupportInitialize) (this.dataGrid1)) BeginInit (); this.SuspendLayout (); // // btnErrorHandle // this.btnErrorHandle.Location = new System.Drawing .Point (40, 24); this.btnerhandle.name = "btnerrorhandle"; this.btnerhandle.size = new system.drawing.size (112, 40); this.btnerhandle.tabindex = 0; this.btnerhandle.text = "ErrorHandle"; this.btnErrorHandle.Click = new System.EventHandler (this.btnErrorHandle_Click); // // btnSuccessHandle // this.btnSuccessHandle.Location = new System.Drawing.Point (232, 24); this.btnS uccessHandle.Name = "btnSuccessHandle"; this.btnSuccessHandle.Size = new System.Drawing.Size (112, 40); this.btnSuccessHandle.TabIndex = 1; this.btnSuccessHandle.Text = "SuccessHandle"; this.btnSuccessHandle.Click = new System.EventHandler (this.btnSuccessHandle_Click); // // dataGrid1 // this.dataGrid1.DataMember = ""; this.dataGrid1.HeaderForeColor = System.Drawing.SystemColors.ControlText; this.dataGrid1.Location = new System. Drawing.point (32, 88); this.DataGrid1.name = "

DataGrid1 "; this.dataGrid1.size = new system.drawing.size (336, 176); this.dataGrid1.tabindex = 2; // // form1 // this.autoscalebasesize = new system.drawing.size (6, 14 This.clientsize = new system.drawing.size (408, 301); this.Controls.add (this.dataGrid1); this.Controls.add (this.btnsuccesshandle); this.controls.add (this.btnerrorhandle) "This.name =" form1 "; this.text =" Multi-threaded Form Control Council "; this.Load = New System.EventHandler (this.MM1_LOAD); (System.comPonentModel.isupportInitialize) (this.DataGrid1 ))))). Endinit (); this.ResumeLayout (false);} #ENDREGION / * ////

/// The primary entry point of the application.

/// [STAThread] static void Main () {Application.Run (new Form1 ());} private void Form1_Load (object sender, System.EventArgs e) {BindDataGrid ();} private void btnErrorHandle_Click (object sender, System.EventArgs e) {StopBindThread (); bindGridThread = new thread (new ThreadStart (BindDataGrid)); // this thread directly call controls the non-operation exception is thrown created bindGridThread.Start ();} private void btnSuccessHandle_Click ( object sender, System.EventArgs e) {StopBindThread (); bindGridThread = new Thread (new ThreadStart (InvokeBindDataGrid)); // invoked through a delegate, legal bindGridThread.Start ();} private void BindDataGrid () {int dataGridItemLenght = 5; DataTable DT = New DataTable ("DataGridSource"); DT.COLUMNS.ADD ("randomvalue"); for (INT i = 0; i

转载请注明原文地址:https://www.9cbs.com/read-132572.html

New Post(0)