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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T01:36:40+00:00 2026-06-05T01:36:40+00:00

The Simplified 7 Steps : [MainForm] User Clicks btnAdd Button AddForm will be shown

  • 0

The Simplified 7 Steps :

  1. [MainForm] User Clicks btnAdd Button
  2. AddForm will be shown
  3. [AddForm] btnCreate is clicked
  4. within the btnCreate_Click we run AddProductProcess with an awaiter
    * We will close the AddForm as soon as the click
    *
    And show the MainForm
  5. Inside AddProductProcess we run AddProduct with an awaiter
  6. We run our AddProduct which will do the lengthy process for us and fill the Application-Level static collection : ProductCollection
  7. [MainForm] When the Process AddProductis done we will show the Added Product Item in our lstProducts ListBox.

5 Pieces of Code :

private void btnAddProduct_Click(object sender, EventArgs e)
{
    FormAddProduct fap = new FormAddProduct(SelCol);
    fap.ShowDialog();
}

private async void btnCreate_Click(object sender, EventArgs e)
{
    string stProduct = txtProductName.Text;

    await ProductCollection.AddProductProcess(stProduct);
    this.Close();
    MainForm.Show();
}           

public async Task AddProductProcess(string pName)
{
    await Task.Factory.StartNew(() => AddProduct(pName));
    // This would be our heavy process
}   

public void AddProduct(string pName)
{
    ProductItem p =  new ProductItem();
    p.Name = pName ;
    p.Position = Count;
    p.GetInfo(); // and some similar heavy methods are inside this
    //ProductCollection.Add(p);
}

public void Add(Product product)
{
    MainForm.lstProduct.Add(product.Name);
}

“MainForm.lstProduct.Add” cause a invalid cross-thread operation error

I need to add a Task Completion notification on it so can add the result the proper way to the ListBox
Could you help me implement it ?

I should pass this line of code to the code that will execute right after the task is finished.

ProductCollection.Add(p);

Any Ideas on this piece of code and the subject are appreciated,

  • 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-05T01:36:42+00:00Added an answer on June 5, 2026 at 1:36 am

    You are using the async/await pattern at some points but not where it matters…

    public async Task AddProductProcess(string pName)
    {
        await Task.Factory.StartNew(() =>
            AddProduct(pName));
    }   
    
    public void AddProduct(string pName)  // not async
    {
        ...
        ProductCollection.Add(p);
    }
    
    
    public void Add(Product product)  // not async
    {
        MainForm.lstProduct.Add(product.Name);
    }
    

    That last method just runs on a Task, so it is a ‘normal’ cross-threading error. You can resolve it in the normal way, using MainForm.Invoke(...) but then you might as well remove all the async and await keywords.

    To properly use async/await you would have to change it to something like:

    public async Task AddProductProcess(string pName)
    {
        await AddProduct(pName);
    }   
    
    public async void AddProduct(string pName)  
    {
        ProductItem p =  new ProductItem();
        p.Name = pName ;
        p.Position = Count;
        await p.GetInfo();    // assuming this is doing the heavy work, make async
        ProductCollection.Add(p);
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have three tables - Context, Session and User. And below are their simplified
Simplified Table structure: CREATE TABLE IF NOT EXISTS `hpa` ( `id` bigint(15) NOT NULL
Simplified example <td>caption</a> <a id=tt-1>text1</a> <a id=tt-2>text2</a> <td>topics</td> <a id=tt-3>text3</a> <a id=tt-4>text4</a> <a id=tt-5>text5</a>
A simplified version of problem I am experiencing: Here is my HTML form: <form
My (simplified) table consist of an Id int identity(1,1), File varchar(20), FileProcessed bit The
A simplified version of what i'm trying to do is as follows: var indication
I simplified my code for next example. So, please don't be wondered why I'm
Following simplified code snippet: #include <QtGui> int main(int argc, char **argv) { QApplication app(argc,
This is a simplified example to illustrate the question: class A {}; class B
I'm writing a simplified Pascal parser/interpreter and now I'm thinking about segmentation faults. I'm

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.