简体   繁体   中英

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.

How can I get it reliably? My previous 2 attempts in detail below so that you can hopefully show me where my error is.

Attempt 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:

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].

Attempt 1(b):

Adding the line Control.CheckForIllegalCrossThreadCalls = False as described at [1] did not help. 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. There was no change in behaviour and I received the same error message as listed in Attempt 1 above.

Attempt 2:

By following that first article [1] I added the following code to my form module:

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.

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?! 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? 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. 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). 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. However adding watch expressions Me.BoardBox.Text and GetBoardText() for all available threads all produce the same result. 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

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.

From Debug Multithreaded Applications in 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 .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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