This article is a mirror article of machine translation, please click here to jump to the original article.

View: 17697|Reply: 0

[Source] Cross-thread operation not valid: Control 'txtresult' accessed from a threa...

[Copy link]
Posted on 12/14/2015 3:27:31 PM | | |

When we make a form application, sometimes we need to get information about the current form or call the controls of other forms.

Here I introduce two methods to achieve this.


When we need to call controls from other forms in a form, we can use controls. CheckForIllegaCrossThreadCalls=false;

For example: Button.CheckForIllegalCrossThreadCalls=false;

Of course, we can also make the entire form cross-threaded

Form.CheckForIllegalCrossThreadCalls=false;

This allows us to access the controls or properties of this form in other forms.

This may not be the safest method, and sometimes debugging will give us an error.


Here I will introduce another method that can be accessed across threads.

Invoke jumps onto the form thread to execute the function on the delegate


First, we need to declare a commission.

public delegate void DelSetTime(object str);

Then we just need to prepare the function that needs to be executed,

public void setTime(object str);

Now we can put the delegate on the thread of the form to execute

Delegate d=new DelsetTime(setTime);

this. Invoke(d,Datetime.Now.ToString());

This way we can achieve cross-threaded access.


Each form has a form thread, which is mainly responsible for completing the drawing of the form and the form controls

This thread we call the form thread or the main thread.

Many times we need to add threads to the form to process the data, and then we can set these newly created threads as background threads when the form is closed

The thread will also end.

ThreadStart ths=new ThreadStart(Demo);

Thread th= new Thread(ths);

th. Start();

th. IsBackground=true;


Original: http://bingdao.sinaapp.com/net%E4%BD%93%E7%B3%BB/380.html     






Previous:The input of the two-digit character array is sorted when the output is made
Next:【iOS Development Series Tutorial Released in the Summer】iPhone Tutorial
Disclaimer:
All software, programming materials or articles published by Code Farmer Network are only for learning and research purposes; The above content shall not be used for commercial or illegal purposes, otherwise, users shall bear all consequences. The information on this site comes from the Internet, and copyright disputes have nothing to do with this site. You must completely delete the above content from your computer within 24 hours of downloading. If you like the program, please support genuine software, purchase registration, and get better genuine services. If there is any infringement, please contact us by email.

Mail To:help@itsvse.com