How to operate the control on the sub-thread

xiaoxiao2021-03-14  188

In general, the control operations on the form directly on the sub-thread are abnormal, which is due to the threads of the sub-threads and the running forms, so they want to operate the control on the form. It is impossible to operate through the control object name, but not to say that Microsoft provides Invoke method, its role is to let the sub-thread tell the window thread to complete the corresponding control operation.

Now use a process strip with thread control, the rough steps are as follows:

1. Create an Invoke function, which is approximately as follows:

///

/// delegate function to be invoked by main thread

///

Private void invokefun ()

{

PRGBAR.Value <100)

Prgbar.value = prgbar.value 1;

}

2. Subphone entry function:

///

/// Thread Function Interface

///

Private void threadfun ()

{

// Create Invoke Method by Specific Function

MethodInvoker Mi = New MethodInvoker (this.invokefun);

For (int i = 0; i <100; i )

{

THIS.BEGININVOKE (MI);

Thread.sleep (100);

}

}

3. Create a sub-thread:

Thread twprocess = new thread (New Threadstart (threadfun);

THDPROCESS.START ();

Remarks:

Using system.threading;

Private system.windows.Forms.ProgressBar prgbar;

The effect after run is as shown below:

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

New Post(0)