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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T12:28:24+00:00 2026-06-11T12:28:24+00:00

It is my first post on StackOverflow forum so please to be lenient. I

  • 0

It is my first post on StackOverflow forum so please to be lenient. I have a problem with function which works called synchronously, but doesnot works called asynchronously.
Below You will find function called synchronously:

private void issueInvoices(List<int> lista)
    {
foreach (int knh_id in lista)
          {
                    Invoice fs = new Invoice();
                    fs.FKS_AKCYZA = false;
                    fs.FKS_CZY_KLON = false;
                    fs.FKS_DATE = Convert.ToDateTime(MTBDataZapisuDoFK.Text);
                    fs.NUMBER = knh_id);
         }
    }

As You can see i passed list to function named issueInvoices list of invoice numbers and in loop i create some invoices.
This function works properly but if i try to call it asynchronously (to display progress bar) my function can not assign to fs.FKS_DATE object dateTime. It looks like static function “Convert.ToDateTime” doesnot work properly. But please take a look on below code where function issueInvoices is called asynchronously…

public delegate void BinaryDelegate(List<int> knh_id);
BinaryDelegate b = new BinaryDelegate(issueInvoices);
IAsyncResult theAsRes = b.BeginInvoke(lista, new AsyncCallback(AddComplete), "Thx U!");
FrmProgressBar fpb=new FrmProgressBar(“Please wait…”);
 fpb.Show();

 /* below i check how many operation i have to do, if all operations are done, then I close fpb window, program is updating progres bar and in thread make operation issueInvoices*/
                        while (ilosc_zrobionych != liczbaKontrahentow)
                       {
                            fpb.PBStan.Value = (int)((100 * ilosc_zrobionych) / liczbaKontrahentow);
                       }
                        fpb.Close();

I put some breakpoints and it looks like program stoping in line, it can conver to datetime, but when i do this synchronously, it works without any errors.
fs.FKS_DATE = Convert.ToDateTime(MTBDataZapisuDoFK.Text);
What could couse this problem and how to resolve it?
Many thanks in advance for reply.

BELOW IS WHOLE CLASS CALLED ASYNCHRONOUSLY:

using System;
using System.Collections.Generic;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Npgsql;
using Castle.ActiveRecord;
using WFR.Model;
using System.Threading;

namespace Faktury_i_Rachunki_2.Forms
{

    public partial class FrmEmisjaFakturPotwierdzonych : FrmBaseForm
    {

        private ArrayList listaSposobowZaplaty;
        public List<int> lista;
        private int liczbaWygenerowach;
        private int liczbaKontrahentow;
        private int ilosc_zrobionych;
        private FrmProgressBar fpb;

        public delegate void BinaryDelegate(List<int> knh_id);


        public FrmEmisjaFakturPotwierdzonych()
        {
            InitializeComponent();
    fpb = new FrmProgressBar("Please wait....");
        }

        private void BtOK_Click(object sender, EventArgs e)
        {                
                BinaryDelegate b = new BinaryDelegate(WyemitujFakture);

                    lista.Add(12);
                    lista.Add(13);
                    lista.Add(17);
                    lista.Add(1);

                liczbaKontrahentow = lista.Count;
                if (TBRejestr.Text.Trim() != "")
                {

                    if (liczbaKontrahentow > 0)
                    {
                        liczbaWygenerowach = 0;
                        ilosc_zrobionych = 0;
                        WyemitujFakture(lista);
              IAsyncResult theAsRes = b.BeginInvoke(lista, new AsyncCallback(AddComplete), "THX");

                        fpb.Show();
                        while (ilosc_zrobionych != liczbaKontrahentow)
                        {
                            fpb.PBStan.Value = (int)((100 * ilosc_zrobionych) / liczbaKontrahentow);
                        }
                        fpb.Close();
                    }

                    try
                    {
                        MessageBox.Show("Wygenerowano " + liczbaWygenerowach.ToString() + " faktur");
                    }
                    catch
                    {
}

        }
}
        private void WyemitujFakture(List<int> lista)
        {
            foreach (int knh_id in lista)
            {
                try
                {
                                    if (luk.Count > 0)
                    {
                        FakturySprzedazy fs = new FakturySprzedazy();
                        fs.FKS_AKCYZA = false;
                        fs.FKS_CZY_KLON = false;
                        fs.FKS_DATA_DOW_KS = Convert.ToDateTime(MTBDataZapisuDoFK.Text);
                        fs.FKS_DATA_FAKTURY = Convert.ToDateTime(MTBDataFaktury.Text);
                        fs.FKS_DATA_SPRZEDAZY = Convert.ToDateTime(MTBDataSprzedazy.Text);
                        liczbaWygenerowach++;
                    }

                }
                catch (Exception ex)
                {
                    MessageBox.Show("Nie można wyemitować faktury dla kontrahenta o id = " + knh_id.ToString() + " " + ex.Message);
                }
                ilosc_zrobionych++;
            }
        }
  • 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-11T12:28:25+00:00Added an answer on June 11, 2026 at 12:28 pm

