Home » VB.NetRSS

Control accessed from a thread other than the thread it was created on.

I have a VB.NET form created in VS 2008. This form contains an ActiveXControl (MapPoint 2006 Europe ocx) located and reference here:

C:\Program Files\Microsoft MapPoint Europe\MappointControl.ocx

The Line that creates the Mappoint object in Form1.designer.vb is this:

FriendWithEvents myMap As AxMapPoint.AxMappointControl

This line is at the end of partial class generated by the form designer

PartialClass Form1

Inherits System.Windows.Forms.Form

The project compiles just fine. However, when I run this starting form I receive this error:

An error occurred creating the form. See Exception.InnerException for details.  The error is: Cross-thread operation not valid: Control 'AxMappointControl1' accessed from a thread other than the thread it was created on.

After some reasearch I tried to set CheckForIllegalCrossThreadCalls

to False:

Windows.Forms.Control.CheckForIllegalCrossThreadCalls = False

In the constructor of the Form1.

If I do that, I receive this error:

Exception of type 'System.Windows.Forms.AxHost+InvalidActiveXStateException' was thrown.

Can someone tell me what is the correct way of using ocx-es in VB.NET 2008?

I mention that the very same project was upgraded from VS 2005 on which worked just fine.

THX in advance,



11 Answers Found


Answer 1

You don't say how you are using threads in your application.

If you are not using threads, then this error is curious.

Otherwise, this is probably a common problem that people run into when using threads in Windows Forms applications.  .NET 2.0 is more aggressive in checking for this problem.

See http://msdn2.microsoft.com/en-us/library/ms171728(VS.80).aspx (How to: Make Thread-Safe Calls to Windows Forms Controls).


Answer 2

I do not use threads.

Just form  on which I throw this MapPoint ActiveX.



Answer 3

I retried disabling hosting process.

I tried retargeting for 2.0 and 3.0.

No success!



Answer 4


My application is also experiencing the same error when it tries to ShowDialog() on a Win VB Form that contains a MapPoint 2006 ocx. I have upgraded my Visual Studio from 2005 into 2008 and my application started having this problem. It used to work perfectly well when I compiled it in Visual Studio 2005 with .Net FramerWork 2.0 with latest SPK.

Here is my current environment under which the problem occurs:

O/S: Vista Enterprise 32-bit

Dev: Visual Studio 2008 RTM, .Net Framework 3.5

Component: Microsoft MapPoint Control v13.0 (2006) ocx, Build

And here is the message:

Cross-thread operation not valid: Control 'mapCanvas' accessed  from a thread  other than the thread it was created  on.
at System.Windows.Forms.Control.get_Handle()
   at System.Windows.Forms.Control.UpdateChildZOrder(Control ctl)
   at System.Windows.Forms.Control.WmCreate(Message& m)
   at System.Windows.Forms.Control.WndProc(Message& m)
   at System.Windows.Forms.ScrollableControl.WndProc(Message& m)
   at System.Windows.Forms.ToolStrip.WndProc(Message& m)
   at System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m)
   at System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m)
   at System.Windows.Forms.NativeWindow.DebuggableCallback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)
   at System.Windows.Forms.UnsafeNativeMethods.IntCreateWindowEx(Int32 dwExStyle, String lpszClassName, String lpszWindowName, Int32 style, Int32 x, Int32 y, Int32 width, Int32 height, HandleRef hWndParent, HandleRef hMenu, HandleRef hInst, Object pvParam)
   at System.Windows.Forms.UnsafeNativeMethods.CreateWindowEx(Int32 dwExStyle, String lpszClassName, String lpszWindowName, Int32 style, Int32 x, Int32 y, Int32 width, Int32 height, HandleRef hWndParent, HandleRef hMenu, HandleRef hInst, Object pvParam)
   at System.Windows.Forms.NativeWindow.CreateHandle(CreateParams cp)
   at System.Windows.Forms.Control.CreateHandle()
   at System.Windows.Forms.Control.CreateControl(Boolean fIgnoreVisible)
   at System.Windows.Forms.Control.CreateControl(Boolean fIgnoreVisible)
   at System.Windows.Forms.AxHost.EndInit()
   at StampXPert.BrowseLocations.InitializeComponent() in L:\Development\Projects\StampXPert\Source\Current\StampXPert\Browsers\BrowseLocations.Designer.vb:line 70
   at StampXPert.BrowseLocations..ctor() in L:\Development\Projects\StampXPert\Source\Current\StampXPert\Browsers\BrowseLocations.vb:line 5
   at StampXPert.Dialogs.BrowseLocations(String LocationDescription) in L:\Development\Projects\StampXPert\Source\Current\StampXPert\ServiceProviders\DefineDialogs.vb:line 473

