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

  • Home
  • SEARCH
  • 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 6744113
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T12:02:41+00:00 2026-05-26T12:02:41+00:00

Here is the simple example: public event EventHandler CookinDone = delegate{}; public void CoockinRequest(){

  • 0

Here is the simple example:

public event EventHandler CookinDone = delegate{};

public void CoockinRequest(){
    var indicator = new ActivityIndicator();
    ActivityIndicator.Show("Oooo coockin' something cool");

    var bw = new BackgroundWorker();    
    bw.DoWork += (sender, e) => CockinService.Cook();
    bw.RunWorkerCompleted += (sender, e) => {
       indicator.Hide();
       CookinDone.Invoke(this,null);
    };

    bw.RunWorkerAsync();
}

Now, everytime I use that method I have to intercept CookinDone event and move on.

var cook = new Cook();
cook.CookinDone += (sender, e) => MessageBox.Show("Yay, smells good");
cook.CoockinRequest();

But how can I simplify that by making return type of the method as Boolean and return result upon the Cookin completion?

if (CoockinRequest()) MessageBox.Show('Yay, smells even better');

if I put there something like while (bw.IsBusy) it will screw my ActivityIndicator, freeze the main thread and I feel it would be the lousiest thing to do. There are also some Monitor.Wait stuff and some other stuff like TaskFactory, but all that stuff seems to be too complicated to use in simple scenarios.

It might be also different in different environments, like some approach is good for WPF apps, some for something else and whatnot, but there should be a general pattern isn’t that right?

How do you do that guys?

  • 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-26T12:02:42+00:00Added an answer on May 26, 2026 at 12:02 pm

    There isn’t a direct way to do this in .NET 4. This is actually very much in line with the new async/await functionality coming in the next release of C#.

    The Task Parallel Library can be used in .NET 4 to accomplish this today. You would do this by changing your code to return a Task<bool>, so the caller could wait on it (if required), or subscribe a continuation on the task which would run when this was complete.

    To do this, you’d rewrite the above code like so:

    public Task<bool> CoockinRequestAsync()
    {
        var indicator = new ActivityIndicator();
        ActivityIndicator.Show("Oooo coockin' something cool");
    
        // This assumes Cook() returns bool...
        var task = Task.Factory.StartNew(CockinService.Cook);
    
        // Handle your removal of the indicator here....
        task.ContinueWith( (t) => 
           {
               indicator.Hide();
           }, TaskScheduler.FromCurrentSynchronizationContext());
    
        // Return the task so the caller can schedule their own completions
        return task;
    }
    

    Then, when you go to use this, you’d write something like:

    private void SomeMethod()
    {
        var request = this.CoockinRequestAsync();
    
        request.ContinueWith( t =>
        {
            // This will run when the request completes... 
            bool result = t.Result;
    
            // Use result as needed here, ie: update your UI
    
        }, TaskScheduler.FromCurrentSynchronizationContext());
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

This problem is not readily reproducible in a simple example here but was wondering
I can't get past this issue I am having. Here's a simple example: class
Here's a very simple Prototype example. All it does is, on window load, an
I have a Control I want to create. Here's a simple example of what
I wanted to make a simple Countdown-Application with C# to show as an example.
I made a simple example to better understan the MVVM pattern. Here is a
I'm probably missing something simple here, but I can't find the answer elsewhere. I
I am probably overlooking something really simple here but I am trying to redirect
Please excuse me if I'm simple here, I want to create a simple widget
No doubt I'm missing something really simple here but I just can't see the

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.