Can I create a .Net message loop without a Form?

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

When I have a long process to run I can create a new thread to run it and
create a modal dialog on my GUI thread. The modal dialog prevents any
messages (like button clicks) from working on my main Form. It also provides
a message loop, which allows paint messages and calls to the Control::Invoke
function to run. I can use these to invalidate an area and update it with
intermediate results of my process. When the process is done, I close the
modal dialog and everything works fine.

How can I do this, in .Net, without a modeless dialog?

I tried using Monitor to block the GUI thread. This prevents a message loop
from running. When I call Invoke from my process thread to invalidate an
area, it just hangs. It seems I will need a message loop. The only things I
can find to create a message loop are Application::Run and Form::ShowDialog.
The first throws an exception because I have already called it and the second
requires a modal dialog.
 
Hi mccoyn!
When I have a long process to run I can create a new thread to run it and
create a modal dialog on my GUI thread. The modal dialog prevents any
messages (like button clicks) from working on my main Form. It also provides
a message loop, which allows paint messages and calls to the Control::Invoke
function to run. I can use these to invalidate an area and update it with
intermediate results of my process. When the process is done, I close the
modal dialog and everything works fine.

Application::DoEvents()

VC7/7.1:
http://msdn.microsoft.com/library/e...sapplicationclassdoeventstopic.asp?frame=true

VC8:
http://msdn2.microsoft.com/system.windows.forms.application.doevents.aspx

--
Greetings
Jochen

My blog about Win32 and .NET
http://blog.kalmbachnet.de/
 
Jochen, Thanks for the reply.

When I call Application::DoEvents it lets every event through. This doesn't
work well for my particular process as it would mean I have to add a check in
most the button events of my application to ignore them if the process is
running. When I use Form::ShowDialog it doesn't allow the events to get to
the parent window, except for a few like paint and invoke events, which are
the ones I want to get through.
 
I was able to use the DoEvents function by disabling my application form.
This at least gets the behavior I want, but the form does an annoying flash
into disabled and right back to enabled when the process doesn't take a lot
of time. The other annoying thing is that DoEvents blocks when there are no
events. So, when my process is finished I have to send an event to get out
of DoEvents so my main thread can start running again and do things like
reenable the app.

I think I might try working with message filters to find another way.
 
Back
Top