I would appreciate if one (or more) MVPs could help out with a solution for this problem.

Answer 5

The problem is investigated in Redmond as we speek.

The problem is that they connot reproduce my bug.

Even I connot reproduce it.

What I mean by that? I reinstalled my alptop and the bug appeared.

I installed a virtual machine and the bug did not appeared.

I installed a freash new machine (a desktop PC) and the bug did not appeared.

May you can confirm if and how we can reproduce the bug,


In this way I guarantee a faster fix.




Answer 6

I just upgraded my single-threaded project from VS 2005 to VS 2008 and the embedded MapPoint 2006 NA OCX is experiencing the same issue.  The form  is sized on load in the following method.   This application had been working fine in production for over a year now.

privatevoid frmBestFit_Resize(object sender, EventArgs e)




axMapPoint.Size = new System.Drawing.Size(this.Width - axMapPoint.Left - 25, this.Height - axMapPoint.Top - 50);


catch (Exception ex)


MessageBox.Show(ex.Message, "An error has occured at " + ex.Source, MessageBoxButtons.OK, MessageBoxIcon.Error);

cls1.SendError(this.ToString(), ex, null);




System.InvalidOperationException was caught
  Message="Cross-thread operation not valid: Control 'axMapPoint' accessed  from a thread  other than the thread it was created  on."
       at System.Windows.Forms.Control.get_Handle()
       at System.Windows.Forms.Control.SetBoundsCore(Int32 x, Int32 y, Int32 width, Int32 height, BoundsSpecified specified)
       at System.Windows.Forms.AxHost.SetBoundsCore(Int32 x, Int32 y, Int32 width, Int32 height, BoundsSpecified specified)
       at System.Windows.Forms.Control.SetBounds(Int32 x, Int32 y, Int32 width, Int32 height, BoundsSpecified specified)
       at System.Windows.Forms.Control.set_Size(Size value)
       at Senske.SenskePro.Accounts.frmBestFit.frmBestFit_Resize(Object sender, EventArgs e) in C:\Users\aranieri.SENSKE\Documents\Visual Studio 2008\Projects\SenskePro2008\Accounts\frmBestFit.cs:line 4047


Answer 7

I had the same problem and It goes away when I check "Enable the Visual Studio hosting process" under your application properties/debug settings. I am not sure why. Maybe this will help.


Answer 8


Just to add to this.  I have created  a brand new application in VS 2008 using MapPoint 2006 and tried both VB and C#.  The same problem occurs.  I have VS 2005 on the same Vista computer and it works ok in 2005.

I have noticed that in dropping the control  on the brand new form  the only thing showing is a square in a circle shape in the top right hand corner of the form.  It is actually just outside the form hanging right on the corner.

In the properties box, both location and size are set to 0,0 and cannot be changed.  You can type in different numbers but as soon as you hit return they are back to 0,0.

Just more information and to see if anyone has a solution yet.

Thanks Paul


Answer 9


There is a discussion thread  titled "Visual Studio 2008 And Mappoint problem" on microsoft.public.mappoint, dating back to Nov 29, 2007 which discusses the issue.  Ewan's post (Feb 1, 2008 at 5:20 pm) discusses a step-by-step resolution, which worked for me.

