I'm writing a WinForms app which has two modes: console or GUI. Three projects within the same solution, one for the console app, one for the UI forms and the third to hold the logic that the two interfaces will both connect too. The Console app runs absolutely smoothly.
A model which holds the user-selections, it has an IList<T>
where T is a local object, Step
, which implements INotifyPropertyChanged
, so in the UI this is mounted on to a DataGridView. All is fine at runtime, the initial state of the objects is reflected on the screen.
Each of the Step
objects is a task which is performed in turn; some of the properties will change, being reflected back to the IList and passed on to the DataGridView.
This action in the UI versions is done by creating a BackgroundWorker raising events back to the UI. The Step
does it thing and generates a StepResult
object which is an enumerated type indicating a result (e.g. Running, NotRun, OK, NotOK, Caveat) and a string to indicate a message (because the step ran but not quite as expected, i.e. with a Caveat). Normally the actions will involve a database interaction, but in debug mode I randomly generate a result.
If the message is null, there's never a problem, but if I generate a response like this:
StepResult returnvalue = new StepResult(stat, "completed with caveat")
I get an error saying that the DataGridView was being accessed from a thread other than the thread it was created on. (I'm passing this through a custom handler which should handle the invoking when required - maybe it doesn't?)
Then if I generate a unique response, e.g. using a random number r
:
StepResult returnvalue = new StepResult(stat, r.ToString());
the actions succeed with no problem, the numbers are written cleanly to the DataGridView.
I'm baffled. I'm assuming it's somehow a string literal problem, but can anyone come up with a clearer explanation?