How to run a piece of code in a different thread

Star79
Star79 used Ask the Experts™
on
Hi,

I am running into a issue when i try to read the excel file when it already opened.
C# code is asking me to close the excel file.
So, i decided to run the block of code which reads and manipulates the excel data in
a different thread hoping, it wont ask me to close the excel file if it is already opened.

void StartThread()
        {
            Thread t = new Thread(new ThreadStart(this.ThreadJob));
             t.IsBackground = true;
             t.Start();
         }

 public void ThreadJob()
   {
       /// some execution statements
   }

Here threadjob contains the code which opens the excel file using infragistics controls and reads the data into datatable and manipulates it. If threadjob has simple statements then not a problem i can run it but since it is accessing ole object i get the following error
Ex = {"Current thread must be set to single thread apartment (STA) mode before OLE calls can be made. Ensure that your Main function has STAThreadAttribute marked on it. This exception is only raised if a debugger is attached to the process."}

So i added STAThread before the procedure no luck, deleted the debug dlls and then tried no luck....can someone give me a piece of code which does all these and once the task is done it disposes the unused objects etcc.... or can give me some other suggestion which doenst need the use of threads at all....

Thanks for your help
Rithesh
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Senior Software Engineer
Top Expert 2009
Commented:
Excel object model isn't thread safe. What it means to your code is that any interaction with Excel (Ole object) has to be done in the main thread of your application (In a  winform is the UI). The STAThreadAttribute will not help.
It is very hard to provide advise without looking at your design or code. But some design advise:
- Read you excel data to objects in main thread.
- Do manipulation in objects thread based or the main thread.
- wirte the results back to Excel in main thread.

Check this link, vent though it talks in a VSTO context, it describes the alternamitve of using ImessageFiler:
http://social.msdn.microsoft.com/Forums/en/vsto/thread/f38edd9b-34dd-4cc0-bad2-71d42b667477

Another way The way other solve this issue: Create a new AppDomain so that you can the main execution thread there, but coding that is very complex and involved and your design has to change to message based system.
Hi,

have you tried using OleDB classes to access your Excel file like a spreadsheet?

J
oops, meant to say 'access your Excel file like a Database'

J

Author

Commented:
I agree with the first solution but  using OleDB classes is not what i am looking for.

Author

Commented:
Perfectly suits for my needs.

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial