Pointer scrambled when running in a thread

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

I am using a com server to return a memory pointer to a C# program in .net.
The code works fine when it is not in a thread. When it is run in a thread
the pointer no longer points to the data the com server has.

I am using a System.IntPtr to transfer the pointer.

Are there marshaling issues?
When you look at the memory in the debugger it appears to be garbage.
What other things do I need to consider when doing this?
 
DBW,

You can not run code outside of a thread. There is always a thread that
is executing your code. Chances are you mean outside of the UI thread.
When you create the new thread, set the apartment state of the thread to STA
and it will probably work. You have to make sure you set the ApartmentState
property before any interop calls are made.

Hope this helps.
 
Thanks for the suggestion. The thread apartment state was already initialized.
Here is the initialization.

m_threadModal = new Thread(new ThreadStart(ShowModal));
m_threadModal.IsBackground = true;
m_threadModal.Priority = ThreadPriority.Highest;
m_threadModal.Name = "Bluto";
m_threadModal.ApartmentState = ApartmentState.STA;
m_threadModal.Start();

Any thourghts


Nicholas Paldino said:
DBW,

You can not run code outside of a thread. There is always a thread that
is executing your code. Chances are you mean outside of the UI thread.
When you create the new thread, set the apartment state of the thread to STA
and it will probably work. You have to make sure you set the ApartmentState
property before any interop calls are made.

Hope this helps.


--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

DBW said:
I am using a com server to return a memory pointer to a C# program in .net.
The code works fine when it is not in a thread. When it is run in a thread
the pointer no longer points to the data the com server has.

I am using a System.IntPtr to transfer the pointer.

Are there marshaling issues?
When you look at the memory in the debugger it appears to be garbage.
What other things do I need to consider when doing this?
 
Back
Top