I have an app that is very basic so far. Its current goal is to read a lot of data about files in a particular directory and it's sub directories. This can take a while if it's like the C:\ directory.
So far it's just has:
Toolstrip (has a combo with directories and a button to start the scan)
StatusStrip (with a ToolStripStatusLabel on it)
Pretty basic. As you process each directory I want it to display text indicating which directory is being scanned on the ToolStripStatusLabel.
The First Problem:
Scanning ties up the thread, so it never actually writes any text to the ToolStripStatusLabel.
I solved this problem by adding a BackgroundWorker to run the scan and this works pretty well. The scan runs, text is displayed on the ToolStripStatusLabel as it should. The user will see directories being worked on as the app works through them.
I understand that I'm access the ToolStripStatusLabel from another thread and the application seems fine with it... normally.
The New Problem:
Now if I change the size of the window (which I could see a user doing to see more of the directory names or to make the app smaller) I get an exception:
System.InvalidOperationException: Cross-Thread operation not valid: Control 'AppToolStrip' accessed from a thread it was created on. and it continues to refer me to the line the exception is on, which is where I'm setting the text for the ToolStripStatusLabel.
Now the part that throws me for a loop is that the line is assigning text to the ToolStripStatusLabel (which is on the StatusStrip) while the exception says that there is a cross-thread problem with the ToolStrip control, which there isn't any code manipulating.
I don't get why this only happens when the application is resized at runtime during a scan and not when it's left alone.
Any clues or workarounds?
So far it's just has:
Toolstrip (has a combo with directories and a button to start the scan)
StatusStrip (with a ToolStripStatusLabel on it)
Pretty basic. As you process each directory I want it to display text indicating which directory is being scanned on the ToolStripStatusLabel.
The First Problem:
Scanning ties up the thread, so it never actually writes any text to the ToolStripStatusLabel.
I solved this problem by adding a BackgroundWorker to run the scan and this works pretty well. The scan runs, text is displayed on the ToolStripStatusLabel as it should. The user will see directories being worked on as the app works through them.
I understand that I'm access the ToolStripStatusLabel from another thread and the application seems fine with it... normally.
The New Problem:
Now if I change the size of the window (which I could see a user doing to see more of the directory names or to make the app smaller) I get an exception:
System.InvalidOperationException: Cross-Thread operation not valid: Control 'AppToolStrip' accessed from a thread it was created on. and it continues to refer me to the line the exception is on, which is where I'm setting the text for the ToolStripStatusLabel.
Now the part that throws me for a loop is that the line is assigning text to the ToolStripStatusLabel (which is on the StatusStrip) while the exception says that there is a cross-thread problem with the ToolStrip control, which there isn't any code manipulating.
I don't get why this only happens when the application is resized at runtime during a scan and not when it's left alone.
Any clues or workarounds?