Background Thread Raising Event in Main Thread

Posted on 2005-04-11
Last Modified: 2010-04-23
I'd like to be able to start a background thread that can raise events on the main thread. I know it's possible to effectively do this by calling Invoke() (or BeginInvoke()) on a form/control from within the code running on the background thread. But can this be done without a control object handling the switch from background thead to UI thread? For example, what if I need this behavior in a console application.

I've also read that 2005 will support a BackgroundWorker class to simplify the interaction with a background thread, but my understanding is that this too will utilize a control object. I'm somewhat new to, so perhaps it's possible to add a control to a console application anyway?
Question by:zacbell
    LVL 48

    Accepted Solution

    Event is raised in the context of caller thread. Raising event means that event source calls directly event handler of the client. This happens in the same thread where event source (caller) runs.
    Invoke or BeginInvoke allow to call functions of Windows forms in the thread where these forms are created. This is impossible in console application (unless it creates a window).
    BackgroundWorker is component which simplifies writing of worker thread, but code produced by it doesn't contain any new features.
    So, answer to your question is "No, it is impossible". Please describe your initial problem, maybe there is another solution for it.
    LVL 9

    Expert Comment

    Windows application or not, you can create a thread that can delegate an event. When the event is fired it will notify the calling program, in the case of a console application that might be pushing the limit a little.

    Author Comment

    Sorry it took so long to reply. I have actually been trying different approaches with my code to either (a) achieve this desired behavior as stated, or (b) accept AlexFM's comment that it's impossible and rewrite my code to achieve the desired behavior using a different solution.

    Basically, I was able to rewrite my code to achieve the desired functionality without using a background thread, and thus without worrying about the thread in which events are called, as suggested by AlexFM.

    By the way, does anyone happen to know the easiest way to determine if a program was called from an interactive session, or was called non-interactively (i.e. was called from scheduler running on a server, and thus not within a user's session)?

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Highfive + Dolby Voice = No More Audio Complaints!

    Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

    It’s quite interesting for me as I worked with Excel using for some time. Here are some topics which I know want to share with others whom this might help. First of all if you are working with Excel then you need to Download the Following …
    Parsing a CSV file is a task that we are confronted with regularly, and although there are a vast number of means to do this, as a newbie, the field can be confusing and the tools can seem complex. A simple solution to parsing a customized CSV fi…
    Hi everyone! This is Experts Exchange customer support.  This quick video will show you how to change your primary email address.  If you have any questions, then please Write a Comment below!
    Sending a Secure fax is easy with eFax Corporate ( First, Just open a new email message.  In the To field, type your recipient's fax number You can even send a secure international fax — just include t…

    758 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

    10 Experts available now in Live!

    Get 1:1 Help Now