Solved

Message Loop in Console App?

Posted on 2004-10-12
10
2,038 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Salesforce Made Easy to Use

On-screen guidance at the moment of need enables you & your employees to focus on the core, you can now boost your adoption rates swiftly and simply with one easy tool.

 

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

Revamp Your Training Process

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
ado.net AddWithValue when using IN condition 7 42
pressing download button in c# 7 45
How we can add Dten600.dll in GAC in c#. 18 50
Read top line from CSV file 14 52
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…
Performance in games development is paramount: every microsecond counts to be able to do everything in less than 33ms (aiming at 16ms). C# foreach statement is one of the worst performance killers, and here I explain why.
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an antispam), the admini…
Are you ready to implement Active Directory best practices without reading 300+ pages? You're in luck. In this webinar hosted by Skyport Systems, you gain insight into Microsoft's latest comprehensive guide, with tips on the best and easiest way…

740 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