C# report an error
Problem scenario
In C# Windows GUI programming, access a control in the designer from a child thread, such as disabling a button. This will trigger the exception:
:"Inter-thread operation is invalid: It is accessed from a thread that did not create the control "btn_exp"."
Cause of the problem
There is a rule in C# Windows GUI programming, that is, the control can only be accessed through the thread that creates the control, otherwise unpredictable results may be produced.
Solution
Plan 1, break the rules
Add the following statement to the constructor:
= false;
Setting the above attribute to false will cancel the checking of illegal cross-thread calls, which is simple and crude but does not guarantee that there will be no errors.
Plan 2: Delegate
.net provides us with the Invoke method and the BeginInvoke method to call delegates. The difference is that the former is thread blocking, while the latter is asynchronous.
Therefore, when the delegate is a time-consuming function, the Invoke method will cause thread blockage, which will manifest as a user interface stuttering; while the BeginInvoke method will not wait for the delegate to be executed.
Although the BeginInvoke method will not cause thread blockage, you can use the EndInvoke method or other similar WaitHandle mechanism to wait for the completion of the asynchronous operation.
BeginInvoke(new MethodInvoker(()=> { btn_exp.Enabled = false; }));
Among them, the difference between MethodInvoker delegate and EventHandler delegate is that the former does not take parameters:
public delegate void MethodInvoker(); public delegate void EventHandler(object sender, EventArgs e);
c# WPF InvalidOperationException Several Common Causes
What is InvalidOperationException
The operation is invalid exception. Exception raised when the method call is invalid for the current state of the object.
inherit
- Object
- Exception
- SystemException
Detailed description
If the method failed to call is caused by other reasons other than the invalid parameter, use this exception. Typically, this exception is raised when the state of the object does not support method calls.
For example, the following method or reason may throw an exception nvalidOperationException:
- If you modify the object of the collection after creating the enumerator,
- If the resource set is closed before executing the method call,
- If the object you are adding will result in an incorrect XML document.
- Try a method to manipulate the UI from a thread that is not the main thread or UI thread.
- Update UI threads from non-UI threads (calling across UI threads, for this reason, the UI controls called in the thread cause an error!)
- Change collection when iterates over a collection
- Sort arrays or collections whose objects cannot be compared
- Convert null <T> to its base type
- Calling methods on empty sets
- Call SingleOrDefault or enumerable for sequences without an element.
- Dynamic cross-application domain field access
Since InvalidOperationException exceptions can be raised in a variety of cases, it is important to read exception messages returned by the message property.
HRESULT
InvalidOperationException uses HRESULT with COR_E_INVALIDOPERATION value 0x80131509.
The above is personal experience. I hope you can give you a reference and I hope you can support me more.