Is there a way to run code in UI thread while calling it outside of it? I know that it basically requires the use of control.Invoke
or control.BeginInvoke
. My problem is that i don't have access to UI elements to use those methods.
My situation is like this:
- UI - DataGridView with DataTable set as it's DataSource
- thread (BackgroundWorker) written outside of MainForm UI, that updates (adds rows) DataTable mentioned above
I've had problems with my app, so i've written a simple app with just the elements mentioned above, to check how it works (or rather why it doesn't) and I found that:
- when I update DataTable in my BackgroundWorker thread, DataGridview can't see this (probably all the events are fired from other thread), even calling DGV.Refresh() doesn't work (it updates DGV, but sometimes DGV gets broken and shows nothing or just 1 row), only forcing DGV refreshing with resizing, scrolling etc. forces DGV to show data from DataTable
- when I am able to run Invoke, everything works fine, nothing more to say
but this is just a simple app, where I have everything in a single file. what can i do with much greater app where my "other thread" can't see UI code? do I have to pass a single control as a parameter somehow, just to get access to that thread? or is there maybe some other, much better looking solution?