V
ValiMaties
Guest
Hi.
I have a method in my form which returns the count of some specific elements from an xml.
private int GetNumberOfMissingElements(string filePath)
{
int i = 0;
XDocument xml = XDocument.Load(filePath);
foreach (XElement xe in xml.Descendants("someElement"))
{
if (xe.Descendants("someSpecificChild").Count() == 0)
{
i++;
}
}
return i;
}
This method I want to use it in a click event of a button, in async way on multiple files. As long as I return an integer value from this method I want to increment a property on my form, which will be displayed in a text box on my form.
private async void CountTotalMissingElements(DataTable files)
{
foreach(DataRow r in files.Rows)
{
string filePath = r["FilePath"].ToString();
var result = await Task.Run(() => GetNumberOfMissingElements(filePath));
TotalMissingElements += result;
}
}
And my TotalMissingElements property is:
private int totalMissingElements;
public int TotalMissingElements
{
get => totalMissingElements;
set
{
totalMissingElements = value;
if(txtTME.InvokeRequired)
{
txtTME.BeginInvoke((MethodInvoker)delegate ()
{
txtTME.Text = totalMissingElements.ToString();
}
}
else
{
txtTME.Text = totalMissingElements.ToString();
}
}
}
The problem is the files are always the same, but results are different. Sometimes shows me a result, sometimes other result.
What I'm doing wrong?
Regards,
Vali
Continue reading...
I have a method in my form which returns the count of some specific elements from an xml.
private int GetNumberOfMissingElements(string filePath)
{
int i = 0;
XDocument xml = XDocument.Load(filePath);
foreach (XElement xe in xml.Descendants("someElement"))
{
if (xe.Descendants("someSpecificChild").Count() == 0)
{
i++;
}
}
return i;
}
This method I want to use it in a click event of a button, in async way on multiple files. As long as I return an integer value from this method I want to increment a property on my form, which will be displayed in a text box on my form.
private async void CountTotalMissingElements(DataTable files)
{
foreach(DataRow r in files.Rows)
{
string filePath = r["FilePath"].ToString();
var result = await Task.Run(() => GetNumberOfMissingElements(filePath));
TotalMissingElements += result;
}
}
And my TotalMissingElements property is:
private int totalMissingElements;
public int TotalMissingElements
{
get => totalMissingElements;
set
{
totalMissingElements = value;
if(txtTME.InvokeRequired)
{
txtTME.BeginInvoke((MethodInvoker)delegate ()
{
txtTME.Text = totalMissingElements.ToString();
}
}
else
{
txtTME.Text = totalMissingElements.ToString();
}
}
}
The problem is the files are always the same, but results are different. Sometimes shows me a result, sometimes other result.
What I'm doing wrong?
Regards,
Vali
Continue reading...