Author Topic: Cross Threading Error  (Read 3101 times)

Noe

  • Full Member
  • ***
  • Posts: 205
    • View Profile
Cross Threading Error
« on: December 03, 2013, 06:46:41 PM »
I had been programming for more than 6 months (and learning VB as this is my first contact with it) an application that uses AHMI to capture data and send database results to two PLC's (a Control Logix and a SLC 505). So far has been working really fine using v3.54, but this week has been a nightmare related to VB. I been getting an error message for "Cross-Threading operation not valid..." when trying to write or read labels from another thread. This is something I was not aware of, as never happened before and I just been reading for two days how to solve it, with little success. Too complicated for something simple like changing a text box content (text property) when a serial.datareceived event is triggered, that is to use a delegate method (still not quite getting how it works, but it works). See second method in here:
http://www.dreamincode.net/forums/blog/143/entry-2337-handling-the-dreaded-cross-thread-exception/

I have two questions for anybody who would help me before I loose all my hair:
1. This method solves the write issue, but not when you read. How can you do that?
2. If my application has lots of portions of code like this since day one, why did not happened before? This is what is more disturbing, as I been experimenting "changes" like this for one day to another that cannot be explained.

I already tested my previous versions to see if I changed something but all the previous versions do the same thing. Even took a new hard drive, installed everything from zero and tried again but getting to the same point, so I really do not understand why this is happening.

Any help will be really appreciated.

Archie

  • Administrator
  • Hero Member
  • *****
  • Posts: 5269
    • View Profile
    • AdvancedHMI
Re: Cross Threading Error
« Reply #1 on: December 03, 2013, 07:10:33 PM »
The AdvancedHMI drivers have a property named SynchronizingObject. When you add a driver to the form, this is automatically set to point to the form. The purpose of this is so events will be invoked on the same thread as the UI which allows you to put data in objects such as a label without the need to invoke in the user code.

If you create driver instances in code, it is up to you to either set the SynchronizingObject or to invoke events back to the user thread when necessary.

Noe

  • Full Member
  • ***
  • Posts: 205
    • View Profile
Re: Cross Threading Error
« Reply #2 on: December 10, 2013, 04:30:47 PM »
Thanks Archie. I did not set a driver instance from code, just using your controls. I finally found the problem was a label text change being made by another thread, but this was not being invoked. This portion of the code just runs occasionally, so I believed it was already tested in old versions, but was not.

I really did not understand how the SynchronizingObject would help me when changing text from a label on the main form from another thread. This is a common text label, not a basic label, and I am putting some of the received text from a barcode scanner when serial.datareceived event is triggered (so I suppose this is a new thread). Invoking a new Sub instead running the code directly in the datareceived event solved it. Is there a better way?

Jesse

  • Jr. Member
  • **
  • Posts: 50
    • View Profile
Re: Cross Threading Error
« Reply #3 on: December 31, 2013, 08:52:46 AM »
I'm currently having a similar problem. My goal was when connection was established to make a label say "Connected to Machine 'A' (or B or C etc) depending on the IP address at the current time. Then when a Connection closed to make the same label read "No Connection".  But I got that same cross threading error.
I got around this by writing:
Control.CheckForIllegalCrossThreadCalls = False         in the mainform load sub
(this is bad practice I know)

Now when I cange an IP address my code works for about 5 seconds  and says "Connected" then connection is momentarily lost (changes to "No Connection") and then returned but my IPConnectionEstablished sub doesn't occur again to change the label back to "Connected", but I am communicating.

This problem is really quite the brain buster.