background worker not responding in our code - c#

i have following code and telerik grid my records are 1266880 background worker are hangout for one minutes i try many ways but cannot success anyone know my application does not hang
PopulateUI(List<DataRow> list)
{
this.Invoke(new MethodInvoker(delegate
{
int Count = list.Count;
grdFares.RowCount = Count;
// grdFares.BeginUpdate();
for (int i = 0; i < grdFares.RowCount; i++)
{
grdFares.Rows[i].Cells[COLS.Id].Value = list[i].ItemArray[0].ToStr();
grdFares.Rows[i].Cells[COLS.FROMLOCATION].Value = list[i].ItemArray[7].ToString();
grdFares.Rows[i].Cells[COLS.TOLOCATION].Value = list[i].ItemArray[8].ToString();
grdFares.Rows[i].Cells[COLS.FARE].Value = list[i].ItemArray[4].ToString();
grdFares.Rows[i].Cells[COLS.RowNo].Value = 0;
// grdFares.Rows[i].Cells[COLS.OrignTypeId].Value = list[i].OriginLocationTypeId;
// grdFares.Rows[i].Cells[COLS.DestinationTypeId].Value = list[i].DestinationLocationTypeId;
grdFares.Rows[i].Cells[COLS.VehicleType].Value = list[i].ItemArray[2].ToString();
grdFares.Rows[i].Cells[COLS.VehicleId].Value = list[i].ItemArray[11].ToString();
}
}));
}

Related

End a task that has a ui updated inside the task C#

I have the following code:
void Selection()
{
bool solutionFound = false;
int generation = 0;
int distance = int.MaxValue;
while(!solutionFound)
{
generation++;
for (int i = 0; i < population.Length; i++)
{
population[i].CalcFitness(target);
}
matingPool = new List<DNA>();
for (int i = 0; i < population.Length; i++)
{
int n = (int)(population[i].fitness * 100);
for (int j = 0; j < n; j++)
{
matingPool.Add(population[i]);
}
}
for (int i = 0; i < population.Length; i++)
{
int a = StaticRandom.Instance.Next(matingPool.Count);
int b = StaticRandom.Instance.Next(matingPool.Count);
DNA partnerA = matingPool[a];
DNA partnerB = matingPool[b];
if(partnerA.GetPhrase().Equals(partnerB.GetPhrase()))
{
while(partnerA.GetPhrase().Equals(partnerB.GetPhrase()))
{
a = StaticRandom.Instance.Next(matingPool.Count);
b = StaticRandom.Instance.Next(matingPool.Count);
partnerA = matingPool[a];
partnerB = matingPool[b];
}
}
DNA child = partnerA.Crossover(partnerB);
child.Mutate(mutationRate);
population[i] = child;
ThreadHelperClass.SetText(this, display_lbl, i + ": " + child.GetPhrase());
ThreadHelperClass.SetText(this, gen_lbl, "Generations: " + generation);
int compute = LevenshteinDistance.Compute(target, child.GetPhrase());
if(compute < distance)
{
distance = compute;
ThreadHelperClass.SetText(this, bestPhrase_lbl, "Best Phrase: " + child.GetPhrase());
}
if(child.GetPhrase().Equals(target))
{
solutionFound = true;
break;
}
}
}
}
and the following code
public static class ThreadHelperClass
{
delegate void SetTextCallback(Form f, Control ctrl, string t);
public static void SetText(Form form, Control control, string text)
{
//Invoke requires compares the thread ID of the calling thread
//to the thread ID of the creating thread
//If they are different InvokeRequired sets to true
if (control.InvokeRequired)
{
SetTextCallback d = new SetTextCallback(SetText);
form.Invoke(d, new Object[] { form, control, text });
}
else
control.Text = text;
}
}
I send Selection to another thread using:
Task.Factory.StartNew(() => Selection(), cToken.Token);
I've already tried to set the ThreadHelper class to use BeginInvoke instead of Invoke and it locks up my UI on the first iteration of the Selection method.
I've also tried to end the task using cToken.Cancel(). This still locks up my UI when the user clicks the stop_btn.
Is there a way I can stop this from happening?

Populate TextBoxes from a List

