Skip to content

How to call a DLL with message loop

geo zhou

Asked Mar 27, 2007, 2:05 PM EDT2 answers
Share
LinkedInX
Hi
 I want to re-write a C# application(exe) to C# DLL. This application
starts by

           myApp.MainApplication app = new myApp.MainApplication();
           Application.Idle += new
EventHandler(app.myApp.OnApplicationIdle);
           Application.Run(app);

 But when I move all the code into a DLL and try to call the DLL from
a test program by
         myApp.MainApplication.Main(args);

 I got next error
  "Starting a second message loop on a single thread is not a valid
operation. Use Form.ShowDialog instead."

 I tried to change
    Application.Run(app);
 To
   app.ShowDialog();

 Error was gone but I can't get mouse event. Application was not
runing correctly.

I changed my test porgram
   Application.Run(new form1());
to
  Form myform = new Form1();
  myform.ShowDialog();

Same error message was returned.

 Anyone has any idea about it, or any related infomation is helpfull.
 Thanks!

2 Answers

Know the answer? Post it. Somebody with the same question will find it here.

  • Mike Gold
    0
    I believe that the dll is treated as a library (assembly) in .NET and will end up on the same thread as your executing application (windows form exe) when you create a class contained inside it.  The executable will contain the message pump for windows messages (not the dll).  Using ShowDialog does create a modal dialog which has it's own internal message pump, but I'm not sure you can trap mouse down events in a modal dialog the same way you would normally do in .NET.  I would recommend taking the app object out of the library.

    Anyone else have any suggestions?

    -Mike
    0
  • 0
    Thank you! Maybe the only way is create a new object and deal all of event in application not in DLL.
    0