I am trying to display some information about the grid requested from sql server. Data collection can take about 10 seconds, so I do not want to block the user interface flow.
I currently have the code:
ThreadPool.QueueUserWorkItem(DataUpdateThread, new UpdateParams(year)); private struct UpdateParams { internal string year; internal UpdateParams(string year) { this.year = year; } } private void DataUpdateThread(object state) { DataTable dTable = new DataTable(); try { this.Invoke((MethodInvoker)delegate {
This works most of the time, in addition, if the form in which it is located closes before the update is completed, it will fail with an error:
Invoke or BeginInvoke cannot be called in the control until a window handle is created.
I understand that the error will be caused by the fact that the form no longer exists, therefore, when the finally section tries to call the method in the user interface thread, it cannot.
Is there a better way to do all this? I suppose I can handle invoke errors, but it looks messy, and I think I probably skipped the simpler way.
PeteT source share