Hi everyone!
I have an application in C#, that at some point calls a

Microsoft.Office.Interop.Word._Application

Problem is that when that point is reached, it takes ages to load the word app. I thought of creating a thread at the beginning of the program, so that the word app is loaded then. My questions are:
1. Is my thinking correct? and if yes
2. How can I access that thread from within the method?
3. Should I overload the destructor to stop the thread at the end?
4. Is there a better way of doing it?

Put the word app initialization in the overloaded constructor and works fine

Basically, it's a good practice to make such calls in separate threads. But creating even one thread will bring your application to the new level of difficulty. Eventually, you will need to "return" the initialized word object to the rest of the application, and that's where the pain starts. You'll need to synchronize threads to wait each other and so on. So if you have another solution, try it.

But if you'll still choose to multithread your application, that's how it's done in pseudocode (one of many possible ways, in fact):

// Some main thread logic
AutoResetEvent evt = new AutoResetEvent(false); // To synchronize threads
Thread thread = new Thread(function);
thread.Start(); // That's where the new thread will start with your custom function
// Do some other work while we're waiting...
evt.WaitOne(); // Wait for the thread to complete, to retrieve the results
var variable2 = variable; // Using the result...


// ...

public void function()
{
// Do some work in separate thread, initialize word app for example
variable = DoSomething();
evt.Set(); // Now the thread is done, so we need to signal to the main thread, so it can use the result
}

And make sure that your variable is ALWAYS accessed only by one thread at a time (using locks or reset events).

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.