I want to update a table on a separate thread regularly using BackgroundWorker so the user will see the updated data refresh every 30 seconds or so and because there can often be a *lot of data to parse so if they wait for a while to push an update button it can take quite a while for all the data to be parsed before it will be displayed.
I had it all working quite nicely, that is until the vScroll_Scroll event fires when a row needs updating, which causes a crash because VScroll is on a different thread, so I get a crossthreading error. Or should I be employing delegation to cross the thread boundaries instead?
Any suggestions?
Error below
Code: Select all
vScroll.Visible=true;
Code: Select all
System.InvalidOperationException
HResult=0x80131509
Message=Cross-thread operation not valid: Control '' accessed from a thread other than the thread it was created on.
Source=System.Windows.Forms
StackTrace:
at System.Windows.Forms.Control.get_Handle()
at System.Windows.Forms.Control.SetVisibleCore(Boolean value)
at System.Windows.Forms.Control.set_Visible(Boolean value)
at OpenDental.UI.GridOD.LayoutScrollsAndFonts() in C:\Users\wjs\Desktop\ODVersions\opendental20.3\opendental20.3\OpenDentBusiness\UI\GridOD.cs:line 2868
at OpenDental.UI.GridOD.EndUpdate(Boolean preserveIndices) in C:\Users\wjs\Desktop\ODVersions\opendental20.3\opendental20.3\OpenDentBusiness\UI\GridOD.cs:line 3349
at Anesthesia.FormAnestheticRecord.FillGridVSData(Int64 anestheticRecordNum, String anestheticDate) in C:\Users\wjs\Desktop\ODVersions\opendental20.3\EASy20.3\Forms\FormAnestheticRecord.cs:line 1225
at Anesthesia.FormAnestheticRecord.timerVSMStatus_Tick(Object sender, EventArgs e) in C:\Users\wjs\Desktop\ODVersions\opendental20.3\EASy20.3\Forms\FormAnestheticRecord.cs:line 3464
at Anesthesia.FormAnestheticRecord.<>c__DisplayClass7_0.<FormAnestheticRecord_Load>b__0() in C:\Users\wjs\Desktop\ODVersions\opendental20.3\EASy20.3\Forms\FormAnestheticRecord.cs:line 219
at System.Threading.ThreadHelper.ThreadStart_Context(Object state)
at System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
at System.Threading.ThreadHelper.ThreadStart()
This exception was originally thrown at this call stack:
[External Code]
OpenDental.UI.GridOD.LayoutScrollsAndFonts() in GridOD.cs
OpenDental.UI.GridOD.EndUpdate(bool) in GridOD.cs
Anesthesia.FormAnestheticRecord.FillGridVSData(long, string) in FormAnestheticRecord.cs
Anesthesia.FormAnestheticRecord.timerVSMStatus_Tick(object, System.EventArgs) in FormAnestheticRecord.cs
Anesthesia.FormAnestheticRecord.FormAnestheticRecord_Load.AnonymousMethod__0() in FormAnestheticRecord.cs
[External Code]