I am trying to populate TextBoxes from a list. I have been able to populate ComboBoxes with comboList:
var comboList = new System.Windows.Forms.ComboBox[4];
comboList[0] = cmbSite1Asset;
comboList[1] = cmbSite2Asset;
comboList[2] = cmbSite3Asset;
comboList[3] = cmbSite4Asset;
List<CRCS.CAsset> assets = _rcs.Assets;
foreach (CRCS.CAsset asset in assets)
{
string id = asset.ID;
for (int i = 0; i < 4; ++i)
{
comboList[i].Items.Add(id);
}
}
But when I try and apply the same principle to TextBoxes
var aosList = new System.Windows.Forms.TextBox[8];
aosList[0] = txtAsset1;
aosList[1] = txtAsset2;
aosList[2] = txtAsset3;
aosList[3] = txtAsset4;
aosList[4] = txtAsset5;
aosList[5] = txtAsset6;
aosList[6] = txtAsset7;
aosList[7] = txtAsset8;
foreach (CRCS.CAsset asset in assets)
{
string id = asset.ID;
for (int n = 0; n < 8; ++n)
{
aosList[n].Items.Add(id);
}
}
TextBox does not like Items.Add ( aosList[n]Items.Add(id); )
I am looking fore a reference or guidance resolving this issue. Thanks!
You should use ComboBox for your problem,instead of iterating on each element,You simply use below lines to populate combobox.
comboList.DataSource=assets;
comboList.DisplayMember="ID";
comboList.ValueMember="ID";
However,if you want your values in TextBox,you can use TextBox.AppendText Method, but it will not work like ComboBox as it will contain texts+texts+texts, will not have indexes like ComboBox.
private void AppendTextBoxLine(string myStr)
{
if (textBox1.Text.Length > 0)
{
textBox1.AppendText(Environment.NewLine);
}
textBox1.AppendText(myStr);
}
private void TestMethod()
{
for (int i = 0; i < 2; i++)
{
AppendTextBoxLine("Some text");
}
}
A Combobox is a collection of items, and so has an Items property from which you can add/remove to change it's contents. A Textbox is just a control that displays some text value, so it has a Text property which you can set/get, and which denotes the string that is displayed.
System.Windows.Forms.TextBox[] aosList = new System.Windows.Forms.TextBox[8];
aosList[0] = txtAsset1;
aosList[1] = txtAsset2;
aosList[2] = txtAsset3;
aosList[3] = txtAsset4;
aosList[4] = txtAsset5;
aosList[5] = txtAsset6;
aosList[6] = txtAsset7;
aosList[7] = txtAsset8;
for (int n = 0; n < 8; ++n)
{
aosList[n].Text = assets[n].ID; // make sure you have 8 assets also!
}
int i = 1;
foreach (var asset in assets)
{
this.Controls["txtAsset" + i].Text = asset.ID;
i++;
}

Xamarin.iOS iOS-Charts: Bar Chart not showing bars

I'm using iOSCharts in project to draw bar chart. There's no official documentations available for iOSCharts and on the github page it says all the api's are same as MPAndroidChart plot. I followed everything I used to do in the android, but bars of the bar chart are not showing. The entire chart takes up the color of the bars
This is how it's showing..
BarChartView barChart = new BarChartView();
ChartXAxis xAxis = barChart.XAxis;
xAxis.LabelPosition = XAxisLabelPosition.Bottom;
xAxis.DrawGridLinesEnabled = false;
xAxis.AxisLineColor = UIColor.Clear.FromHexString("#DEDEDE");
barChart.RightAxis.Enabled = false;
barChart.LeftAxis.DrawAxisLineEnabled = false;
barChart.LeftAxis.GridColor = UIColor.Clear.FromHexString("#DEDEDE");
barChart.LeftAxis.StartAtZeroEnabled = false;
barChart.Legend.Enabled = false;
barChart.LeftAxis.LabelCount = 5;
barChart.BackgroundColor = UIColor.Clear.FromHexString("#FFFFFF");
barChart.GridBackgroundColor = UIColor.Clear.FromHexString("FFFFFF");
barChart.SetDescriptionText("");
barChart.MoveViewToX(7);
VitalGraph vGraph = ..............
barChart.SetData(GetBarData(vGraph.vitalId, vGraph));
barChart.Frame = measurementsChartContainer.Frame;
measurementsChartContainer.AddSubview(barChart);
private BarChartData GetBarData(int vitalId, VitalGraph vGraph)
{
List<float> valueList = vGraph.valueList.Select(x => float.Parse(x)).ToList();
UIColor[] colorList = new UIColor[vGraph.colorList.Count];
for (int i = 0; i < vGraph.colorList.Count; i++)
colorList[i] = UIColor.Clear.FromHexString(vGraph.colorList[i]);
BarChartDataEntry[] entries = new BarChartDataEntry[7];
for (int i = 0; i < valueList.Count; i++)
{
entries[i] = new BarChartDataEntry(valueList[i], i);
}
//barChart.MoveViewToX(valueList.Count)
BarChartDataSet barDataSet = new BarChartDataSet(entries, "Vital Reading");
barDataSet.BarSpace = 40f;
NSString[] dayList = new NSString[vGraph.dayList.Count];
for (int i = 0; i < vGraph.dayList.Count; i++)
dayList[i] = new NSString(vGraph.dayList[i]);
List<IBarChartDataSet> dataList = new List<IBarChartDataSet>();
dataList.Add(barDataSet);
BarChartData barData = new BarChartData(dayList, dataList.ToArray());
//barDataSet.SetColors(colorList);
return barData;
}
I had no trouble using MPAndroidChart in Xamarin.Drioid. Am I something wrong here?
BarChartDataSet yDataSet = new BarChartDataSet (barEntry, barChartModel.dataSetLegend [i]);
yDataSet.SetColor(UIColor.Red);
Hope it helps you.

