Invalid inter-thread operation: Access it solution from a thread that is not creating a control ""

Source: Internet
Author: User

Invalid inter-thread operation: Access it solution from a thread that is not creating a control "" http://blog.sina.com.cn/s/blog_568e66230101der7.html

When designing a file monitoring system using FileSystemWatcher, if a file is modified or created, the file modification event is triggered multiple times to generate multiple messages. In order to set a file to be modified once to produce a number of information to boil down to one, in the design of a new thread, in the specified time (for example, within 2 seconds) The modification of this file is considered a modification, resulting in only one message.

When this work is done, another problem arises: because you need to add a new hint to the ListBox control when the file is modified, the code is:

THIS.LSTRESULTS.ITEMS.ADD (info);

The result occurs at run time: invalid inter-thread operation: An error message such as this is accessed from a thread that does not create the control "Lstresults" . Based on the help tips on VS2012, go directly to the MSDN Help link http://msdn.microsoft.com/query/dev11.query?appId=Dev11IDEF1&l=EN-US&k=k ( EHInvalidOperation.WinForms.IllegalCrossThreadCall); K (targetframeworkmoniker-.netframework,version=v2.0); K ( DEVLANG-CSHARP) &rd=true

The concept of thread safety is presented here. In the case of this error message, that is, if you change the state of a control (such as setting a property value in it), you must use the thread that created the control, and if it is not the thread that created the control, you need to use the agent to complete the task. The MSDN specific descriptions are as follows:

1. Query the InvokeRequired property of the control

2. If invokerequired is true, use Invoke to invoke the control encapsulated by the delegate

3. If InvokeRequired is false, manipulate the control directly

Therefore, to solve this problem, we need

1, the definition of the Commission;

2, define the method of the corresponding delegate, this method to complete the operation of the control;

3, the operation of the original control, converted to invoke the invocation of the delegate method;

The specific solutions are as follows:

Define a delegate
Private delegate void Testdelegate (String str);

Define a method which match the above Delegae
private void TestMethod (String str)
{
Query the control's (here is Lstresults) invokerequired
if (this.lstResults.InvokeRequired)
{
Instansiate a delegate with the method
Testdelegate mydelegate = new Testdelegate (TestMethod);
Invoke delegate
This.lstResults.Invoke (MYDELEGATE,STR);
}
Else
{
Invokedrequired is false, so call the control directly
THIS.LSTRESULTS.ITEMS.ADD (str);
}
}

The original control operation code:

THIS.LSTRESULTS.ITEMS.ADD (info);

Changed to a thread-safe code as follows:

THIS.LSTRESULTS.ITEMS.ADD (info);
A Thread-safe call
This.testmethod (info);

Note that the original Control action code is commented, and the actual operation is placed within the TestMethod () method.

The above approach is the recommended approach for MSDN.

Another easiest way to do this is to change the properties:

Control.checkforillegalcrossthreadcalls = false;

This method directly blocks the emergence of error messages, which can be used in general, but note that this is not a thread-safe approach, it simply masks the error message.

Invalid inter-thread operation: access it from a thread that is not creating a control "" (GO)

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.