What is the difference between the focus () and gotfocus () of the control in C?

Source: Internet
Author: User

Q:

F (this. invokerequired)

{

This. begininvoke (New methodinvoker (loadglobalimage ));

Return;

}

What does it mean?

 

 

A:

In C #, direct access to the control is prohibited across threads. invokerequired is generated to solve this problem.

When the invokerequired attribute value of a control is true, a thread other than the control is created to access it. In this case, the new methodinvoker (loadglobalimage) is called internally to complete the following steps, this method ensures the control security. You can understand that someone wants to borrow money from you and he can take it directly in your wallet. This is too insecure, therefore, it is safe to let others tell you first and then lend the money out of your wallet to others.

 

 

Another:

In the design, in order to separate the interface from the logic, my approach is to use the event. The interface only needs to respond to the event to process the display of the interface. Events may be triggered by different threads in logical processing. The response methods of these events will cause an exception when modifying the control content on the interface.

The control. invokerequired attribute and the invoke method are used.

Get a value that indicates whether the caller must call the invoke method when calling the method of the control, because the call is located in a thread other than the thread where the control is created. If the handle of a control is created on a thread different from that of the calling thread (that is, you must call the control through the invoke method), the value is true; otherwise, the value is false. Controls in Windows Forms are bound to specific threads and do not have thread security. Therefore, if you call the control method from another thread, you must use one invoke method of the control to mail the call to the appropriate thread. This attribute can be used to determine whether the invoke method must be called. It is useful when no thread has a control.

Next let's talk about this usage (my general practice): First define a delegate, which is the same as the signature of the event processing function. Of course, it is also possible to directly use the delegate of this event, for example:

Private delegate void invokecallback (string MSG );

Then, determine the value of this attribute to determine whether to call the invoke function:

 

Void m_comm_messageevent (string MSG)

{If (txtmessage. invokerequired)

{

Invokecallbackmsgcallback = new invokecallback (m_comm_messageevent );

Txtmessage. Invoke (msgcallback, new object [] {MSG });

}

Else

{Txtmessage. Text = MSG ;}

Note: This function is an event processing function, and txtmessage is a text box. In this way, the thread security of controls in the form is achieved.

Void m_comm_messageevent (string MSG) {If (txtmessage. invokerequired) {invokecallbackmsgcallback = new invokecallback (m_comm_messageevent); txtmessage. invoke (msgcallback, new object [] {MSG});} else {txtmessage. TEXT = MSG ;}

 

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

Invokerequired is true when the current thread is not the thread for creating controls. For example, you can open a thread by yourself or use timer events to access controls on the form, this attribute of the form in the thread is true.
Simply put, if there are two threads, thread a and thread B, and there is a control C, it is new in thread. If you call C. invokerequired in any method running in thread a, false is returned. On the contrary, if any method running in thread B calls C. invokerequired, true is returned. Whether the UI thread is irrelevant to the result. (Normally, the thread where the control is located is the UI thread, but there can be exceptions)
It can also be considered that when new control () is used, control records the current thread with a variable. When invokerequired is called, returns the thread recorded when the current thread is not new.

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

I understand: If invokerequired = true indicates that other threads need to access the control, the invoke is called to be transferred to the control owner for processing.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.