vendredi 18 avril 2014

Multithreading - C# TaskFactory changements des paramètres - Stack Overflow


So i have the following code:


public void tViewers(int? start, int? stop)
{
for (int? i0 = start; i0 <= stop; i0++)
{
StartLabel:
Viewer v = new Viewer(channelNameTextBox.Text, this);
if (urlWithTokens.Contains(v.getViewerLink()))
{
goto StartLabel;
}
else
{
if (v.getViewerLink() != "")
{
Console.WriteLine("[V #" + i0 + "] SUCCESS");
urlWithTokens.Add(v.getViewerLink());
}
else
{
Console.WriteLine("Channel not found.");
showError("The channel name is not valid.", true);
this.Invoke(new Action(() => this.botControlls.Enabled = true));
urlWithTokens.Clear();
}
}
v = null; // clear
}
Console.WriteLine("[V] " + start + " to " + stop + " COMPLETED");
start = null;
stop = null;
GC.SuppressFinalize(this);
}

Which is executed by:


for (int i = 0; i < maxThreads; i++)
{
taskFactory.StartNew(() => tViewers(someValue, someHigherValue));
}

The problem here is the local parameters "start" and "stop" in tViwers and it returst some stange values.


fx if i print "start" it should return the "someValue" and "someHigherValue" depending on what "thread" it is running in, however it returns strange values 40, 50 or something (even if it should return 1, 2, 3...


I have tried using the GC.SuppressFinalize(this); and setting the int's to null my allowing them to be null (int?). However the problem is still there...


Can someone help me?



So i have the following code:


public void tViewers(int? start, int? stop)
{
for (int? i0 = start; i0 <= stop; i0++)
{
StartLabel:
Viewer v = new Viewer(channelNameTextBox.Text, this);
if (urlWithTokens.Contains(v.getViewerLink()))
{
goto StartLabel;
}
else
{
if (v.getViewerLink() != "")
{
Console.WriteLine("[V #" + i0 + "] SUCCESS");
urlWithTokens.Add(v.getViewerLink());
}
else
{
Console.WriteLine("Channel not found.");
showError("The channel name is not valid.", true);
this.Invoke(new Action(() => this.botControlls.Enabled = true));
urlWithTokens.Clear();
}
}
v = null; // clear
}
Console.WriteLine("[V] " + start + " to " + stop + " COMPLETED");
start = null;
stop = null;
GC.SuppressFinalize(this);
}

Which is executed by:


for (int i = 0; i < maxThreads; i++)
{
taskFactory.StartNew(() => tViewers(someValue, someHigherValue));
}

The problem here is the local parameters "start" and "stop" in tViwers and it returst some stange values.


fx if i print "start" it should return the "someValue" and "someHigherValue" depending on what "thread" it is running in, however it returns strange values 40, 50 or something (even if it should return 1, 2, 3...


I have tried using the GC.SuppressFinalize(this); and setting the int's to null my allowing them to be null (int?). However the problem is still there...


Can someone help me?


0 commentaires:

Enregistrer un commentaire