MessageLoop on a thread

T

Theresa Smallwood

Is there a way to add a Windows MessageLoop to an existing thread? I have a
..NET application that is called from from a COBOL app using COM, and when I
show a form, the System.Windows.Forms.Application.MessageLoop is set to
false. Is there some way to ensure that the form displayed has a
MessageLoop?
I have tried displaying the form several ways (described below), but all of
them seem to have some problem that makes it a less then ideal solution.

Showing the form this way:

System.Windows.Forms.Application.Run(_child);

Worked great, except that the user couldn't get back to COBOL application
that launched the .NET form - and we needed to be able to do that.

So I tried this:

ShowFormDelegate formshow = new ShowFormDelegate(ShowFormMessageLoop);
this.AppContext.CurrentWorkbench.Form.Invoke(formshow, new object[]
{_child});

Where ShowFormMessageLoop is:

private void ShowFormMessageLoop(Form frm)
{
frm.Show();
}

(Where this.AppContext.CurrentWorkbench.Form is a form that is not visible,
in fact it's never visible, it's just created in the background. )
But this does not have a MessageLoop, so Tabs, ESC, function keys, etc. are
not processed at all on the form.


This method came the closest to working correctly, except that some of the
3rd party controls (grids and toolbars) that were on the forms would not
process keystrokes correctly - the controls are apparently implementing a
message filter that is not getting processed.

_applicationcontext = new System.Windows.Forms.ApplicationContext(_child);
ThreadStart _threadstart = new ThreadStart(this.StartMessageLoop);
Thread _thread = new Thread(_threadstart);
_thread.IsBackground = true;
_thread.ApartmentState = ApartmentState.STA;
_thread.Start();

Where StartMessageLoop is:

private void StartMessageLoop()
{
System.Windows.Forms.Application.Run(_applicationcontext);
}

If anyone has any ideas, I would greatly appreciate it. Thanks!
Theresa
 
L

Lee Gillie

Theresa said:
Is there a way to add a Windows MessageLoop to an existing thread? I have a
.NET application that is called from from a COBOL app using COM, and when I
show a form, the System.Windows.Forms.Application.MessageLoop is set to
false. Is there some way to ensure that the form displayed has a
MessageLoop?
I have tried displaying the form several ways (described below), but all of
them seem to have some problem that makes it a less then ideal solution.

Showing the form this way:

System.Windows.Forms.Application.Run(_child);

Worked great, except that the user couldn't get back to COBOL application
that launched the .NET form - and we needed to be able to do that.

This should be the way to do it, but I don't understand what is meant by
"getting back to cobol". You mean when you call "Run" it does not return
control to the caller? This is a synchronous call and uses the current
thread for the pump, and returns to caller when all the forms have
closed, and the forms are done. If you need this to NOT be synchronous,
then start a new thread, and call application.run from there. This new
thread, is then, your GUI thread. If Cobol needs to make calls to affect
the GUI you will have to marshal the calls to the GUI thread. The
thread is either running the pump, or not, you have to decide who's the
boss.

HTH - Lee
 

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