Solved

Message Loop in Console App?

Posted on 2004-10-12
10
1,987 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
Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 

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
 

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

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Set value of dynamically created checkboxlist 2 44
Service Controller 1 28
C# Gridview 1 49
how to double quote a string for an inline sql statement. 8 76
This article is for Object-Oriented Programming (OOP) beginners. An Interface contains declarations of events, indexers, methods and/or properties. Any class which implements the Interface should provide the concrete implementation for each Inter…
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
This video shows how to use Hyena, from SystemTools Software, to bulk import 100 user accounts from an external text file. View in 1080p for best video quality.
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…

821 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