3

I got a dll which is running as an out-of-process plugin in another application.

When the main application calls into my plugin my code runs in a thread-pool thread (I think) inside my plugins's appdomain. It is a MTA thread.

My question is what is the correct way to show a messagebox/dialog in my plugin?

Most answers I have found only says the dialog should open in "the GUI thread" but I don't have a GUI-thread in my appdomain! Tried searching for a definition of GUI-thread but could not find anything. Some hints says it is the thread where Application.Run is executed.

What I have tried is to just create a STA-thread and open the messagebox/dialog there. It seems to work most of the time but occationally I get a strange 100% CPU usage inside the ShowDialog method.

Should I start a message loop with Application.Run in my own appdomain? Should it run just during the callback or is it expensive to create/teardown so I should create it at start and have it running all the time?

(I have access to the main application's window handle which I use as parent/owner)

adrianm
  • 14,468
  • 5
  • 55
  • 102
  • Doesn't the appliaction you're plugging into provide an API to show a dialog? – CodeCaster May 30 '15 at 08:43
  • @CodeCaster, unfortunatly not. – adrianm May 30 '15 at 10:35
  • I think `ShowDialog` is not your dll job. You can provide a message(function return value for example) which can be showed by calling application. Or create event which can be wired up by calling application – Fabio May 30 '15 at 14:04
  • @adrianm I agree with Fabio above... even if you managed to invoke a method in the calling app, you are creating dependencies... perhaps you could put a wrapper around the main app and thereby effectively provide an API which permits dialogue interaction. – Paul Zahra Nov 04 '15 at 10:42

1 Answers1

1

Try to use Win API NativeMethods:

/// Direct Task Dialog call. [DllImport("comctl32.dll", CharSet = CharSet.Unicode, EntryPoint = "TaskDialog")] public static extern int TaskDialog(IntPtr hWndParent, IntPtr hInstance, string pszWindowTitle, string pszMainInstruction, string pszContent, int dwCommonButtons, IntPtr pszIcon, out int pnButton);

You can find good using example here: https://code.google.com/p/cassini/

Paul Zahra
  • 9,522
  • 8
  • 54
  • 76
gogosweb
  • 101
  • 1
  • 3
  • Thank you for your answer but don't understand how it answers the question. Which thread should I open the task dialog from? Standard thread pool MTA thread, my own created STA thread? – adrianm Nov 10 '15 at 13:33
  • Use the STA model. See related answer: http://stackoverflow.com/questions/127188/could-you-explain-sta-and-mta – gogosweb Nov 25 '15 at 09:31
  • So you mean create a STA thread and open the Task Dialog there. That is what I do now but I open the normal message box instead. Why should Task Dialog work where MessageBox doesn't. (I have tried to call the native win32 message box with the same result) – adrianm Nov 25 '15 at 15:01