473,407 Members | 2,306 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,407 software developers and data experts.

Pointer scrambled when running in a thread

DBW
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?
Nov 16 '05 #1
2 984
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]
- mv*@spam.guard.caspershouse.com

"DBW" <DB*@discussions.microsoft.com> wrote in message
news:A5**********************************@microsof t.com...
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?

Nov 16 '05 #2
DBW
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 [.NET/C# MVP]" wrote:
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]
- mv*@spam.guard.caspershouse.com

"DBW" <DB*@discussions.microsoft.com> wrote in message
news:A5**********************************@microsof t.com...
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?


Nov 16 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

16
by: jacob navia | last post by:
Valid pointers have two states. Either empty (NULL), or filled with an address that must be at a valid address. Valid addresses are: 1) The current global context. The first byte of the data...
4
by: JS | last post by:
I have a file called test.c. There I create a pointer to a pcb struct: struct pcb {   void *(*start_routine) (void *);   void *arg;   jmp_buf state;   int    stack; }; ...
0
by: Ryan Mitchley | last post by:
I am compiling under Visual C++ 2005 Express (with the Platform SDK installed). I have a class that is responsible for creating and repeatedly running a Timeslice() method using a thread: ...
26
by: Bill Reid | last post by:
Bear with me, as I am not a "professional" programmer, but I was working on part of program that reads parts of four text files into a buffer which I re-allocate the size as I read each file. I...
33
by: Ney André de Mello Zunino | last post by:
Hello. I have written a simple reference-counting smart pointer class template called RefCountPtr<T>. It works in conjunction with another class, ReferenceCountable, which is responsible for the...
9
by: Dave Stallard | last post by:
Pardon if this is the wrong newsgroup for this question, and/or if this question is naive. I have a multi-threaded Windows application in which certain variables/object fields are shared: one...
17
by: Mike | last post by:
Hello, I have following existing code. And there is memory leak. Anyone know how to get ride of it? function foo has been used in thousands places, the signature is not allowed to change. ...
7
by: jc | last post by:
i'm developing a project using vc++. the main exe is a win32 application. it needs two dlls. one is my own implementation of string operations. the other dll is to parse a2l files(it is similar to...
2
weaknessforcats
by: weaknessforcats | last post by:
Handle Classes Handle classes, also called Envelope or Cheshire Cat classes, are part of the Bridge design pattern. The objective of the Bridge pattern is to separate the abstraction from the...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.