I am creating a program where you do rep of a stretch, so it adds one to amount of reps. I want to have a interval between each rep (not the current problem). I have this nested within the AllFramesReadyEventArgs. The problem is, since the SDK uses the AllFramesReadyEvent through out the whole program, so it is an infinite loop. How can I do each rep without it repeating when I don't want it to? Thanks in advance.
if (skeleton.TrackingState == SkeletonTrackingState.Tracked)
{
int whereIsX = (int)Canvas.GetLeft(ellipse1);
int whereToX = (int)Canvas.GetLeft(pspine);
whatToMultiplyX = whereToX / whereIsX;
int whereIsY = (int)Canvas.GetTop(ellipse1);
int whereToY = (int)Canvas.GetTop(pspine);
whatToMultiplyY = whereToY / whereIsY;
Canvas.SetTop(ellipse1, Canvas.GetTop(ellipse1) * whatToMultiplyY);
Canvas.SetLeft(ellipse1, Canvas.GetLeft(ellipse1) * whatToMultiplyX);
for (int i = 0; i < Doctor_ShoulderX.Count; i++)
{
Canvas.SetTop(doctorHand, Doctor_HandY[i] * whatToMultiplyY);
Canvas.SetTop(doctorElbow, Doctor_ElbowY[i] * whatToMultiplyY);
Canvas.SetTop(doctor_Shoulder, Doctor_ShoulderY[i] * whatToMultiplyY);
Canvas.SetLeft(doctorHand, Doctor_HandX[i] * whatToMultiplyX);
Canvas.SetLeft(doctorElbow, Doctor_ElbowX[i] * whatToMultiplyX);
Canvas.SetLeft(doctor_Shoulder, Doctor_ShoulderX[i] * whatToMultiplyX);
handX = Canvas.GetLeft(handright);
handY = Canvas.GetTop(handright);
elbowX = Canvas.GetLeft(elbowright);
elbowY = Canvas.GetTop(elbowright);
shoulderX = Canvas.GetLeft(shoulderright);
shoulderY = Canvas.GetTop(shoulderright);
shoulder_x.Text = "Shoulder X: " + shoulderX.ToString();
shoulder_y.Text = "Shoulder Y: " + shoulderY.ToString();
elbow_x.Text = "Elbow X: " + elbowX.ToString();
elbow_y.Text = "Elbow Y: " + elbowY.ToString();
hand_x.Text = "Hand X: " + handX.ToString();
hand_y.Text = "Hand Y: " + handY.ToString();
Patient_HandX.Add(handX);
Patient_HandY.Add(handY);
Patient_ElbowX.Add(elbowX);
Patient_ElbowY.Add(elbowY);
Patient_ShoulderX.Add(shoulderX);
Patient_ShoulderY.Add(shoulderY);
}
}
I'll use answer cause more writing than comment can handle :)
aleframesready action is invoked in every frame. so if here you have limit of 20 in loop
for (int i = 0; i < Doctor_ShoulderX.Count; i++)
it will be invoked from 0 to 20 every time. Imo you you should create a static boolean variable in the class. After the code go out from the loop set it to false (if on start it had true). Before you invoke the loop check if the variable is set to true. If yes invoke the loop, if no just continue
Related
I have a 3D array with arbitrary X, Y and Z lengths
I want to iterate over it in a parallel.for loop, which can't be nested without wasting tasks afaik
Instead, the single loop's length is ArrayLengthX * ArrayLengthY * ArrayLengthZ
Can I mathematically get the current 3D array element from the current iteration + the X, Y and Z length of the array? if so how?
edit : Example below, hope this is enough to understand what's going on
DimensionSettings dimensionSettings = dimension.Settings;
Vector3Int DimSize = dimensionSettings.GetDimensionSize();
TaskProgressMutex.WaitOne();
CurrentProgress = 0;
TotalProgress = DimSize.x * DimSize.y * DimSize.z;
TaskProgressMutex.ReleaseMutex();
int ChunkAmount = DimSize.x * DimSize.y * DimSize.z;
GD.Print("Chunks to generate : " + ChunkAmount);
ParallelLoopResult result = Parallel.For(0, ChunkAmount, (int i) =>
{
GD.Print(i);
int xcoords = ???;
int ycoords = ???;
int zcoords = ???;
Vector3Int ChunkCoords = new Vector3Int(xcoords, ycoords, zcoords);
GD.Print("Current Chunk : " + xcoords + " " + ycoords + " " + zcoords);
GenerateChunk(ChunkCoords, seed);
TaskProgressMutex.WaitOne();
CurrentProgress += 1;
//GD.Print("Chunk " + xcoords + " " + ycoords + " " + zcoords + " finished generating. Current Progress : " + CurrentProgress);
TaskProgressMutex.ReleaseMutex();
});
My suggestion is to parallelize only the outer loop (the X dimension), and do normal (non-parallel) inner loops for the Y and Z dimensions:
ParallelOptions options = new()
{
MaxDegreeOfParallelism = Environment.ProcessorCount
};
Parallel.For(0, dimSize.X, options, (int x) =>
{
for (int y = 0; y < dimSize.Y; y++)
{
for (int z = 0; z < dimSize.Z; z++)
{
Vector3Int chunkCoords = new (x, y, z);
GenerateChunk(chunkCoords, seed);
}
}
});
The idea is to increase the chunkiness of the parallel work. In general the more chunky is the workload, the less is the overall overhead of the parallelization. At the other extreme, if each unit of work is too chunky, the partitioning of the work may become imbalanced.
Also when you use the Parallel.For/Parallel.ForEach/Parallel.Invoke methods I am suggesting to specify always the MaxDegreeOfParallelism, although this suggestion deviates from the general advice offered in the official documentation.
I make a loop like this :
int total;
total = ((toVal - fromVal) + 1) * 2;
RadProgressContext progress = RadProgressContext.Current;
progress.Speed = "N/A";
finYear = fromVal;
for (int i = 0; i < total; i++)
{
decimal ratio = (i * 100 / total);
progress.PrimaryTotal = total;
progress.PrimaryValue = total;
progress.PrimaryPercent = 100;
progress.SecondaryTotal = 100; // total;
progress.SecondaryValue = ratio;//i ;
progress.SecondaryPercent = ratio; //i;
progress.CurrentOperationText = "Step " + i.ToString();
if (!Response.IsClientConnected)
{
//Cancel button was clicked or the browser was closed, so stop processing
break;
}
progress.TimeEstimated = (total - i) * 100;
//Stall the current thread for 0.1 seconds
System.Threading.Thread.Sleep(100);
}
Now i want a specific method to run according to toVal & fromVal
in the previous loop but not with the same number of cycles
i want to to run it in a loop like this :
for (fromVal; fromVal < toVal ; fromVal++)
{
PrepareNewEmployees(calcYear, fromVal);
}
for example :
fromVal = 2014
toVal = 2015
so i want to run twice not 4 times! like this :
PrepareNewEmployees(calcYear, 2014);
PrepareNewEmployees(calcYear, 2015);
but in the previous loop for (int i = 0; i < total; i++)
You're missing the point of progress bar updating. You're not supposed to run 4 iterations and do some work every 2 iterations, but the oposite. Do a loop like:
for (int i = fromVal; i < toVal; i++)
{
PrepareNewEmployees(...);
decimal ratio = ((double)toVal-i)/(toVal-fromVal) *100;
//Some other things, that need to be done twice in an iteration
}
Because you are using Thread's already, consider to implement following:
public void ResetProgress()
{
SetProgress(0);
}
public SetProgress(int percents)
{
// set progress bar to a given percents/ratio
// you will have to use Invoke and blablabla
}
Then any your job will looks like this
ResetProgress();
// note: you need to remember from which value you start to be able to calculate progress
for (int i = startVal; i < toVal ; i++)
{
PrepareNewEmployees(calcYear, i);
SetProgress(100 * (i - startVal) / (toVal - startVal)); // in percents [0-100]
}
// optional, required if you exit loop or use suggestion below
SetProgress(100);
You can also optimise it, to do not update progress after each step, but after certain numbers of steps. To example, instead of calling SetProgress you do
if(i % 10 == 0)
SetProgress();
This will calls SetProgress ten times less often. Of course, there are some assumptions, like: i starts from 0 and if you want to have 100% bar at the end, then i should be dividable by 10. Just an idea to start.
Okay at the moment I my self am new to programming and learning it slowly. At the moment I am taking programming classes to help better understand programming. I have ran in to a problem that has stumped me.
Now while I can do the assignment in a different way and manner as compared to what I provided. My question is, why is this happening? I get no errors, what so ever, the only thing that happens is after the input the Console Fezzes. I want to know what I did wrong.
static void Main(string[] args)
{
double[] Population = new double[6];
string[] Years = { "2", "3", "4", "5", "6", "7" };
double GrowthPercentage = 0.0;
double MathPercentage = 0.0000;
double ActualGrowth = 0.0;
int WhileCounter = 0;
//Ask user for Population of Roarkville
Console.WriteLine("Enter the Population of RoarkVille: ");
//Read Population and store
Population[0] = Convert.ToDouble(Console.ReadLine());
//Ask user for Growth percentage
Console.WriteLine("Enter the Growth percentage ");
//Read Growth Percentage
GrowthPercentage = Convert.ToDouble(Console.ReadLine());
//Calculation of Growth Percentage: Growth Percentage/100 = Math Percentage
MathPercentage = GrowthPercentage / 100;
//ActualGrowth = Population * Math Percentage
//Population2 = ActualGrowth + Population
while (WhileCounter < 5)
{
ActualGrowth = Population[WhileCounter] + MathPercentage;
WhileCounter++;
Population[WhileCounter] = ActualGrowth + Population[WhileCounter--];
}
for (int i = 0; i < Population.Length; i++)
{
Console.WriteLine("Population of 201{0:d}", Years[i]);
Console.WriteLine(Population[i]);
}
//Display 2012 Population
//Display 2013 Population
//Display 2014 Population
//Display 2015 Population
//Display 2016 Population
//Display 2017 Population
Console.ReadLine();
}
so what happen is that when you input on the growth percentage using this code:
while (Counter < 5)
{
ActualGrowth = Population[Counter] + MathPercentage;
Counter++;
Population[Counter] = ActualGrowth + Population[Counter--];
}
for (int i = 0; i < Population.Length; i++)
{
Console.WriteLine("Population of 201{0:d}", Years[i]);
Console.WriteLine(Population[i]);
}
the numbers that will you input will be infinite on the growth percentage:
this one can help you also
while (Counter < 5)
{
ActualGrowth = Population[Counter] + MathPercentage;
Counter++;
Population[Counter] = ActualGrowth + Population[Counter-1];
}
for (int i = 0; i < Population.Length; i++)
{
Console.WriteLine("Population of 201{0:d}", Years[i]);
Console.WriteLine(Population[i]);
}
The ++ operator changes the actual value of the variable, so WhileCounter++ increases the variable by 1
The -- operator does the same, which is not what you want to do in the line
Population[WhileCounter] = ActualGrowth + Population[WhileCounter--];
Instead, use WhileCounter - 1 , like so
Population[WhileCounter] = ActualGrowth + Population[WhileCounter - 1];
WhileCounter++;
Population[WhileCounter] = ActualGrowth + Population[WhileCounter--];
The value of WhileCounter never changes as far as the loop is concerned. In the loop body you increment WhileCounter and proceed to immediately decrement it, so the condition WhileCounter < 5 is always true.
You may as well have written
int WhileCounter = 0;
while(WhileCounter < 5)
{
WhileCounter += 1; // WhileCounter == 1
WhileCounter -= 1; // WhileCounter == 0
}
// aint never gunna happen
You should read up on the following operators and understand what they actually do:
--
++
I want to display 2 sets of data on the one list box, for example, I would wont to display the 7 times table and the 8 times table on the same listbox. Here is how I get the first set of data displaying:
int awnser = 0;
int z;
z = int.Parse(textBox1.Text);
for (int i = 0; i < 11; i++)
{
awnser = z * i;
listBox6.Items.Add(z + " * " + i + " = " + awnser.ToString());
}
But how do I get a line break or separation so I can put the 8 times table just underneath?
How about this?
EDIT Insert it AFTER your loop
listBox6.Items.Add(z + " * " + i + " = " + awnser.ToString());
}
listBox6.Items.Add("--------------------");
In WPF this is easy to do using a custom template, but in WinForms I think you must do it by rendering the list items yourself.
Look at this example where they override the OnDrawItem method: http://www.syncfusion.com/FAQ/windowsforms/faq_c87c.aspx#q627q
This is a piece of my code, it is called every second, after about 10 seconds the values start to become weird (see below):
double a;
double b;
for (int i = 0; i < currAC.Length; i++ )
{
a = currAC[i];
b = aveACValues[i];
divisor = (a/b);
Console.WriteLine("a = " + a.ToString("N2") + "\t" + "b = " + b.ToString("N2"));
Console.WriteLine("divisor = " + divisor);
Console.WriteLine("a+b = " + (a+b));
}
and the output:
a = -0.05 b = 0.00
divisor = 41
a+b = -0.0524010372273268
currAC and aveACValues are double[]
what on earth is going on???? The addition result is correct every time, but the division value is wrong, yet it is reading a and b correctly??
EDIT: '41' is the value of the first calculation, ie when a = currAC[0], but this should not remain???
if b == -0.001219512195122, then a/b==41, and a+b==-0.051219512195122 - so something around those areas (rounding etc) sounds feasible...
Also; note that for some arithmetic, it is possible it is using values that are still in registers. Registers may exhibit slightly different accuracy (and so give different results) than local variables.
double can result in inprecise results, due to the specification (see msdn). You might want to use decimal instead, it offers more precision.
What happens if the variables are declared as close to where they are used as is possible?
for (int i = 0; i < currAC.Length; i++ )
{
double a = currAC[i];
double b = aveACValues[i];
double divisor = (a/b);
Console.WriteLine("a = " + a.ToString("N2") + "\t" + "b = " + b.ToString("N2"));
Console.WriteLine("divisor = " + divisor.ToString());
Console.WriteLine("a+b = " + (a+b).ToString());
}
This would ensure that you have a fresh "divisor" each time and will not be affected by other scopes.