I have an issue with loading data into a COM object in a background thread.
I have some files that are taking a long time to load and when this happens my application locks up and becomes unusable. Because of this I am trying to load the data in a background thread, BackgroundWorker object.
I traced into my code and placed a generic Thread.Sleep() in code behind for my Usercontrol that is loading the file. I am able use the application during the Sleep() call but when I replace the Sleep() call with my LoadFile() call it locks the UI thread.
Here is the simplified version of the code that I am calling:
private void CurrentDocumentChangedEvents()
{
//fire event for native file viewer
var worker = new BackgroundWorker();
worker.DoWork += worker_DoWork;
worker.RunWorkerAsync();
gridView1.Focus();
}
void worker_DoWork(object sender, DoWorkEventArgs e)
{
//Call to UserControl to load the file selected.
LoadFileForViewing(filePath);
}
public void showFile(string filePath)
{
//Method inside UserControl that loads my selected file
Thread.Sleep(10000);
//axoixctrl_Viewer.ViewFile(false, filePath);
}
axoixctrl_Viewer is the COM object that is loaded into my Usercontrol. The usercontrol lives in the UI thread, it is initialized when the Form Loads. Is the problem related to creating the COM object on the UI thread or is my threading wrong?
Thanks
LoadFileForViewing?axoixctrl_Viewer.ViewFileis being invoked on the UI thread.