Solved

Message Loop in Console App?

Posted on 2004-10-12
10
1,868 Views
Last Modified: 2008-02-20
I am attempting to use the Microsoft RTC APIs from a managed C# application.  The documentation for the object states that it uses the message loop for function calls and event handling.  However, I would like to use this component from a console application.  So, my question is, can I add a message loop to a console application easily, or if that's not easy, is there an easy way to create a windows forms application that doesn't actually display a form?
0
Comment
Question by:derekl
  • 6
  • 4
10 Comments
 
LVL 14

Expert Comment

by:AvonWyss
ID: 12291441
Both is possible. Actually, you can have console applications that also have forms (no matter if they are visible or not). To start a new message loop on a given thread, call Application.Run() - see the help files for details.
0
 

Author Comment

by:derekl
ID: 12293091
So it's as easy as starting a thread and calling Application.Run, and the COM object will find that message loop and use it appropriately?  That would pretty much be the threads entire purpose I assume?  Also, do I have to call Application.Exit from the same thread, to exit the message loop?  I don't ever expect to get a message on that loop as I said it is simply need to have a message loop to satisfy the com object requirements.
0
 
LVL 14

Expert Comment

by:AvonWyss
ID: 12295171
Yes, that's basically it, but the COM object will not "find" the message loop. It must be run from the thread where you created the message loop, since Windows messages are per thread. And calling Application.Exit will tear down the message loop, so that it's a good idea to call this when you're done. Or just assign the thread's IsBackground proprrty to true and let the thread in the message loop, it will then be aborted when the application closes.
0
 

Author Comment

by:derekl
ID: 12298246
How can I run the COM object on the message loop thread if Application.Run never returns and I will not be receiving any windows messages?  I seem to recall a method that will allow me to schedule method executions for the applications Message Loop thread, InvokeLater or something.  Would this work?
0
 
LVL 14

Expert Comment

by:AvonWyss
ID: 12298458
Yes, you can create an invisible form (for instance, which is minimized and not shown in the taskbar), and then you can call it's Invoke() method to have things run in its thread.
0
Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

 

Author Comment

by:derekl
ID: 12299036
OK, only two more questions.  I assume the form must be created on the thread with the message loop.  Is it OK to do this before the loop is started?  Also, is using a form's invoke method the only means to guarantee code executes on a given thread?  It seems like this would be a useful feature in non windows forms applications.
0
 
LVL 14

Expert Comment

by:AvonWyss
ID: 12299193
Well, the handles are not allocated when you call the constructor, so you SHOULD be able to construct the form before calling the thread. But I'd rather do it in the thread to be on the safe side, where lies the problem of doing this?

The Form.Invoke() method will put a message in the message queue which is then dispached via message loop. The handler for this specific message then executes the given delegate, and since it was called fromt he message loop, in the thread where the loop is running. You can think as sort of injecting code into the message loop.

To do this with any Thread, you can use the Abort(object) method and handle this in a catch block of the thread, issuing an ResetAbort() inside that block to make the thread continue to run. But since this raises an exception in the thread, it's only suitable for very specifically for this built threads.
0
 

Author Comment

by:derekl
ID: 12300473
OK, I've upped the point value since you've helped me so much!

I guess I don't see how to create the object after the message loop is started.  If I call Application.Run() (no argument version) this function never returns.  You are telling me I can execute arbitrary code on this thread by using Form.Invoke(), but how do I execute the code to create the form on the thread?  Since the form does not exist yet, I obviously can't use Form.Invoke for this purpose.  It sounds like I would need to pump a message to the MessageLoop thread to get this done.  Is this correct?  If so, how do I pump a message to this specific MessageLoop.  Or, since I will have a form, maybe I can use a custom ApplicationContext object to handle this?  I believe it's constructor gets executed on the MessageLoop thread, correct?  If I attempted to use Application.Run(Form form) I believe that version shows the form which is not acceptable.  So, is Application.Run(ApplicationContext ac) the way to go?
0
 
LVL 14

Expert Comment

by:AvonWyss
ID: 12301198
Well, like this:

public class ApplicationForm(): Form {
...your form here...
}

public ApplicationForm applicationForm;

private void ThreadCode() {
     applicationForm=new ApplicationForm();
     Application.Run(applicationForm);
}

...your code here...
     new Thread(new ThreadStart(ThreadCode)).Start();
...wait some so the other thread starts and creates the form...
     applicationForm.Invoke(...);

That should to the trick!
0
 
LVL 14

Accepted Solution

by:
AvonWyss earned 200 total points
ID: 12301226
Or you can also use the normal empty Run() and the Idle event, but if you want to run a form anyways, it's easiest like the example I gave in the previous post.
0

Featured Post

Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
Contant UI messages in .NET 1 41
Using MS Code on my Mac 6 47
Object Oriented Best Practice 5 35
install app on other machine 13 18
Introduction This article series is supposed to shed some light on the use of IDisposable and objects that inherit from it. In essence, a more apt title for this article would be: using (IDisposable) {}. I’m just not sure how many people would ge…
This article introduced a TextBox that supports transparent background.   Introduction TextBox is the most widely used control component in GUI design. Most GUI controls do not support transparent background and more or less do not have the…
Here's a very brief overview of the methods PRTG Network Monitor (https://www.paessler.com/prtg) offers for monitoring bandwidth, to help you decide which methods you´d like to investigate in more detail.  The methods are covered in more detail in o…
You have products, that come in variants and want to set different prices for them? Watch this micro tutorial that describes how to configure prices for Magento super attributes. Assigning simple products to configurable: We assigned simple products…

746 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now