I hope I don't get in trouble for copying and pasting Ewan's post here, I apologize in advance if I'm doing something wrong.


Andre Ranieri

Also check out this blog entry for future reference:



OK here is the issue resolution

First the problem

The problem appears to be caused by DEP (Data Execution Prevention) getting
triggered. DEP is short for Data Execution Prevention. It is a technology
that exists in Microsoft operating systems which prevents execution of code
from memory pages which are not marked as executable. In Windows Vista, the
set of processes and applications to which DEP is applied is configurable by
administrators, but there is also a role for application developers.

In the header of a PE file there is a flag called
IMAGE_DLLCHARACTERISTICS_NX_COMPAT. This flag affects whether or not the OS
enables DEP for a process. Setting this flag tells the OS that the image is
compatible with DEP. For executable images, if this flag is set, the process
is run with DEP enabled unless the machine is configured with the DEP policy
set to AlwaysOff.

The MapPointControl seems do something that is causing the DEP to trigger
and thus affecting both the Visual Studio IDE which is hosting the control  at
design time and the actual executable which tries to host it at runtime. To
resolve the problem, we need to set the header of the executable with this

The solution


Place the MapPoint object at Form_Load - setting its location and size


Public Class Form1

   Dim apppath As String = Application.StartupPath
   Friend WithEvents MPC As AxMapPoint.AxMappointControl

   Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load

       Me.MPC = New AxMapPoint.AxMappointControl

       Me.MPC.Enabled = True
       Me.MPC.Location = New System.Drawing.Point(10, 10)
       Me.MPC.Name = "MPC"
       Me.MPC.Size = New System.Drawing.Size(100, 100)
       Me.MPC.TabIndex = 0

       MPC.OpenMap(apppath & "\maps\default.ptm")

   End Sub
End Class


Place the following in properties - compile - build events - POST build
command line

(changing the path to the batch file)

call d:\vsvars32.bat "$(TargetPath)"

(changing the path to the batch file)


Create a batch file with the following

@SET VSINSTALLDIR=C:\Program Files\Microsoft Visual Studio 8
@SET VCINSTALLDIR=C:\Program Files\Microsoft Visual Studio 8\VC
@SET FrameworkDir=C:\Windows\Microsoft.NET\Framework
@SET FrameworkVersion=v2.0.50727
@SET FrameworkSDKDir=C:\Program Files\Microsoft Visual Studio 8\SDK\v2.0
@if "%VSINSTALLDIR%"=="" goto error_no_VSINSTALLDIR
@if "%VCINSTALLDIR%"=="" goto error_no_VCINSTALLDIR

@echo Setting environment for using Microsoft Visual Studio 2005 x86 tools.

@rem Root of Visual Studio IDE installed files.
@set DevEnvDir=C:\Program Files\Microsoft Visual Studio 8\Common7\IDE

@set PATH=C:\Program Files\Microsoft Visual Studio 8\Common7\IDE;C:\Program
Files\Microsoft Visual Studio 8\VC\BIN;C:\Program Files\Microsoft Visual
Studio 8\Common7\Tools;C:\Program Files\Microsoft Visual Studio
8\Common7\Tools\bin;C:\Program Files\Microsoft Visual Studio
8\VC\PlatformSDK\bin;C:\Program Files\Microsoft Visual Studio
Files\Microsoft Visual Studio 8\VC\VCPackages;%PATH%
@set INCLUDE=C:\Program Files\Microsoft Visual Studio
8\VC\ATLMFC\INCLUDE;C:\Program Files\Microsoft Visual Studio
8\VC\INCLUDE;C:\Program Files\Microsoft Visual Studio
8\VC\PlatformSDK\include;C:\Program Files\Microsoft Visual Studio
@set LIB=C:\Program Files\Microsoft Visual Studio 8\VC\ATLMFC\LIB;C:\Program
Files\Microsoft Visual Studio 8\VC\LIB;C:\Program Files\Microsoft Visual
Studio 8\VC\PlatformSDK\lib;C:\Program Files\Microsoft Visual Studio
@set LIBPATH=C:\Windows\Microsoft.NET\Framework\v2.0.50727;C:\Program
Files\Microsoft Visual Studio 8\VC\ATLMFC\LIB

