|
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
|