Results 1 to 6 of 6

Thread: [2005] Interesting Cross thread situation

  1. #1

    Thread Starter
    PowerPoster JuggaloBrotha's Avatar
    Join Date
    Sep 2005
    Location
    Lansing, MI; USA
    Posts
    4,286

    [2005] Interesting Cross thread situation

    I have a Background Worker doing some work, when the code starts processing the next file I have it show the name in a StatusStripLabel on the StatusStrip. This works fine, I don't need to check if an Invoke is required or anything (The control actually doesn't have the Invoke stuff in it like labels, forms, textboxes do)

    Out of the blue when the background worker was running, I minimized the window. When I resorted the window half a minute later, the application crashed and gave me a 'Cross thread violation' exception. Now my question is: How do I use a delegate sub to check if an Invoke is required on a control that is thread safe, but will still throw a cross thread error?

    Or, is it thread safe to check Me.WindowState without invoking it before assigning a value to the text property of the StatusStripLabel?

    Thoughts? Opinions?
    Currently using VS 2015 Enterprise on Win10 Enterprise x64.

    CodeBank: All ThreadsColors ComboBoxFading & Gradient FormMoveItemListBox/MoveItemListViewMultilineListBoxMenuButtonToolStripCheckBoxStart with Windows

  2. #2
    Raging swede Atheist's Avatar
    Join Date
    Aug 2005
    Location
    Sweden
    Posts
    8,018

    Re: [2005] Interesting Cross thread situation

    You shouldnt generally need to do any invoking when using the BackgroundWorker because its designed so you should call ReportProgress and do all control interaction in the ProgressChanged event handler.

    If you still need to use invoke here, try calling the StatusStrips invoke method.
    Rate posts that helped you. I do not reply to PM's with coding questions.
    How to Get Your Questions Answered
    Current project: tunaOS
    Me on.. BitBucket, Google Code, Github (pretty empty)

  3. #3

    Thread Starter
    PowerPoster JuggaloBrotha's Avatar
    Join Date
    Sep 2005
    Location
    Lansing, MI; USA
    Posts
    4,286

    Re: [2005] Interesting Cross thread situation

    The ReportProgress method doesn't let me pass a string, it only accepts and integer (the percent)

    I'll see about Invoking the status strip itself
    Currently using VS 2015 Enterprise on Win10 Enterprise x64.

    CodeBank: All ThreadsColors ComboBoxFading & Gradient FormMoveItemListBox/MoveItemListViewMultilineListBoxMenuButtonToolStripCheckBoxStart with Windows

  4. #4
    Raging swede Atheist's Avatar
    Join Date
    Aug 2005
    Location
    Sweden
    Posts
    8,018

    Re: [2005] Interesting Cross thread situation

    Quote Originally Posted by JuggaloBrotha
    The ReportProgress method doesn't let me pass a string, it only accepts and integer (the percent)

    Also the ToolStripStatusLabel doesn't have an Invoke method, it doesn't have an InvokeRequired either
    Actually, ReportProgress has a second overload that accepts 2 arguments; an integer and an object.

    If you look at my last post you see that I suggested to call Invoke on the StatusStrip control, not the StatusStripLabel
    Rate posts that helped you. I do not reply to PM's with coding questions.
    How to Get Your Questions Answered
    Current project: tunaOS
    Me on.. BitBucket, Google Code, Github (pretty empty)

  5. #5

    Thread Starter
    PowerPoster JuggaloBrotha's Avatar
    Join Date
    Sep 2005
    Location
    Lansing, MI; USA
    Posts
    4,286

    Re: [2005] Interesting Cross thread situation

    Quote Originally Posted by Atheist
    Actually, ReportProgress has a second overload that accepts 2 arguments; an integer and an object.
    Didn't see the 'object' part, I really should be using a larger monitor here

    Thanks for the help, invoking the status strip didn't sound like a fun idea.
    Currently using VS 2015 Enterprise on Win10 Enterprise x64.

    CodeBank: All ThreadsColors ComboBoxFading & Gradient FormMoveItemListBox/MoveItemListViewMultilineListBoxMenuButtonToolStripCheckBoxStart with Windows

  6. #6
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: [2005] Interesting Cross thread situation

    You might want to have a quick look at my BackgroundWorker example code. Just follow the link in my signature.

    Also, even if you're using a BackgroundWorker you'll still need to use delegation sometimes. ReportProgress/ProgressChanged is fine if you want to do the same thing every time, but if you want to be able to do a few different things on the UI from the background thread then it becomes cumbersome.

    The reason that the ToolStripStatusLabel has no Invoke method is that it isn't a control. Invoke is inherited from the Control class, so the StatusStrip class DOES have an Invoke method.

    The issue with cross-thread calls to controls is accessing the Handle. Presumably the Handle of your StatusStrip was not accessed until you minimised and restored the form.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width