Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to solve the problem of error reporting in C # programming by System.InvalidOperationException

2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)05/31 Report--

This article mainly explains the "C # programming error report System.InvalidOperationException how to solve", the article explains the content is simple and clear, easy to learn and understand, the following please follow the editor's train of thought slowly in depth, together to study and learn "C # programming error report System.InvalidOperationException how to solve" it!

C# error reporting System.InvalidOperationException problem scenario

In C # Windows GUI programming, to access a control in the designer from a child thread, such as disabling a button. This triggers the exception:

System.InvalidOperationException: "invalid inter-thread operation: it is never accessed by the thread that created the control" btn_exp "."

Cause of the problem

There is a rule in C # Windows GUI programming that the control can only be accessed through the thread that created the control, otherwise it may produce unpredictable results.

Solution

Plan one, break the rules.

Add the following statement to the constructor:

Control.CheckForIllegalCrossThreadCalls = false

Setting the above property to false cancels checking for illegal cross-thread calls, simple and rude, but does not guarantee error-free.

Option 2, using Delegate

Net provides us with the Invoke method and the BeginInvoke method to call the delegate. The difference between them is that the former is thread blocking and the latter is asynchronous.

Therefore, when the delegate is a more time-consuming function, the Invoke method will cause thread blocking, showing as a stutter in the user interface, while the BeginInvoke method will not wait for the delegate to finish execution.

Although the BeginInvoke method does not cause thread blocking, you can use the EndInvoke method or other similar WaitHandle mechanism to wait for the asynchronous operation to complete.

BeginInvoke (new MethodInvoker (() = > {btn_exp.Enabled = false;}))

The difference between a MethodInvoker delegate and an EventHandler delegate is that the former takes no parameters:

Public delegate void MethodInvoker (); public delegate void EventHandler (object sender, EventArgs e); several common causes of c # WPF InvalidOperationException exception what is InvalidOperationException

Invalid operation exception. The exception that is thrown when the method call is not valid for the current state of the object.

Inherit

Object

Exception

SystemException

detailed description

This exception is used if the failure to call the method is caused by something other than an invalid parameter. Typically, this exception is thrown when the state of the object does not support method calls.

For example, the following methods or reasons throw an exception nvalidOperationException:

IEnumerator.MoveNext if the object of the collection is modified after the enumerator is created.

ResourceSet.GetString if you close the resource set before executing the method call.

XContainer.Add if the object you want to add will result in an incorrect XML document.

Try a method of manipulating UI from a thread that is not a main thread or a UI thread.

Update the UI thread from a non-UI thread (it is called across UI threads, and this is why the UI control called in the thread causes an error!)

Change the collection when iterating through the collection

Sort arrays or collections whose objects cannot be compared

Convert to its base type for null

Call the system.exception method on an empty collection

Call SingleOrDefault or enumerable on a sequence without a single element.

Dynamic cross-application domain field access

Because InvalidOperationException exceptions can be thrown in a variety of situations, it is important to read the exception messages returned by the message property.

HRESULT

InvalidOperationException uses HRESULT with a COR_E_INVALIDOPERATION value of 0x80131509.

Thank you for your reading, the above is the content of "C # programming error reporting System.InvalidOperationException how to solve". After the study of this article, I believe you have a deeper understanding of how to solve this problem of C# programming error reporting System.InvalidOperationException. The specific use of the situation also needs to be verified in practice. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report