简体   繁体   English

如何在调试时可靠地获取Visual Basic .Net中的ComboBox.Text值?

[英]How can I reliably get ComboBox.Text value in Visual Basic .Net while debugging?

AFAIK this should be a very simple task, but after 2 different attempts I'm still unable to verify the current value of a ComboBox in Visual Basic .Net 4.5. AFAIK这应该是一个非常简单的任务,但经过两次不同的尝试后,我仍无法验证Visual Basic .Net 4.5中ComboBox的当前值。

How can I get it reliably? 我怎样才能可靠地获得它? My previous 2 attempts in detail below so that you can hopefully show me where my error is. 我之前的2次尝试详细说明,以便您可以告诉我我的错误在哪里。

Attempt 1(a): 尝试1(a):

Attempt accessing the MyComboBox.Text property from the Watch window while debugging in Visual Studio Express 2013. Instead of retrieving the text property, I get this message in the Watch Window: 在Visual Studio Express 2013中调试时尝试从Watch窗口访问MyComboBox.Text属性。我在Watch Window中收到此消息,而不是检索text属性:

An exception 'Microsoft.VisualStudio.Debugger.Runtime.CrossThreadMessagingException' occurred . An exception 'Microsoft.VisualStudio.Debugger.Runtime.CrossThreadMessagingException' occurred

This seems to be "normal" behaviour and workarounds are described at a helpful article on microsoft.com [1]. 这似乎是“正常”行为,解决方法在microsoft.com上的一篇有用的文章中有所描述[1]。

Attempt 1(b): 尝试1(b):

Adding the line Control.CheckForIllegalCrossThreadCalls = False as described at [1] did not help. 如[1]中所述添加Control.CheckForIllegalCrossThreadCalls = False这一行并没有帮助。 I included this line in the New() Sub of my form code and verified that Control.CheckForIllegalCrossThreadCalls was indeed set to False at run time by adding it as an expression to the watch window. 我将此行包含在我的表单代码的New() Sub中,并通过将其作为表达式添加到监视窗口来验证Control.CheckForIllegalCrossThreadCalls在运行时确实设置为False There was no change in behaviour and I received the same error message as listed in Attempt 1 above. 行为没有变化,我收到了与上面步骤1中列出的相同的错误消息。

Attempt 2: 尝试2:

By following that first article [1] I added the following code to my form module: 通过关注第一篇文章[1],我将以下代码添加到我的表单模块中:

Delegate Function GetBoardTextCallback() As String
Private Function GetBoardText() As String
    If Me.BoardBox.InvokeRequired Then
        Dim d As New GetBoardTextCallback(AddressOf GetBoardText)
        Return Me.Invoke(d)
    Else
        Return Me.BoardBox.Text
    End If
End Function

Now in my Watch Window when I add GetBoardText() there is a delay of about 5 seconds and then a message appears in the Value column about Unable to Evaluate Expression, then after I hit refresh on the expression I get this message: Cannot evaluate expression because we are stopped in a place where garbage collection is impossible, possibly because the code of the current method may be optimized. 现在在我的Watch Window中,当我添加GetBoardText()会有大约5秒的延迟,然后在Value列中显示一条消息,指出无法评估表达式,然后在表达式上点击刷新后我收到此消息: Cannot evaluate expression because we are stopped in a place where garbage collection is impossible, possibly because the code of the current method may be optimized.

I am new to delegates so I suspect my implementation has something wrong with it. 我是新来的代表,所以我怀疑我的实现有问题。 I must admit I'm a little baffled as to why simply reading the value of a form element needs to be so complicated (even though the concept of separate threads for visual elements seems straight forward). 我必须承认,为什么简单地阅读表单元素的值需要如此复杂(即使视觉元素的单独线程的概念看起来很简单),我有点困惑。 How Can I reliably get (let alone set!) the Text property of a ComboBox in Visual Basic .Net in a way that works while debugging in my Watch Window, as well as (hopefully) also working as an expression I can use anywhere in my code?! 我如何可靠地获取(更不用说设置!)Visual Basic .Net中ComboBox的Text属性,这种方式在我的Watch Window中调试时有效,并且(希望)也可以作为我可以在任何地方使用的表达式我的代码?! Am I going to have to create a new and unique delegate function for each and every get/set/element member combination for all my form elements? 我是否必须为所有表单元素的每个get / set / element成员组合创建一个新的唯一委托函数? Please no! 请不!

EDIT (Attempt 2 code verified as threadsafe): On further investigation I've verified many times that my code sample in Attempt 2 really does work, but there must be some crucial error in my understanding of the VS 2013 debugger. 编辑(尝试2代码验证为线程安全):在进一步调查中,我已多次验证我的代码示例在尝试2中确实有效,但在理解VS 2013调试器时必定存在一些重要错误。 If the reason that Watch Window expressions were failing was because of cross-threading problems, then they should work if calling my thread-safe getters (after all, those getters work just fine for other threads at run-time that didn't create the form controls). 如果监视窗口表达式失败的原因是由于交叉线程问题,那么它们应该在调用我的线程安全getter时工作(毕竟,那些getter在运行时对其他线程工作得很好而没有创建形式控制)。 So what is so unique about the debugger's thread(s) that they can't use those thread-safe functions that I wrote?! 那么调试器的线程有什么独特之处呢?他们不能使用我写的那些线程安全函数?! Addressing that problem is really the answer to this question. 解决这个问题确实是这个问题的答案。

EDIT (bugs in debugger tools?): Also, according to other MS documentation [2] there are tools in the debugger for debugging multi-threaded applications. 编辑(调试工具中的错误?):另外,根据其他MS文档[2],调试器中有调试多线程应用程序的工具。 However adding watch expressions Me.BoardBox.Text and GetBoardText() for all available threads all produce the same result. 但是 ,为所有可用线程添加监视表达式Me.BoardBox.TextGetBoardText()都会产生相同的结果。 This is more evidence IMO that there is something weird/unique about the debuggers access to thread memory or it's process. 这更有证据表明,调试器访问线程内存或它的进程有一些奇怪/独特的东西。 If it were just a "debugger is looking at the wrong thread" type problem, then the multi-threading debugging options should have fixed it. 如果它只是“调试器正在查看错误的线程”类型问题,那么多线程调试选项应该已经修复了它。

[1] http://msdn.microsoft.com/en-us/library/ms171728(VS.80).aspx [2] http://msdn.microsoft.com/en-us/library/ms164739.aspx [1] http://msdn.microsoft.com/en-us/library/ms171728(VS.80).aspx [2] http://msdn.microsoft.com/en-us/library/ms164739.aspx

When you are debugging across threads, you will not be able to get the value of something on another thread. 当您跨线程调试时,您将无法在另一个线程上获取某些内容的值。 The Invoke method shown in your question will allow your code to get the value from the ComboBox, but doesn't apply when you are debugging. 您的问题中显示的Invoke方法将允许您的代码从ComboBox获取值,但在调试时不适用。

From Debug Multithreaded Applications in Visual Studio Visual Studio中的调试多线程应用程序

Debugging a multithreaded application that has a user interface can be especially difficult. 调试具有用户界面的多线程应用程序可能特别困难。 In that case, you might consider running the application on a second computer and using remote debugging. 在这种情况下,您可以考虑在另一台计算机上运行该应用程序并使用远程调试。 For information, see Remote Debugging in Visual Studio . 有关信息,请参阅Visual Studio中的远程调试

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM