Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

eliminate cross thread errors in vb.net

Posted on 2013-10-28
8
Medium Priority
?
706 Views
Last Modified: 2013-10-28
Hi,

I have a windows form app in VB.net, whereby the main form instantiates a class, Rtu, that has events. I added events to Rtu in order to get a MainForm textbox to display messages from the Rtu class.  Not sure if there's a better way send messages.

In any event, in the Rtu init I raise an event which is handled by the mainform class method messageHandler.  I added it like this:
            AddHandler rtu.broadcast, _
             AddressOf Me.messageHandler

Open in new window

This worked fine on init, but later down the road I encountered a problem and got this error in the messageHandler callback:

System.InvalidOperationException was unhandled
  Message=Cross-thread operation not valid: Control 'displayTextbox' accessed from a thread other than the thread it was created on.
Private Sub messageHandler(ByVal e As String)
    Try
        displayTextbox.Text = e
    Catch ex As Exception
        displayTextbox.Text = "Command execution failed "
    End Try
End Sub

Open in new window

I looked up the error and found what I thought was a solution here:
http://msdn.microsoft.com/en-us/library/ms171728(v=vs.85).aspx
which talks about thread-safe ways to access components and gives code, but
there was better source code here which I used:
http://www.vbforums.com/showthread.php?575929-Type-SetTextCallback-is-not-defined

The problem is that when the line
Me.Invoke(d, New Object() {msg}) 

Open in new window

is executed, the program just hangs.

How can I fix this?
0
Comment
Question by:ugeb
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 3
8 Comments
 
LVL 70

Expert Comment

by:Éric Moreau
ID: 39605230
0
 
LVL 86

Expert Comment

by:Mike Tomlinson
ID: 39605429
"the program just hangs"

A hang on Invoke() indicates deadlock, which means the main UI thread is unable to service the Invoke() request.  This can happen if you've improperly used SyncLock(), or possibly if you've got a polling loop running in the main UI thread.  There are other possibilities...we'd need to see more code to diagnose.
0
 
LVL 11

Author Comment

by:ugeb
ID: 39606335
I do check invokeRequired, which is why I have an invoke statement later on.  This is the code block from mainform:
    Private Delegate Sub SetTextCallback(ByVal [text] As String)

    Private Sub messageHandler(ByVal msg As String)
        Try
            If Me.displayTextbox.InvokeRequired Then
                Dim d As New SetTextCallback(AddressOf messageHandler)
                Me.Invoke(d, New Object() {msg})
            Else
                Me.displayTextbox.Text = msg
            End If
        Catch ex As Exception
            displayTextbox.Text = "Command execution failed "
        End Try
    End Sub

Open in new window

I have a DDE (Ndde library) system running in the rtu instance which raises the event every time there's a call back.  It runs asynchronously and calls a method in rtu (whenever it has data) which in turn raises the event.
I left the catch block as invalid code on purpose while I'm debugging.  It never gets called.
0
Certified OpenStack Administrator Course

We just refreshed our COA course based on the Newton exam.  With 14 labs, this course goes over the different OpenStack services that are part of the certification: Dashboard, Identity Service, Image Service, Networking, Compute, Object Storage, Block Storage, and Orchestration.

 
LVL 86

Accepted Solution

by:
Mike Tomlinson earned 2000 total points
ID: 39606379
There's nothing wrong with that code.  You can try switching to BeginInvoke() to see if that makes a difference.

If not, then either you have code somewhere else that is mis-behaving (see my previous comment), or the Ndde library is the bad apple.
0
 
LVL 11

Author Comment

by:ugeb
ID: 39606513
Yes, BeginInvoke seemed to eliminate the deadlock.  Is there any housekeeping I need to do by using BeginInvoke instead of Invoke?  Will there be memory or other issues?
0
 
LVL 86

Expert Comment

by:Mike Tomlinson
ID: 39606540
None that I can think of.  Invoke() is synchronous, while BeginInvoke() is asynchronous.  Other than that, they're the same...
0
 
LVL 11

Author Closing Comment

by:ugeb
ID: 39606612
Thank you, that did the trick!
0

Featured Post

Learn by Doing. Anytime. Anywhere.

Do you like to learn by doing?
Our labs and exercises give you the chance to do just that: Learn by performing actions on real environments.

Hands-on, scenario-based labs give you experience on real environments provided by us so you don't have to worry about breaking anything.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Welcome my friends to the second instalment and follow-up to our Minify and Concatenate Your Scripts and Stylesheets (http://www.experts-exchange.com/Programming/Languages/.NET/ASP.NET/A_4334-Minify-and-Concatenate-Your-Scripts-and-Stylesheets.html)…
Many of us here at EE write code. Many of us write exceptional code; just as many of us write exception-prone code. As we all should know, exceptions are a mechanism for handling errors which are typically out of our control. From database errors, t…
Add bar graphs to Access queries using Unicode block characters. Graphs appear on every record in the color you want. Give life to numbers. Hopes this gives you ideas on visualizing your data in new ways ~ Create a calculated field in a query: …
In this video, Percona Solution Engineer Dimitri Vanoverbeke discusses why you want to use at least three nodes in a database cluster. To discuss how Percona Consulting can help with your design and architecture needs for your database and infras…

705 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question