"C:\Program Files\Microsoft Visual Studio 8\VC\bin\editbin.exe" /NXCOMPAT:NO

@goto end

@echo ERROR: VSINSTALLDIR variable is not set.
@goto end

@echo ERROR: VCINSTALLDIR variable is not set.
@goto end



in properties - debug

UNCHECK - Enable the VS hosting process

This will set the flag in the *.exe file built by the project and it runs
fine - only issue is not being able to see the MapPoint control on the form  

I have spent hour on the phone with a VERY helpful MS support engineer and
he is chasing down the MapPoint control developers to find out what the fix
will be.

If anyone would like a sample project (if you run into problems doing the
above) - email me at h...@va3gy.ca with the message title

MapPoint Control  Issue

I will as time allows send a sample of a working application




Answer 10

Hi all,
as it has been mentioned on http://msdn.microsoft.com/en-us/library/ms171728.aspx "You are strongly advised to fix this problem when you see it, but you can disable it by setting the CheckForIllegalCrossThreadCalls property to false . This causes your control  to run like it would run under Visual Studio .NET 2003 and the .NET Framework 1.1.".

So, it is better to maintain the error instead of using CheckForIllegalCrossThreadCalls = false;

Here is an example below in order to solve the problem.

using System.Threading;
using System.Reflection;

private void MyForm_Load(object sender, EventArgs e)
     pictureBox1.Visible = false;

private thread  thread1 = null;
private Thread thread2 = null;

delegate void ControlValueDelegate(Control ctrl, string pName, object pValue);

private void SetControlValue(Control ctrl, string pName, object pValue)
    if (ctrl.InvokeRequired)
         ControlValueDelegate d = new ControlValueDelegate(SetControlValue);
         ctrl.Invoke(d, new object [] { ctrl, pName, pValue });
          Type t = ctrl.GetType();
          PropertyInfo[] props = t.GetProperties();
          foreach (PropertyInfo p in props)
              if(p.Name.ToUpper() == pName.ToUpper())
                  p.SetValue(ctrl, pValue, null);

void Work()
          SetControlValue(pictureBox1, "visible", false);

void Start()
      thread1 = new Thread(new ThreadStart(Work));

void Stop()
      if (thread1 != null)
            thread1 = null;

private void btnStart_Click(object sender, EventArgs e)
      btnStart.Enabled = false;
      btnStop.Enabled = true;

private void btnStop_Click(object sender, EventArgs e)
      btnStop.Enabled = false;
      btnStart.Enabled = true;

Kindest regards...


Answer 11

I know this is an older thread, however I have had the same issue.  I have am using NO threads in my application.  On start in the debugger, it throws the same error as described above in the form  initialization routines that I did NOT write, these are the routines that are in Form.Designer.vb which are auto created  by VS2008.  Although I must note that I am running Win7 64bit for my dev machine.  I assume you are running some 64bit OS as well.  Maybe when the said virtual machines where setup, they where setup as 32 bit machines!? Which wouldn't really be a complete test of the issue.

I have also tried the various settings in VS2008 on my 64 bit os.  The target os, target framework, target CPU and many others fruitlessly looking for a solution.

Either way if I setup a virtual machine XP 32bit and install VS2008 on it, I have no issues with running the SAME code in the debugger.  This obviously is an issue that probably has been around for awhile, but explained away by some explaination that leaves the user frusterated as the above.

Thank god you can still get XP otherwise I would be left scratching my head.  However I have not tried to deploy the application to a 64bit operating system for testing.  It works fine on 32bit systems though.  My guess is it would fail as in my previous attempts to run it.

Maybe I will try VS2010 later to see if it has the same issues or not.



<< Previous      Next >>

Microsoft   |   Windows   |   Visual Studio   |   Follow us on Twitter