Is it good idea to use “Control.CheckForIllegalCro

2019-01-15 10:39发布

I have a class that receives data from serialport. i used action<T> delegate to pass data to the form where it is displayed in a textbox. the thing is i could not access the textbox control, becouse it says: Cross-thread operation not valid: Control accessed from a thread other than the thread it was created on.

so i set Control.CheckForIllegalCrossThreadCalls = false, and it is working.

is it good idea to do that? or there is a better way of doing it.

Thanks

4条回答
相关推荐>>
2楼-- · 2019-01-15 11:02

Will you write something like below

       try
        {
            Object obj=null;
            var result = obj.ToString();
        }
        catch (Exception )
        {


        }

I am sure your answer would be NO , similar thing is with Control.CheckForIllegalCrossThreadCalls = false, it will just eating exception but present unknown results to your estimated clients

查看更多
甜甜的少女心
3楼-- · 2019-01-15 11:03

No it's not. A good idea would have been to put your error into a google search. If you had clicked the first link, you would have found a post here on SO that explained it. This way, you only produced another duplicate.

查看更多
迷人小祖宗
4楼-- · 2019-01-15 11:03

Not a good idea to do that.

I believe the preferred method is to check to see if Control.InvokeRequired == true, and if so then use Control.Invoke with the proper delegate, which will marshall the method call onto the UI thread.

查看更多
Rolldiameter
5楼-- · 2019-01-15 11:05

While it may appear to work most of the time, it is sure to fail every now and then.

If you need to access/modify the UI control from another thread, use Control.Invoke.

查看更多
登录 后发表回答