Multiple Task.Run not waiting/running properly

I'm trying to optimize a heavy process with some tasks, but it seems that after the first iteration it skips the ProcessQueue method run.
private void ImportSheet(Excel.Worksheet Data)
{
this._ImportedSlurry = new ConcurrentQueue<AnalizedDataDTO>();
this._LastRowReached = false;
this._CurrentRow = this._FirstRow;
while(!this._LastRowReached)
{
AnalizedDataDTO ImportedRow = this.ValidateRow(Data, _CurrentRow);
Task task1 = Task.Run(() => ProcessQueue(Enumerable.Range(this._CurrentRow, 50).ToArray(),Data));
Task task2 = Task.Run(() => ProcessQueue(Enumerable.Range(this._CurrentRow+50, 50).ToArray(),Data));
Task.WaitAll(task1, task2);
this._CurrentRow += 100;
}
}
private void ProcessQueue(int[] range, Excel.Worksheet Data)
{
for (int i = range.First(); i < range.Length; i++)
{
AnalizedDataDTO ImportedRow = this.ValidateRow(Data, i);
if (ImportedRow == null)
{
this._LastRowReached = true;
break;
}
else
{
this._ImportedSlurry.Enqueue(ImportedRow);
}
}
}
The problem is your for-loop, to be specific its if-condition.
You should either iterate over range, i.e.
foreach (var i in range)
or iterate over an index, i.e.
foreach (var idx = 0; idx < range.Length; idx++)
{
var i = range[idx];
For example assume that this._CurrentRow == 100, then your version of the for loop would look like:
for (int i = 100 /* range.First() */; i < 50 /* range.Length */; i++)

Dynamic ObservableCollection in TreeView?

I Have this problem where I can´t load my data into my TreeView but I have to do it manually instead. My static solution looks like this:
public static ObservableCollection<ClassOfDoom> GetAll()
{
ObservableCollection<ClassOfDoom> listToReturn = new ObservableCollection<ClassOfDoom>();
ClassOfDoom treeItem = null;
OUViewModel ouvm = new OUViewModel();
int[] tempOU = ouvm.HierarchyIntOU;
int[] tempP = ouvm.HierarchyIntParent;
treeItem = new ClassOfDoom("Root");
treeItem.cID = tempOU[0];
treeItem.pID = tempP[0];
for (int i = 0; i < ouvm.HierarchyIntParent.Length; i++)
{
if (treeItem.cID == tempP[i])
{
treeItem.ClassOfDooms.Add(new ClassOfDoom(tempOU[i].ToString()));
treeItem.ClassOfDooms.Last().pID = tempP[i];
treeItem.ClassOfDooms.Last().cID = tempOU[i];
for (int i1 = 0; i1 < ouvm.HierarchyIntParent.Length; i1++)
{
if (treeItem.ClassOfDooms.Last().cID == tempP[i1])
{
treeItem.ClassOfDooms.Last().Countries.Add(new ClassOfDoom(tempOU[i1].ToString()));
}
}
}
}
listToReturn.Add(treeItem);
return listToReturn;
}
This works but as you can see it´s only three levels and I wan´t a dynamic amount of levels. If someone wonders my ClassOfDooms list looks like this:
public ObservableCollection<ClassOfDoom> ClassOfDooms
{
get
{
if (classOfDooms == null) classOfDooms = new ObservableCollection<ClassOfDoom>();
return classOfDooms;
}
set { classOfDooms = value; }
}
I want to state again that I have no trouble reading data from my database or anything like that. The TreeView get´s the right information just not all of it.
EDIT: I solved it myself like this:
ClassOfDoom[] asdfDooms = new ClassOfDoom[ouvm.HierarchyIntParent.Length];
for (int i = 0; i < ouvm.HierarchyIntParent.Length; i++)
{
asdfDooms[i] = new ClassOfDoom();
asdfDooms[i].cID = tempOU[i];
asdfDooms[i].pID = tempP[i];
asdfDooms[i].name = tempPersonName + asdfDooms[i].cID.ToString();
}
for (int aint = 0; aint < ouvm.HierarchyIntParent.Length; aint++)
{
for (int i = 0; i < ouvm.HierarchyIntParent.Length; i++)
{
if (asdfDooms[aint].cID == asdfDooms[i].pID)
{
asdfDooms[aint].classOfDooms.Add(asdfDooms[i]);
}
}
}
listToReturn.Add(asdfDooms[0]);
Try implementing the INotifyPropertyChanged interface in conjunction with the HierarchicalDataTemplate control, this should ensure that changes made to the underlying data structure are reflected in the TreeView structure.

Categories

Resources