I have three different function which i call based on the condition. When data is lesser my application performance is good. However, i have around 11000 rows of data my application taking 1-2 hour to complete the process.
I have some logic written in my functions which filters data and add into collection. Is there is any way i can improve the performance of my application using multi-threading or parallel processing. so that my function runs parallel and performance get improved.
I created a sample of my requirement below:
private void button1_Click(object sender, EventArgs e)
{
String[] AllData = new String[7];
AllData[0] = "1";
AllData[1] = "2";
AllData[2] = "3";
AllData[3] = "1";
AllData[4] = "2";
AllData[5] = "3";
AllData[6] = "1";
for (int i = 0; i < AllData.Length; i++)
{
CommonFunction(AllData[i]);
}
}
private void CommonFunction(String AllData)
{
if (AllData == "1")
{
Function1(1);
}
else if (AllData == "2")
{
Function2(2);
}
else
{
Function3(3);
}
}
private void Function1(int ID)
{
for (int i = 0; i < 10; i++)
{
}
}
private void Function2(int ID)
{
for (int i = 0; i < 10; i++)
{
}
}
private void Function3(int ID)
{
for (int i = 0; i < 10; i++)
{
}
}
Any help is highly appreciated.
Instead of sequential for loop, you can try to use Parallel.ForEach.
for (int i = 0; i < AllData.Length; i++)
{
CommonFunction(AllData[i].ToString());
}
can be converted to
Parallel.ForEach(AllData, someData =>
{
CommonFunction(someData);
});
Related
So i'm trying to make a program that uses the League of Legends API in C#.
I found a NuGet package that makes using the API a lot easier.
Everything works fine so far except the code execution stops after the
first for loop i use.
Here's the code: (of course i took out the api key)
string[] summnames;
long[] champids;
long[] teamids;
long[] champs;
CreepScoreAPI.ParticipantLive[] enemy;
CreepScoreAPI.ParticipantLive[] ally;
CreepScoreAPI.ParticipantLive centsumm;
CreepScoreAPI.ParticipantLive[] champsss;
CreepScoreAPI.ChampionStatic[] champions;
CreepScoreAPI.Summoner[] sumners;
CreepScoreAPI.League[] leaguesz;
Dictionary<string, List<CreepScoreAPI.League>>[] leagues;
int[] champidsint;
string[] champnames;
int s;
int se;
public async Task<string> game(string summname)
{
string data;
CreepScoreAPI.CreepScore cs = new CreepScoreAPI.CreepScore("api key");
var summoner = await cs.RetrieveSummoner(CreepScoreAPI.CreepScore.Region.EUNE, summname);
long summid = summoner.id;
var thegame = await summoner.RetrieveCurrentGameInfo();
CreepScoreAPI.ParticipantLive[] participants = thegame.participants.ToArray();
for (int i = 0; i <= 9; i++) { summnames[i] = participants[i].summonerName;}
for (int i = 0; i <= 9; i++) { champids[i] = participants[i].championId;}
for (int i = 0; i <= 9; i++) { teamids[i] = participants[i].teamIdLong;}
for (int i = 0; i <= 9; i++) { champids[i] = participants[i].championId;}
for (int i = 0; i <= 9; i++) { champidsint[i] = Convert.ToInt32(champids[i]);}
for (int i = 0; i <= 9; i++) { champions[i] = await cs.RetrieveChampionData( CreepScoreAPI.CreepScore.Region.EUNE, champidsint[i], CreepScoreAPI.Constants.StaticDataConstants.ChampData.All, "en_US", "7.1.1",false ); }
for (int i = 0; i <= 9; i++) { champnames[i] = champions[i].name; }
for (int i = 0; i <= 9; i++) { sumners[i] = await cs.RetrieveSummoner(CreepScoreAPI.CreepScore.Region.EUNE, summnames[i]); }
for (int i = 0; i <= 9; i++) { leagues[i] = await sumners[i].RetrieveLeague(); }
/* teamsorter */
foreach (CreepScoreAPI.ParticipantLive p in participants)
{
if (p.summonerId == summid)
{
centsumm = p;
}
if (p.teamIdLong == centsumm.teamIdLong)
{
ally[s] = p;
s++;
}
if (p.teamIdLong != centsumm.teamIdLong)
{
enemy[se] = p;
se++;
}
}
data = " I'LL FORMAT A STRING THAT OUTPUTS ALL THE DATA I NEED HERE";
return data;
}
When I call the game function and input the name of the summoner i gets to the first for loop and doesn't even populate the summnames[] array and stops execution with no error code.
What I'm trying to do with all the loops is populate the variables I made before the function so I can use them later for other purposes.
I think you should assign all the arrays and set their length to at least 10.
This could solve the problem.
Is there a way to convert it, from exact value text become consist of? So I don't need to type Ballet instead of Bal.
Here's the code:
private void button6_Click_1(object sender, EventArgs e)
{
ColumnView View = gridControl1.MainView as ColumnView;
View.BeginUpdate();
try
{
int rowHandle = 0;
DevExpress.XtraGrid.Columns.GridColumn col = View.Columns["genre"];
while (true)
{
// // Locate the next row
rowHandle = View.LocateByValue(rowHandle, col, textBox6.Text);
// // Exit the loop if no row is found
if (rowHandle == DevExpress.XtraGrid.GridControl.InvalidRowHandle)
break;
//// Perform specific operations on the found row
gridView1.FocusedRowHandle = rowHandle;
rowHandle++;
}
}
finally { View.EndUpdate(); }
}
for (int i = 0; i < gridView1.VisibleRowCount; i++)
{
var row = gridView1.GetDataRow(i);
var genre = row["ColumnName"].ToString(); //ColumnName is your genre Column name
if(genre.StartsWith(textBox6.text)){
//here you can set row sellected
}
}
I dont have experience with devexpress but you can try it like this.
i dont if this what u seek, but it solved my own problem
for (int i = 0; i < gridView1.RowCount; i++)
{
var rosw = gridView1.GetDataRow(i);
var genre = rosw["genre"].ToString();
int tmpg = 0;
// //tmpg = genre.IndexOf(textBox8.Text, StringComparison.OrdinalIgnoreCase);
if (genre.IndexOf(textBox8.Text, StringComparison.OrdinalIgnoreCase) >= 0)
{
//if (tmpg >= 1)
// MessageBox.Show(genre);
gridView1.FocusedRowHandle = i;
break;
}
}
I have an application which must contain the items in large quantities and continuously into listviews control:
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
deg_loadfile load = new deg_loadfile(loadfile);
load.BeginInvoke(new AsyncCallback(loadcallback),null);
}
void countfile() {
int listcount = listView1.Items.Count;
for (int x = 0; x < listcount; x++) {
listView1.BeginInvoke((MethodInvoker)delegate {
listView1.Items[x].SubItems[1].Text = "ok";
listView1.Items[x].SubItems[2].Text = "ok";
listView1.Items[x].SubItems[3].Text = "done";
},x);
}
}
void countcallback(IAsyncResult ae) {
MessageBox.Show("count finished");
}
delegate void deg_count();
void loadcallback(IAsyncResult ae) {
MessageBox.Show("finished");
}
delegate void deg_loadfile();
void loadfile() {
string file = File.ReadAllText("hughlist.txt");
string[] files = Regex.Split(file,"\n");
foreach (string str in files) {
listView1.BeginInvoke((MethodInvoker)delegate {
ListViewItem item = new ListViewItem(str);
item.SubItems.Add("");
item.SubItems.Add("");
item.SubItems.Add("");
listView1.Items.Add(item);
},str);
}
}
private void button2_Click(object sender, EventArgs e)
{
deg_count count = new deg_count(countfile);
count.BeginInvoke(new AsyncCallback(countcallback),null);
}
The code above throws system.argumentoutofrangeexception (please see image below)
link image: http://i.stack.imgur.com/WJ7sA.png
The iteration has exceeded the conditions that I have given, why did it happen (note:in my case, i must use "for" instead of "foreach" or other enumeration) ?
thx
UPDATE:
thx to #saruman for the answer (keyword is "access to modified closure"), i've updated the code as below
for (int x = 0; x < listcount; x++) {
var x1=x;
listView1.BeginInvoke((MethodInvoker)delegate
{
listView1.Items[x1].SubItems[1].Text = "ok";
listView1.Items[x1].SubItems[2].Text = "ok";
listView1.Items[x1].SubItems[3].Text = "done";
}, x1); }
Look up access to modified closure
Try this
var listcount = listView1.Items.Count;
for (var x = 0; x < listcount; x++)
{
var x1 = x;
listView1.BeginInvoke((MethodInvoker)delegate
{
listView1.Items[x1].SubItems[1].Text = "ok";
listView1.Items[x1].SubItems[2].Text = "ok";
listView1.Items[x1].SubItems[3].Text = "done";
});
}
In C#, I have checked list boxes, that I need to store the data in arrays, but when I start the event that writes the objects to the array, I have to set the size of the array, which I naturally set to the amount of items checked. However, the items checked, for both checked list boxes I have is 1, no matter how many I check. Can someone help?
public partial class Form3 : Form
{
public static object[] dtype;
public static bool loaded = false;
bool typeselecte = false;
bool typeselectd = false;
public Form3()
{
InitializeComponent();
}
private void Form3_Shown(object sender, EventArgs e)
{
if (loaded)
{
int counte = 0;
int countd = 0;
types1.Items.AddRange(dtype);
types2.Items.AddRange(dtype);
if (typeselecte)
{
for (int i = 0; i < types1.Items.Count; i++)
{
if (i == Form1.enumber[counte])
{
types1.SelectedItems[i] = Form1.esearch[i];
counte++;
}
}
}
if (typeselectd)
{
for (int j = 0; j < types2.Items.Count; j++)
{
if (j == Form1.dnumber[countd])
{
types2.SelectedItems[j] = Form1.dsearch[j];
countd++;
}
}
}
}
}
public void dtypes()
{
dtype = new object[types1.Items.Count];
for (int i = 0; i < types1.Items.Count; i++)
{
dtype[i] = types1.Items[i];
}
}
private void button1_Click(object sender, EventArgs e)
{
if (types1.SelectedItems.Count > 0)
typeselecte = true;
if (types2.SelectedItems.Count > 0)
typeselectd = true;
Form1.esearch = new object[types1.SelectedItems.Count];
Form1.dsearch = new object[types2.SelectedItems.Count];
Form1.enumber = new int[types1.SelectedItems.Count];
Form1.dnumber = new int[types2.SelectedItems.Count];
int counte = 0;
int countd = 0;
if (typeselecte)
{
for (int i = 0; i < types1.SelectedItems.Count; i++)
Form1.esearch[i] = types1.SelectedItems[i];
}
if (typeselectd)
{
for (int j = 0; j < types2.SelectedItems.Count; j++)
Form1.dsearch[j] = types2.SelectedItems[j];
}
if (typeselecte)
{
for (int k = 0; k < types1.Items.Count; k++)
{
if (Form1.esearch[k] == types1.Items[k])
{
Form1.enumber[counte] = k;
counte++;
}
else
{
k--;
}
}
}
if (typeselectd)
{
for (int l = 0; l < types2.Items.Count; l++)
{
if (Form1.dsearch[l] == types2.Items[l])
{
Form1.dnumber[countd] = l;
countd++;
}
else
{
l--;
}
}
}
this.Close();
}
}
Form1.esearch and dsearch are object arrays, which the size hasn't been picked yet, and e and dnumber are int arrays that have unknown size as well, I just didn't feel the need to put in that code.
I believe you need to use the property CheckedItems as opposed to SelectedItems.
I want to get list of methods in current file, when my tool is fired, but I don't have idea how that combobox is nested in the _applicationObject object. Can you help me?
I did it this way:
private void ListMethods()
{
CodeNamespace codenamespace = null;
for(int i = 1; i <= _applicationObject.ActiveWindow.ProjectItem.FileCodeModel.CodeElements.Count; i++)
{
if(_applicationObject.ActiveWindow.ProjectItem.FileCodeModel.CodeElements.Item(i).Kind == vsCMElement.vsCMElementNamespace)
{
codenamespace = (CodeNamespace)_applicationObject.ActiveWindow.ProjectItem.FileCodeModel.CodeElements.Item(i);
}
}
for(int i = 1; i <= codenamespace.Members.Count; i++)
{
if(codenamespace.Members.Item(i) is CodeClass)
{
ListMethodsForClass(codenamespace.Members.Item(i) as CodeClass);
}
}
}
private void ListMethodsForClass(CodeClass codeclass)
{
for (int i = 1; i <= codeclass.Members.Count; i++)
{
message(codeclass.Members.Item(i).Name);
}
}
I hope it will help in the future some of you :)