Unreachable Code Detected C# - c#

Im trying to Post an unlimited number of likes but looping the cookies and proxies based on the how many cookies are stored in the array. Apparently i++ is unreachable code. What is the reason for that?
public void PostLikes()
{
PostLike postLike = new PostLike();
for (int i =0;i<this.cookies.ToArray().Length;i++)
{
for (int j = 0; ; j++)
{
postLike.PostLike(this.cookies[i], this.importedProxies[i], this.proxyUsernameTextbox, this.proxyPasswordTextbox, this.postsIds[j]);
}
}
}

The real problem is that:
for (int j = 0; ; j++)
Produces an infinite loop, assuming you don't have any other control statements inside (e.g. break, return, goto, throw)
You probably meant to do something like this:
for (int j = 0; j < this.postsIds.Length; j++)
{
...
}

And don't do this
for (int i =0;i<this.cookies.ToArray().Length;i++)
because
this.cookies.toArray().Length
its evaluating in every iteration of the for loop, you are making 'this.cookies' to array every time so just you get its length? :) You are increasing the complexity of the method

for (int j = 0; ; j++)
This is a dead loop, so i++ won't be reached

Related

Single-Threaded enumeration works, but Multi-Threaded does not, not sure why

Some more details:
I have a list of arrays of circle objects. Each circle has a list area that corresponds to a pixel on a bitmap.
When I call diffCircles, I'm comparing the vector2's in each area of each circle in each array iteratively to remove their area from the original circle.
I'm trying to add multithreading to a project of mine (to process a lot of short operations in a small span of time).
The original method is:
for(int i = 0; i < generations.Count-1; i++)
{
for(int j = 0; j < generations[i].Length; j++)
{
foreach (Circle circle in generations[i][j].children)
{
Circle.DiffCircles(generations[0][0], circle);
}
}
}
And with the above implementation, the program works perfectly as intended (it's just slow).
However if I try to change it to:
for (int i = 0; i < generations.Count - 1; i++)
{
for (int j = 0; j < generations[i].Length; j++)
{
foreach (Circle circle in generations[i][j].children)
{
Thread diffCirc = new Thread(() => Circle.DiffCircles(generations[0][0], circle));
diffCirc.Start();
}
}
}
The program stops working as intended.
The DiffCircles method is
public static void DiffCircles(Circle original, Circle toSubtract)
{
for (int i = 0; i < original.area.Count - 1; i++)
{
for (int j = 0; j > toSubtract.area.Count - 1; j++)
{
if (original.area[i-1].X == toSubtract.area[j-1].X
&& original.area[i-1].Y == toSubtract.area[j-1].Y)
{
original.area.Remove(original.area[i-1]);
}
}
}
}
Could I get any advice? I've never worked with multi-threading before. I can't understand why the multithreaded implementation doesn't work, but the singlethreaded implementation does. Would appreciate any advice!
Maybe deadlock caused by thread contention.
You can try to lock your thread.
lock(new object()){
Thread diffCirc = new Thread(() => Circle.DiffCircles(generations[0][0], circle));
diffCirc.Start();
}

How can I skip loop for first and last element of array and set them to constant value?

I would like the loop to start from the second element and to end before the last one.
So that I can set the first and last one to constant value.
for (int i = 0; i < n; i++) {
for (int j = 0; j < 3; j++) {
Console.Write($"A[{i},{j}] = ");
tab[i, j] = double.Parse(Console.ReadLine());
}
}
You can use LINQ simply:
var result = yourArray.Skip(1).SkipLast(1).Prepend(theConstValue)
.Append(theConstValue).ToArray();
The Prepend, Adds a value to the beginning of the sequence.
The Append, Appends a value to the end of the sequence.
Why don't do something like that?
for (int i = 1; i < n - 1; i++)
{
//your code
}
Let the loop start at element 1 and end it one earlier

Jagged array of tasks - Concurrency Issues

I am defining a jagged array of threads (such that each thread can operate on the directory tree of its own) in this manner
Task[][] threads = new Task[InstancesDir.Length][];
for (int i = 0; i < InstancesDir.Length; i++)
{
threads[i] = new Task[InstancesDir[i].Length];
}
for (int i = 0; i < FilesDir.Length; i++)
{
for (int j = 0; j < FilesDir[i].Length; j++)
{
threads[i][j] = Task.Run(() =>
{
Calculate(i, j, InstancesDir, FilesDir, PointSum);
});
}
Task.WaitAll(threads[i]);
}
But in calculate i always get value of j >= FilesDir[i].Length . I have also checked that objects are passed by value except arrays. What could be a workaround for this and what could be the reason for this behavior?
PS. Introducing a shared lock might help in mitigation the concurrency issue but i want to know about the reason for such behavior.
But in calculate i always get value of j >= FilesDir[i].Length
This isn't a concurrency issue, as your for loop is executing on a single thread. This happens because the lambda expression is closing over your i and j variables. This effect is called Closure.
In order to avoid it, create a temp copy before passing both variables to Task.Run:
var tempJ = j;
var tempI = i;
threads[tempI][tempJ] = Task.Run(() =>
{
Calculate(tempI, tempJ, InstancesDir, FilesDir, PointSum);
});

reimplement a loop using Parallel.For

how to re implement the loop below using Parallel.For?
for (int i = 0; i < data.Length; ++i)
{
int cluster = clustering[i];
for (int j = 0; j < data[i].Length; ++j)
means[cluster][j] += data[i][j]; // accumulate sum
}
getting better performance and speed up is the goal.
You can mostly just replace the outer loop. However, you need to take care with the setting, as you're setting values from multiple threads:
Parallel.For(0, data.Length, i =>
{
int cluster = clustering[i];
for (int j = 0; j < data[i].Length; ++j)
Interlocked.Add(ref means[cluster][j], data[i][j]);
});
However, this may not run any faster, and may actually run significantly slower, as you could easily introduce false sharing since everything is reading from and writing to the same arrays.

Checkers Board Assistance

I'm just wondering if there is a simpler way of doing this:
for (int i = 0; i < 1; i++)
{
for (int j = 0; i < 8; j+2)
{
board[ i, j ] = 2;
board[( i + 1 ), j ] = 2;
board[( i + 2 ), j ] = 2;
}
}
What I'm trying to do is place checkers pieces on the actual checkers board. So this is to place the black pieces on the top.
P.S. If you could also give me some help on the bottom set of pieces(white).
Apart from fixing the loop you could otherwise explicitly place the pieces, makes it more readable
int[,] board = new[,]{{1,0,1,0,1,0,1,0},
{0,1,0,1,0,1,0,1},
{1,0,1,0,1,0,1,0},
{0,0,0,0,0,0,0,0},
{0,0,0,0,0,0,0,0},
{0,1,0,1,0,1,0,1},
{1,0,1,0,1,0,1,0},
{0,1,0,1,0,1,0,1}};
Modulo will do the trick but i think that TonyS's anwser was my first reaction and I would prefer that insteed the one shown below.
char[,] board = new char[8,8];
private void InitializeBoard()
{
string BoardRepresentation = "";
//for every board cell
for (int i = 0; i < 8; i++)
{
for (int j = 0; j < 8; j++)
{
//initialize board cell
board[i, j] = '0';
if (j <= 2)//black top
{
//Modulo is the trick
if ((j - i) == 0 || ((j - i) % 2) == 0)
{
board[i, j] = 'B';
}
}
else if (j >= 5) //white bot
{
if ((j - i) == 0 || ((j - i) % 2) == 0)
{
board[i, j] = 'W';
}
}
}
}
for (int j = 0; j < 8; j++)
{
for (int i = 0; i < 8; i++)
{
BoardRepresentation += board[i, j] + " ";
}
BoardRepresentation += Environment.NewLine;
}
}
You missed to tell us what exactly is it that you want to achieve. I see several big errors in your code, so I'll assume that you don't have full grasp of how for loop works. I hope it's not too presumptuous that I'm trying to explain it here
For loop
For loop is used to execute same portion of code several times. How many times it will be executed depends on conditions you set. Most often, you will see it in this format:
for (int i = 0; i < n; i++)
{
// Some code
}
This for loop executed code within the brackets ({ and }) n times. This is not only way to define a loop. More thorough definition of a loop is following:
for (<initialization>; <condition>; <afterthought>)
Initialization - You can some variables needed for looping. This is executed once before code within the loop is executed first time. This is optional, and you can leave it empty and use variable declared before in condition.
Condition - This is executed before each execution of code within the loop. If condition expression evaluates to true, loop is executed. Once loop is executed and afterthought is executed, condition is evaluated again and again until it evaluates to false. Condition is also optional. If you leave it out, in C# loop will be executed again until you break the loop in a different way.
Afterthought - This is executed each time code within the loop is finished executing. This is usually used to increment a variable on which loop depends. This is also optional.
Fixing your code
I assume you wanted to mark fields in a 8x8 matrix like in a checkerboard, although this is not stated in your question. You could do it this way:
// For each row in a board (start with 0, go until 7, increase by 1)
for (int i = 0; i < 8; i++)
{
// start coloring the row. Determine which field within the row needs
// to be black. In first row, first field is black, in second second
// field is black, in third row first field is black again and so on.
// So, even rows have black field in first blace, odd rows have black
// on second place.
// We calculate this by determining division remained when dividing by 2.
int firstBlack = i % 2;
// Starting with calculated field, and until last field color fields black.
// Skip every second field. (start with firstBlack, go until 7, increase by 2)
for (int j = firstBlack; j < 8; j += 2)
{
// Color the field black (set to 2)
board[i][j] = 2;
}
}
You can see my comments inline.
Big errors in your code
// This loop would be executed only once. It goes from 0 to less than 1 and is increased
// after first execution. You might as well done it without the loop.
for (int i = 0; i < 1; i++)
{
// This doesn't make sense, because you use i in condition, and initialize
// and update j.
// Also, you are trying to update j, but you are not doing so. You are not
// assigning anything to you. You should do j+=2 to increase by two. Or you
// can do j = j + 2
for (int j = 0; i < 8; j+2)
{
// I didn't really understand what you were trying to achieve here
board[ i, j ] = 2;
board[( i + 1 ), j ] = 2;
board[( i + 2 ), j ] = 2;
}
}

Categories