哪种方法最好 [英] Which way is the best

查看:63
本文介绍了哪种方法最好的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,

我正在其他人的WinForm应用程序上工作.丢失了诸如SetText,Enable,Visible,AppendText等控件操作.现在,我想做一些改进,并且希望在后台线程中进行.因此,我有两个选择来维护有效的跨线程操作.

1.在控件上使用InvokeRequired属性.
2.将Control.CheckForIllegalCrossThreadCalls属性设置为false.

如果我采用第一种方法,那么我将与现有代码进行大量工作.但是最合适的方法.

如果我选择第二种方法,它将停止所有异常.但这不是最好的方法.

那么根据您的经验,我最适合的选择是什么?评论真的很感激.

Hi all,

I am working on someone else WinForm application. There are lost of control manipulations such as SetText, Enable, Visible, AppendText... Now I want to do some improvements and I want to do it in a background thread. So I have two options to maintain valid cross-thread operations.

1. Use of InvokeRequired property on controls.
2. Set the Control.CheckForIllegalCrossThreadCalls property to false.

If I take the first way, I have lots of work to do with the existing codes. But the most suitable way.

If I take the second option, it''ll stop all the exception. But not the best way to do.

So in your experience what is the most suitable option I have? Comments really appreciate.

推荐答案

两者都不是有效的选项.

第一个是大禁忌.由于它可以掩盖实际问题,因此事情变得更糟.一个应用程序甚至可以成功运行多次,然后又失败,而导致问题的原因将很难找到,几乎是不可能的.

第二个根本不是一个选择.让我们看看.

这只是一个允许代码选择System.Windows.Forms.Control.InvokeSystem.Windows.Forms.Control.BeginInvoke的谓词,因此实际选项使用的是调用,而不是 InvokeRequired. 您不能通过非UI线程调用与UI相关的任何内容.您应该始终使用调用.
现在,InvokeRequired呢?它的需求有限.首先,可以在哪个控件上调用InvokeBeginInvoke无关紧要;它可以是当前正在运行的UI中实际包含的任何控件,也可以是某些UI线程.如果您从任何其他线程调用此谓词,则该谓词将始终返回true,并且如果您在相同UI线程中对其进行调用,则该谓词将始终返回false. .在大多数情况下,您无需调用InvokeRequired就可以知道这一点.仅当您有时可以从同一UI线程(有时从某些其他线程)调用某些方法时,才可能需要此调用.

请参阅我过去的解决方案,在其中详细说明所有内容: Control.Invoke()与.Control.BeginInvoke() [ ^ ].

—SA
Both are not valid options.

First one is big no-no. It makes things worse as it can conceal a real problem. An application can even run successfully many times and later fail, and the cause of the problem will be very hard to find, nearly impossible.

Second one is not an option at all. Let''s see.

This is just a predicate which allows a code to select weather System.Windows.Forms.Control.Invoke or System.Windows.Forms.Control.BeginInvoke, so the real option is using invocation, not InvokeRequired. You cannot call anything related to UI form a non-UI thread. You should always use invocation.
Now, what about InvokeRequired? It has limited need. First of all, it does not matter on which control Invoke or BeginInvoke can be called; it can be any control actually included in currently running UI be some UI thread. This predicate will always return true if you call it from any other thread, and will always return false if you call it in the same UI thread. In most cases, you know that by design, without calling InvokeRequired. This call may be needed only if you have some method can sometimes be called from the same UI thread, sometime from some other thread(s).

See my past solution where I explain all that in detail: Control.Invoke() vs. Control.BeginInvoke()[^].

—SA


选项2仅禁止显示警告.线程可能发生的所有丑陋事件一直在发生.因此,选项2实际上不是选项.

所以先去吧.这是您唯一的一个.
Option 2 only suppresses a warning. All the ugly things that can happen with threading keep happening. Therefore Option 2 actually is not an option.

So go for first. It''s the only one you have.


在不了解细节的情况下,请小心使用后台线程更改UI,您可能会

a)如果用户先更改某些内容然后又更改其他内容并启用了按钮,则会使用户感到困惑-用户很难知道哪个更改影响了按钮的启用状态

b)如果用户先更改某些内容,然后又更改了其他内容,然后启用了按钮,然后单击该按钮,则第二次更改可能会使该按钮不再有效

它会很快变得非常混乱.
Without knowing the details, just be careful about changing the UI using background threads, you may

a) confuse the user, if the user changes something and then changes something else and a button is enabled - it is difficult for the user to know which change affected the buttons enabled status

b)if the user changes something and then changes something else and then a button is enabled and they click on it, the second change may invalidate the button being valid anymore

It can start to get very messy very quickly.


这篇关于哪种方法最好的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