    The problem is in getting the value of MTBDataZapisuDoFK.Text (which I assume to be a textbox). Getting or setting the text of a textbox means sending messages to its window. But you keep the UI-thread busy in the while loop and therefore it can not process any messages.

    Put a call to Application.DoEvents() into the while loop to allow messages to be processed:

    fpb.Show();
    while (ilosc_zrobionych != liczbaKontrahentow)
    {
        Application.DoEvents();
        fpb.PBStan.Value = (int)((100 * ilosc_zrobionych) / liczbaKontrahentow);
    }
    fpb.Close();
    

    I assume that the only reason for calling the method asynchronously is to be able to update the UI during processing the WyemitujFakture-method. Using Application.DoEvents() you do not need asynchonous calls:

    fpb = new FrmProgressBar("Please wait....");
    fpb.Show();
    Application.DoEvents();
    WyemitujFakture(lista);
    fpb.Close();
    

    You should call Application.DoEvents() after you call fpb.Show() to allow the form to be displayed properly. Also you should instantiate the form in the method itself instead of the constructor, because you can not use the same instance again after calling fpb.Close() (it will be disposed).

    Then you can update the progress bar in the WyemitujFakture-method:

    private void WyemitujFakture(List<int> lista)
    {
        foreach (int knh_id in lista)
        {
            try
            {
                if (luk.Count > 0)
                {
                    FakturySprzedazy fs = new FakturySprzedazy();
                    fs.FKS_AKCYZA = false;
                    fs.FKS_CZY_KLON = false;
                    fs.FKS_DATA_DOW_KS = Convert.ToDateTime(MTBDataZapisuDoFK.Text);
                    fs.FKS_DATA_FAKTURY = Convert.ToDateTime(MTBDataFaktury.Text);
                    fs.FKS_DATA_SPRZEDAZY = Convert.ToDateTime(MTBDataSprzedazy.Text);
                    liczbaWygenerowach++;
                }
    
            }
            catch (Exception ex)
            {
                MessageBox.Show("Nie mozna wyemitowac faktury dla kontrahenta o id = " + knh_id.ToString() + " " + ex.Message);
            }
            ilosc_zrobionych++;
    
            fpb.PBStan.Value = (int)((100 * ilosc_zrobionych) / liczbaKontrahentow);
            Application.DoEvents();
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

This is my first post on StackOverflow, so please be gentle... I have some
This is my first post on stackoverflow, so please excuse me if my question
First, I must say that I have read several post about this at StackOverflow
This is my first time to post my query on stackoverflow. I have created
It's the first i post to stackoverflow although i always check the forum. I
First post on stackoverflow, apologies beforehand for anything noob-like. I have been receiving an
This is my first post on stackoverflow.com so please be kind (rewind) ;) I
This is my first post in stackoverflow. I want to improve its google pagerank
this is my first post here on stackoverflow and am very impressed by the
first post here as I'm stuck with my wonderful C++ function. The error 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.