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 8626099
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T07:56:36+00:00 2026-06-12T07:56:36+00:00

I’ve got to call a routine from a supplied DLL. This DLL takes 2

  • 0

I’ve got to call a routine from a supplied DLL. This DLL takes 2 to 10 minutes to run, depending on the speed of the PC it is being run on.

I have put the DLL call into a BackgroundWorker thread so that the interface will remain responsive.

private object Load(string feature) {
  object result = null;
  using (var w = new BackgroundWorker()) {
    w.WorkerReportsProgress = true;
    w.WorkerSupportsCancellation = true;
    w.DoWork += delegate(object sender, DoWorkEventArgs e) {
      e.Result = DAL.get(feature);
    };
    w.RunWorkerCompleted += delegate(object sender, RunWorkerCompletedEventArgs e) {
      progressBar1.Visible = false;
      if (e.Error != null) {
        MessageBox.Show(this, e.Error.Message, "Load Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
      } else {
        result = e.Result;
      }
    };
    w.RunWorkerAsync();
    if (w.IsBusy) {
      progressBar1.Style = ProgressBarStyle.Marquee;
      progressBar1.Visible = true;
    }
  }
  return result;
}

This works, but I can’t call this method inline with other calls that are waiting on its results because it will immediately return a null value.

So, I stuck in a ManualResetEvent instance to try and get the method to wait until it actually had a value before returning:

private object Load(string feature) {
  object result = null;
  using (var w = new BackgroundWorker()) {
    var mre = new ManualResetEvent(false);
    w.WorkerReportsProgress = true;
    w.WorkerSupportsCancellation = true;
    w.DoWork += delegate(object sender, DoWorkEventArgs e) {
      e.Result = DAL.get(feature);
    };
    w.RunWorkerCompleted += delegate(object sender, RunWorkerCompletedEventArgs e) {
      progressBar1.Visible = false;
      if (e.Error != null) {
        MessageBox.Show(this, e.Error.Message, "Model Load Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
      } else {
        result = e.Result;
      }
      mre.Set();
    };
    w.RunWorkerAsync();
    if (w.IsBusy) {
      progressBar1.Style = ProgressBarStyle.Marquee;
      progressBar1.Visible = true;
      progressBar1.Value = 0;
      const string statusRun = @"\|/-";
      const string statusMsg = "Loading Data...";
      int loops = 0;
      do {
        int index = loops++ % 4;
        tsStatus.Text = statusMsg + statusRun[index].ToString(); // '\', '|', '/', '-'
      } while (!mre.WaitOne(200));
    }
  }
  return result;
}

However, it appears that doing this causes all of my CPU time to be spent on the ManualResetEvent’s WaitOne method and the Set() trigger is never called.

Has anyone encountered this behavior before and found a good workaround for it?

I have created a workaround for it in the past, but it involved creating a second thread to run the WaitOne method so that the first thread can process the DoWork code.

  • 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-06-12T07:56:37+00:00Added an answer on June 12, 2026 at 7:56 am

    This works, but I can’t call this method inline with other calls that are waiting on its results because it will immediately return a null value.

    That is why the async and await keywords were invented. There is no easy solution for this that doesn’t block the thread.

    Since the DLL you are using apparently does not supply methods implemented using any of the Asynchronous Programming Patterns (like BeginOperation/EndOperation, or Task-Based Async Programming), you’re stuck with a separate worker thread.

    What you can do is:

    Start your BackgroundWorker or Thread as usual, then return immediately. Do not continue with the operation which would have required the return value of the lengthy DLL process. Once the BackgroundWorker or Thread is finished, have it report progress, and in the ProgressChanged event handler, you can retrieve the result of the lengthy DLL process and continue with the operation. Alternatively you can use the RunWorkerCompleted event (might actually be a better choice).

    In the meantime, you may have to disable all controls which could start the lengthy DLL process again, or which would otherwise issue invalid operations while the process is running. And as Henk Holterman wrote, do not dispose the BackgroundWorker like that.

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

Sidebar

Related Questions

I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
For some reason, after submitting a string like this Jack’s Spindle from a text
this is what i have right now Drawing an RSS feed into the php,
I have this code to decode numeric html entities to the UTF8 equivalent character.
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
I have this code: - (void)parser:(NSXMLParser *)parser foundCDATA:(NSData *)CDATABlock { NSString *someString = [[NSString
This could be a duplicate question, but I have no idea what search terms
I have a text area in my form which accepts all possible characters from
Does anyone know how can I replace this 2 symbol below from the string
I have a view passing on information from a database: def serve_article(request, id): served_article

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.