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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T03:58:38+00:00 2026-06-12T03:58:38+00:00

Here is the code (in a simple form): delegate DataTable Functie(); Functie deleg; DataTable

  • 0

Here is the code (in a simple form):

delegate DataTable Functie();
Functie deleg;

DataTable find1()
{
    return new DataTable();
}
DataTable find2()
{
    return new DataTable();
}


private void btnFind1_Click(object sender, EventArgs e)
{
    deleg = this.find2;
    backgroundWorker1.RunWorkerAsync();
}


private void btnFind2_Click(object sender, EventArgs e)
{
    deleg = this.find1;
    backgroundWorker1.RunWorkerAsync();
}

private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)
{
    BackgroundWorker bk = sender as BackgroundWorker;
    e.Result = deleg;

    if (bk.CancellationPending)
    {
        e.Cancel = true;
    }
}


private void backgroundWorker1_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
{
    if (e.Cancelled)
    {
        MessageBox.Show("Operation was canceled");
    }
    else if (e.Error != null)
    {
        MessageBox.Show("An error occurred: " + e.Error.Message);
    }
    else
    {
        dataGridView1.DataSource = (DataTable)e.Result;
    }
}

So, basically I have 2 functions find1 and find2 and these are long-time-processing functions. I want to run these 2 functions on an BackgroundThread so I created a delegate. When i call one function, the delegate takes the address of a function or the other.
When I start the thread, I pass the delegate instead of directly typing the function, so I won’t create 2 background threads, each with its own events and stuff.
But I run into problems, when I run the program, the following error is thrown:

"System.Reflection.TargetInvocationException was unhandled
Exception has been thrown by the target of an invocation."

It has to do with the delegate passed to the thread. I can, of course, write everything simpler with conditions to see what functions has to be ran, but I got into this and I didn’t know how to solve it. I want to know what I did wrong.

  • 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-12T03:58:39+00:00Added an answer on June 12, 2026 at 3:58 am

    I think, in your DoWork function, you need to call the delegate like this:

    e.Result = deleg();
    

    The reason is that this will actually invoke the function, rather than just set the result equal to a reference to the function.

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

Sidebar

Related Questions

I have a simple situation here. lets face html code first => <form name=geoKey
Here is the super simple code i have: HtmlDocument htmlDoc = new HtmlDocument(); htmlDoc.OptionWriteEmptyNodes
I've got a simple upload form. Here's my code: <form id=Form1 method=post enctype=multipart/form-data runat=server
here my simple code to add filename and their associated icon to virtualtreeview PFileInfoRec
Here is some simple code: DIR* pd = opendir(xxxx); struct dirent *cur; while (cur
Here is a simple code that shows what I think is a bug when
Here's the code for a simple page I have that is not working: <head>
enter code here Hi All, I have a simple windows service application that connects
I've got a quite strange problem here. I'm calling some simple code via Ajax.Updater:
All my code is here,quite simple,and I don't konw where it goes wrong. Person

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.