Search code examples
.netoverridingmessagedelphi-prism

Starting a second message loop on a single thread is not a valid operation


I am trying to override "wndProc" method for my winform. So, that I can implement my own wndproc method for specific messages. I am able to successfully do that. However, now I am running into another issue which I have never seen before.

After successfully compiling it, I ran the program and it popped up with the following error message, "Starting a second message loop on a single thread is not a valid operation. Use Form.ShowDialog instead."

My Overridden method winproc under protected access specifier:

method WndProc(var m:Message); override;

My definition of the method winproc as follows:

method MainForm.WndProc(var m: Message);
const WM_NCLBUTTONDOWN = 161;
const WM_SYSCOMMAND = 274;
const HTCAPTION = 2;
const SC_MOVE = 61456;
begin
    if ((m.Msg = WM_SYSCOMMAND) and (m.WParam.ToInt32 = SC_MOVE)) then
    begin
        exit;
    end;

    if ((m.Msg = WM_NCLBUTTONDOWN) and (m.WParam.ToInt32 = HTCAPTION)) then
    begin
        exit;
    end;
end;

The Error is raised is in the program.pas file:

[STAThread]
class method Program.Main(args: array of string);
begin
  Application.EnableVisualStyles();
  Application.SetCompatibleTextRenderingDefault(false);
  Application.ThreadException += OnThreadException;
  using lMainForm := new MainForm do
    Application.Run(lMainForm); < --------Raised here.
end;

How do I go about resolving it?


Solution

  • As hinted by HansPassant, I included the base class wndproc and it works.

    In Delphi-Prism, we do this.

    inherited WndProc(var m);

    same as base.wndproc(m);