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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T09:24:09+00:00 2026-06-09T09:24:09+00:00

I want to invoke a BackgroundWorker thread when a WebClient object calls back. The

  • 0

I want to invoke a BackgroundWorker thread when a WebClient object calls back.

The method which I target to run on this BackgroundWorker is not fixed so I need to programmatically target a specified method.

To achieve this:
One of the properties on the event args object passed to the WebClient details which method should be obtained (e.UserState.ToString()). This method is being obtained as expected.

What I then hope to do is add this obtained method as a delegate on the BackgroundWorker.DoWork event.

// this line gets the targeted delegate method from the method name
var method = GetType().GetMethod(e.UserState.ToString(), BindingFlags.NonPublic | BindingFlags.Instance);
if (method != null)
{
    // get the DoWork delegate on the BackgroundWorker object
    var eventDoWork = _bw.GetType().GetEvent("DoWork", BindingFlags.Public | BindingFlags.Instance);
    var tDelegate = eventDoWork.EventHandlerType;
    var d = Delegate.CreateDelegate(tDelegate, this, method);

    // add the targeted method as a handler for the DoWork event
    var addHandler = eventDoWork.GetAddMethod(false);
    Object[] addHandlerArgs = { d };
    addHandler.Invoke(this, addHandlerArgs);

    // now invoke the targeted method on the BackgroundWorker thread
    if (_bw.IsBusy != true)
    {
        _bw.RunWorkerAsync(e);
    }
}

For some reason a TargetException is thrown on the line

addHandler.Invoke(this, addHandlerArgs);

The exception message is

Object does not match target type.

The signature of the method which I’m building the code around is

private void GotQueueAsync(object sender, DoWorkEventArgs e)

This matches the signature for a BackgroundWorker.DoWork event handler.

Can anyone explain to me what I’ve done wrong or why I’m unable to programmatically add this handler method.

[In case it matters, this is a WP7 app.]

  • 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-09T09:24:11+00:00Added an answer on June 9, 2026 at 9:24 am

    You are passing this incorrectly:

    addHandler.Invoke(this, addHandlerArgs);
    

    The object with the event is not this (although this has the handler) – it should be _bw:

    addHandler.Invoke(_bw, addHandlerArgs);
    

    More simply, though:

    var d = (DoWorkEventHandler)Delegate.CreateDelegate(
        typeof(DoWorkEventHandler), this, method);
    _bw.DoWork += d;
    

    Or at least use EventInfo.AddEventHandler.

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

Sidebar

Related Questions

I want to invoke the main method which is static. I got the object
This is my javascript method in .aspx file. I want to invoke this method
I want to invoke a method on a COM object that is defined in
i am developing a client/server program in which i want to invoke a method
I want to invoke the method by the method name stored in the list.
I want to invoke a selector of a method that has the usual NSError**
I have a project, in which I want to invoke another Jar file in
I've an IBAction that calls up a panel. I want to invoke that action
I want to specifically invoke the base class method; what's the most concise way
I want to run an operation on a background thread. When it has completed

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.