The first topic is What wrong with my InvokeRequied
I followed the answer that he recommended it to me but I found a new problem.
The result of below picture is cross thread exception.
What is wrong with my code ?
How to solve this problem ?
The first topic is What wrong with my InvokeRequied
I followed the answer that he recommended it to me but I found a new problem.
The result of below picture is cross thread exception.
What is wrong with my code ?
How to solve this problem ?
Copyright © 2021 Jogjafile Inc.
According to MSDN
InvokeRequired
can returnfalse
even in cases whereInvokeRequired
should betrue
- namely in the case that you accessInvokeRequired
before theHandle
of that control/form (or a parent of it) has been created.Basically your check is incomplete which leads to the result you see.
You need to check
IsHandleCreated
- if that isfalse
then you would need to useInvoke
/BeginInvoke
regardless of whatInvokeRequired
returns.[UPDATE] BUT: This usually won't work robustly since
Invoke
/BeginInvoke
check which thread createdHandle
to do their magic... [/UPDATE]Only if
IsHandleCreated
istrue
you act based on whatInvokeRequired
returns - something along the lines of:[UPDATE]
Thus the following is important to avoid this problem
Always make sure that the
Handle
is already created BEFORE the first access on a thread other than the UI thread.According to MSDN you just need to reference
control.Handle
in the UI thread to force it being created - in your code this must happen BEFORE the very first time you access that control/form from any thread that is not the UI thread.[/UPDATE]