0

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

8
  • 2
    Probably an STA issue - where your COM object executes all its methods on the main thread - see stackoverflow.com/q/804968/5427 Commented Jul 15, 2013 at 13:33
  • The posted code is fine. Can we see the contents of LoadFileForViewing? Commented Jul 15, 2013 at 13:36
  • When is showFile being called? Not sure cause you didn't show that part of the code, but presumably you'd call it after your BackgroundWorker completes. I believe by default there's a stub for it, RunWorkerCompleted. Also, are you sure there isn't a typing mismatch when you're calling your COM function? You said the thread locks so this may not be of use, but try wrapping some of your problem code with try catch and see if you can spot anything off. Commented Jul 15, 2013 at 13:37
  • Only thing LoadFileForViewing() does is a simple File.Exist() check to make sure the filePath exists before sending the sting to showFile() inside the Usercontrol Commented Jul 15, 2013 at 13:40
  • 1
    The call to axoixctrl_Viewer.ViewFile is being invoked on the UI thread. Commented Jul 15, 2013 at 13:41

1 Answer 1

0

After some research I figured out that the reason I am having this issue. It is because of a STA issue as posted by morechilli. The COM object I am using can only be used Single Threaded. Thanks for all the comments

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.