S
Scott
namespace TestProg
{
class Globals
{
public static Int32 percent_done;
private static GCHandle percent_done_handle;
public static IntPtr percent_done_pointer;
}
public static void Init()
{
percent_done = 0;
percent_done_handle = GCHandle.Alloc(percent_done, GCHandleType.Pinned);
percent_done_pointer = percent_done_handle.AddrOfPinnedObject();
}
public static void Cleanup()
{
percent_done_handle.Free();
}
static class Program
{
[STAThread]
static void Main()
{
Globals.Init();
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new Form1());
Globals.Cleanup();
}
}
}
// Form1 spawns a thread that then passes percent_done_pointer to some
unmaged C++ that takes a really long time to execute.
// The C++ code will occasionally update percent_done through
percent_done_pointer.
// Another thread in this C# sharp program is polling the value of
percent_done and updating a progress bar on Form1.
// The problem is that percent_done does not change from the viewpoint of
the C# program.
// In the watch window it remains at 0, yet I can see the memory pointed to
by percent_done_pointer changing.
// I guess the only conclusion I can reach is that percent_done_pointer
points to some memory that is a copy of percent_done.
// But why?
{
class Globals
{
public static Int32 percent_done;
private static GCHandle percent_done_handle;
public static IntPtr percent_done_pointer;
}
public static void Init()
{
percent_done = 0;
percent_done_handle = GCHandle.Alloc(percent_done, GCHandleType.Pinned);
percent_done_pointer = percent_done_handle.AddrOfPinnedObject();
}
public static void Cleanup()
{
percent_done_handle.Free();
}
static class Program
{
[STAThread]
static void Main()
{
Globals.Init();
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new Form1());
Globals.Cleanup();
}
}
}
// Form1 spawns a thread that then passes percent_done_pointer to some
unmaged C++ that takes a really long time to execute.
// The C++ code will occasionally update percent_done through
percent_done_pointer.
// Another thread in this C# sharp program is polling the value of
percent_done and updating a progress bar on Form1.
// The problem is that percent_done does not change from the viewpoint of
the C# program.
// In the watch window it remains at 0, yet I can see the memory pointed to
by percent_done_pointer changing.
// I guess the only conclusion I can reach is that percent_done_pointer
points to some memory that is a copy of percent_done.
// But why?