If you encounter an error like the one below while coding your projects in C#, continue reading this article for a solution.
First of all, let’s see what was our mistake.
Visual Studio will give an error like this,
An exception of type ‘System.InvalidOperationException’ occurred in System.Windows.Forms.dll but was not handled in user code
Additional information: Cross-thread operation is incomplete: The ‘lblNumber’ control was accessed from another thread other than the thread in which it was created.
As the picture,
How to Resolve Cross Thread Peration Not Valid in C# WinForms
The main reason for this error is Thread conflicts. It is the error that is usually received in the development processes of programs with ASynchronous (MultiThread) structure.
The solution for this error is very simple. Adding the following code to the loading phase of your program will take care of this problem.
1 2 3 |
Control.CheckForIllegalCrossThreadCalls = false; |
Sample Code:
1 2 3 4 5 6 7 8 9 10 11 |
......... public Form1() { InitializeComponent(); CheckForIllegalCrossThreadCalls = false; //here } .......... |
Thanks to this code, Thread conflicts will be ignored and concrete errors will be eliminated.
Good work…