Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 224013
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T19:16:36+00:00 2026-05-11T19:16:36+00:00

I have a question about events in .NET (C#). I have had to write

  • 0

I have a question about events in .NET (C#). I have had to write code for several situations in which I have a background task running and I want to notify a main thread, or controller class, that something has occurred, such as task completed, or finished copying a file, but I don’t want the background task to wait for the main thread’s delegate to process the event.

I want to do something like message passing: Send the message, but who cares what they do with it.

For example:

A class is written to process several processes in sequence, one after the other. Each process needs to run on a background thread. When work is completed, an event fires and tells the controller that it’s done (let’s say using OnCompleted() method)

The problem is that if the controller’s event handler is used to start the subsequent process, the previous processes’ OnComplete method stays on the call stack (never finishes executing) until the all of the processes have completed.

In that situation, how could the background task notify the controller class that the work is done without keeping the event raising method on the stack?

Example 2: A backup program.

A background thread runs to copy each file to the destination. The background needs to notify the UI the last file that was copied, but it doesn’t need to wait for the UI to update. Instead, it just wants to say, “BTW, here’s some info. Now, let me get back to work.” The event listener shouldn’t block the processing of the event raiser.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-11T19:16:36+00:00Added an answer on May 11, 2026 at 7:16 pm

    You could do an async invoke when raising the event (as mentioned), or just raise the event itself on a background thread:

    void OnUpdated(EventArgs e) {
       EventHandler h = this.Updated;
       if (h != null) h(e);
    }
    
    void DoStuff() {
       BigMethod();
       ThreadPool.QueueUserWorkItem(OnUpdated, EventArgs.Empty);
       BigMethod2();
    }
    

    If you raise asynchronously, multiple listeners would be processing your event at the same time. At the very least, that requires a thread-safe EventArg class. If you expect them to interact with your class as well, then you should document very carefully or make it thread-safe as well.

    Raising the event on a background thread carries the same caveats for your class methods, but you don’t have to worry about the EventArgs class itself.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have simple question about .net garbage collection. In the following code I create
I have a question about using streams in .NET to load files from disk.
Total beginner question about jQuery: I have a Form that contains several TextBoxes (input
I am slowly moving into Silverlight from asp.net and have a question about how
Note: My question is not about ASP.Net. I have a GridView bound with an
I have a question about the 'Event Dispatch Thread'. I have a Main class
I have question about NSView: Imagine a Custom View where the mouseDown, mouseDrag and
I have question about normalization. Suppose I have an applications dealing with songs. First
I have a question about best practices regarding how one should approach storing complex
I have a question about locking. This doesn't have to be only about record

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.