This is my first StackOverflow forum post, so please be lenient. I have a problem with a function that works synchronously but does not work asynchronously. Below you will find the function 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 a list of functions called issueInvoices a list of account numbers, and in the loop I created several invoices. This function works correctly, but if I try to call it asynchronously (to display a progress bar), my function cannot assign fs.FKS_DATE to a dateTime object. It looks like the static function "Convert.ToDateTime" is not working properly. But please see below code where the 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(); while (ilosc_zrobionych != liczbaKontrahentow) { fpb.PBStan.Value = (int)((100 * ilosc_zrobionych) / liczbaKontrahentow); } fpb.Close();
I put some breakpoints, and it is like stopping a program in a string, it can convert to datetime, but when I do it synchronously, it works without errors. fs.FKS_DATE = Convert.ToDateTime (MTBDataZapisuDoFK.Text); What can solve this problem and how to solve it? Thanks so much in advance for your reply.
BELOW THE TARGET CLASS IS CALLED FOR FREE:
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++; } }
source share