Search code examples
vb.netwinformsbackgroundworkervisible

Backgroundworker freezing when coming against .visible properties


I am experiencing my UI hanging whenever I am trying to set the visibility of a control depending on logic to visible = true or visibile = false it hangs. I could set everything on my form to True in the designer and not have any issues with the backgroundworker handling the logic i put inside it however whenever I have to switch from visible true/false it hangs the application. Is this due to some cross threading occuring where I'm trying to set the visibility on the UI anywhere else but the UI thread? If so, why am I able to change the text properties of my controls and not have any issues but visible is giving me some issue?


Solution

  • Yes, you are crashing due to executing UI commands on a background thread. You can perform whatever non-UI logic you're doing on that thread, and then change the actual value of .visible or other UI controls by using the dispatcher - see: http://msdn.microsoft.com/en-us/library/0b1bf3y3.aspx?cs-save-lang=1&cs-lang=vb#code-snippet-1