DLL with message loop

Z

zhouzuo

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!
 
G

gshzheng

I think maybe you neednot a message loop in your dll(managed),
Only class definition (forms included) is present in you dll ,.

And Application.Run() or app.ShowDialog() should be done in the consumer
dll(exe project).

mostly likes:

//firstly,you should add refrence of YourDll.dll to the exe-project.

//in exe project
Application.Run(new YourDll.YourForm());

If the description above is not what your want, pls post your consumer code
which invoke dll.

gshzheng
20070328
 
Z

zhouzuo

Thank you for helping!
I change my consumer code as you suggested. like next


using System;
using System.Collections.Generic;
using System.Windows.Forms;

namespace myApp
{
static class Program
{
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new myApp.MainApplication());
}
}

The program started but DLL is not working correctly. Mouse event can
not be handled.

MainApplication was define as

public class MainApplication : System.Windows.Forms.Form, IGlobe

What wrong with it? I am new programmer in C#, any suggestion is very
wellcome!
 
G

gshzheng

The program started but DLL is not working correctly. Mouse event can
not be handled.

Are you sure "mouse event can not be handled"?
You just add a button on the form, try test the click event.


The form class is nothing but a class can be visible.
Also the form class in other dll is nothing but needing add reference.

Maybe firstly,for the sake of find the trouble,you should drag the form
class to exe-project,not using a separate dll.
where the form class is separate or not, nothing should be different.





Thank you for helping!
I change my consumer code as you suggested. like next


using System;
using System.Collections.Generic;
using System.Windows.Forms;

namespace myApp
{
static class Program
{
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new myApp.MainApplication());
}
}

The program started but DLL is not working correctly. Mouse event can
not be handled.

MainApplication was define as

public class MainApplication : System.Windows.Forms.Form, IGlobe

What wrong with it? I am new programmer in C#, any suggestion is very
wellcome!
 
Z

zhouzuo

Thank you for answer!
Are you sure "mouse event can not be handled"?

Next is the error that I got

---------------------------------------------------------------------------------------------------------------------------------------------------------
See the end of this message for details on invoking
just-in-time (JIT) debugging instead of this dialog box.

************** Exception Text **************
System.NullReferenceException: Object reference not set to an instance
of an object.
at myApp.myWindow.OnMouseMove(MouseEventArgs e) in E:\myApp
\myWindow.cs:line 1610
at System.Windows.Forms.Control.WmMouseMove(Message& m)
at System.Windows.Forms.Control.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.Callback(IntPtr hWnd, Int32
msg, IntPtr wparam, IntPtr lparam)
 

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