InvalidOperationException: It is invalid to push a main message loop in this context.

C

Chris

Hi There,

I have a rather tricky problem: On one of the Windows XP Pro installations,
my .Net application has nothing more to say then:

System.InvalidOperationException: It is invalid to push a main message loop
in this context. This may cause your application to not respond to Windows
messages.

at System.Windows.Forms.ThreadContext.RunMessageLoopInner(Int32 reason,
ApplicationContext context)
at System.Windows.Forms.ThreadContext.RunMessageLoop(Int32 reason,
ApplicationContext context)
at System.Windows.Forms.Application.Run(ApplicationContext context)

I tried to narrow down the project and used the CLR hosting code described
in this article:

http://www.thecodeproject.com/dotnet/simpleclrhost.asp

This works fine, because the class in the Hi method only shows a message
box, but if this is extended to show a form in a windows message loop e.g.:

namespace Ranjeet.SimpleCLRHost
{
public class HelloHostDemo
{
public void Hi()
{
MessageBox.Show("YOO HOO from the Managed World!", "And now for this
message");
try
{
System.Windows.Forms.Form v_Form = new System.Windows.Forms.Form();
v_Form.Text = "test";
System.Windows.Forms.Application.Run(v_Form);
}
catch(Exception ex)
{
System.Windows.Forms.MessageBox.Show(ex.ToString());
}
}
}
}

I get the above exception. The .Net installation should be fine, since
non-CLR hosted apps, run fine. Now the problem is, that I have been digging
(internet & source code) the whole day and cannot find the slightest hint
why this exception is thrown.

Any idea - even a vague pointer into the right direction - would be greatly
appreciated.

Thanks,

Chris
 
D

David Lloyd

Chris:

The InvalidOperationException is thrown when a method call fails for reasons
other than invalid parameters. What appears to be happening, and I do not
know all the specifics of your setup, is that the form that you are invoking
the Hi() method on already has invoked the Application.Run method on the
current thread. Therefore, my understanding is that when you try to invoke
it again to show v_Form , on the same thread, the main message loop is
already running and it cannot start another one on the same thread. Thus
the exception.

One alternative, and again this I don't know your requirements, is to invoke
the Application.Run method for the v_Form on a new thread. For example:

public void Hi()

{

MessageBox.Show("YOO HOO from the Managed World!", "And now for this
message");

try

{

Thread newThread = new Thread(new ThreadStart(this.RunForm));

newThread.Start();


}

catch(Exception ex)

{

System.Windows.Forms.MessageBox.Show(ex.ToString());

}

}

private void RunForm()

{

try

{

System.Windows.Forms.Form v_Form = new System.Windows.Forms.Form();

v_Form.Text = "test";

System.Windows.Forms.Application.Run(v_Form);

}

catch(Exception ex)

{

System.Windows.Forms.MessageBox.Show(ex.ToString());

}

}


--
David Lloyd
MCSD .NET
http://LemingtonConsulting.com

This response is supplied "as is" without any representations or warranties.


Hi There,

I have a rather tricky problem: On one of the Windows XP Pro installations,
my .Net application has nothing more to say then:

System.InvalidOperationException: It is invalid to push a main message loop
in this context. This may cause your application to not respond to Windows
messages.

at System.Windows.Forms.ThreadContext.RunMessageLoopInner(Int32 reason,
ApplicationContext context)
at System.Windows.Forms.ThreadContext.RunMessageLoop(Int32 reason,
ApplicationContext context)
at System.Windows.Forms.Application.Run(ApplicationContext context)

I tried to narrow down the project and used the CLR hosting code described
in this article:

http://www.thecodeproject.com/dotnet/simpleclrhost.asp

This works fine, because the class in the Hi method only shows a message
box, but if this is extended to show a form in a windows message loop e.g.:

namespace Ranjeet.SimpleCLRHost
{
public class HelloHostDemo
{
public void Hi()
{
MessageBox.Show("YOO HOO from the Managed World!", "And now for this
message");
try
{
System.Windows.Forms.Form v_Form = new System.Windows.Forms.Form();
v_Form.Text = "test";
System.Windows.Forms.Application.Run(v_Form);
}
catch(Exception ex)
{
System.Windows.Forms.MessageBox.Show(ex.ToString());
}
}
}
}

I get the above exception. The .Net installation should be fine, since
non-CLR hosted apps, run fine. Now the problem is, that I have been digging
(internet & source code) the whole day and cannot find the slightest hint
why this exception is thrown.

Any idea - even a vague pointer into the right direction - would be greatly
appreciated.

Thanks,

Chris
 
C

Chris

Hi David,

thank you very much for your response.

Well, I start this Hi method and the v_Form inside via a C++ implemented CLR
host. That means this form is the main form of the app/process and the
message loop is for sure the first and only message loop in the whole
app/process.

The problem really is, that this very simple CLR hosted application as I
described works fine on a lot of PCs but we have three PCs where we get this
exception. There is no documentation on it and I cannot find the exception
being thrown in the rotor sourcecode nor in the system.windows.forms dll
viewed with the reflector. A "normal" application by the way works fine and
shows the form, the exact same thing but CLR hosted does work only most of
the times, but not on this three PCs.

Have I mentioned that all of this PCs have not installed Visual Studio?

So, still dont know how to solve this problem. I will try to publish some
code here and also try my test sample by starting a second thread to run the
message loop inside...

Thanks,

Chris
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top