R
RayLopez99
Why the below result? From Chap. 19 of Albahari. Makes no sense.
One or both of these threads should be outputting the AAA and CCC
strings, but they are not. see the asterisk //***
RL
/* OUTPUT
inside Print now
inside Print now
int, string are: 22, BBB...is not threaded here
The thread '<No Name>' (0xfe0) has exited with code 0 (0x0).
int, string are: 22, BBB...is not threaded here
myClassB is like BBB?: 22,BBB...is not threaded here
..............
inside Print2 now
int, string are: 333, DDD is not threaded here?! compare with above
inside Print2 now
int, string are: 333, DDD is not threaded here?! compare with above
The thread '<No Name>' (0xe3c) has exited with code 0 (0x0).
* */
private void Function001()
{
Class1 myClassA = new Class1();
myClassA.int1 = 11; myClassA.string1 =
"AAA..is..threaded!"; //*** //??!! not captured despite being before
t.Start?! why?
Thread t = new Thread(delegate() { Print(myClassA); });
t.Start(); ////*** outer-variable capture as per p. 643
Albahari *anyway!* despite being here, upstream of below?! Because
static? No!
myClassA.int1 = 22; myClassA.string1 = "BBB...is not
threaded here";
Class1 myClassB = new Class1();
myClassB = Print(myClassA);
int i = myClassB.int1; string s1 = myClassB.string1;
Debug.WriteLine("myClassB is like BBB?: " + i.ToString()
+ "," + s1); //yes, works fine
Debug.WriteLine(".............");
Class1 myClassC = new Class1();
myClassC.int1 = 33; myClassC.string1 =
"CCC..is..threaded!"; //*** //??!! not captured despite being before
t.Start?!
Thread tt = new Thread(delegate() { Print2(myClassC); });
tt.Start(); //no outer variable capture since not
static!? No. Same as before--why?
myClassC.int1 = 333; myClassC.string1 = "DDD is not
threaded here?! compare with above";
Print2(myClassC);
static Class1 Print(object messageObject)
//static version of function Print, but makes no difference if not
static
{
Class1 returnCl1 = (Class1)messageObject;
Debug.WriteLine("inside Print now");
Debug.WriteLine("int, string are: " +
returnCl1.int1.ToString() + ", " + returnCl1.string1);
return returnCl1;
}
Class1 Print2(object messageObject)
{
Class1 returnCl1 = (Class1)messageObject;
Debug.WriteLine("inside Print2 now");
Debug.WriteLine("int, string are: " +
returnCl1.int1.ToString() + ", " + returnCl1.string1);
return returnCl1;
}
public class Class1
{
public string string1;
public int int1;
public Class1()
{
}
}
One or both of these threads should be outputting the AAA and CCC
strings, but they are not. see the asterisk //***
RL
/* OUTPUT
inside Print now
inside Print now
int, string are: 22, BBB...is not threaded here
The thread '<No Name>' (0xfe0) has exited with code 0 (0x0).
int, string are: 22, BBB...is not threaded here
myClassB is like BBB?: 22,BBB...is not threaded here
..............
inside Print2 now
int, string are: 333, DDD is not threaded here?! compare with above
inside Print2 now
int, string are: 333, DDD is not threaded here?! compare with above
The thread '<No Name>' (0xe3c) has exited with code 0 (0x0).
* */
private void Function001()
{
Class1 myClassA = new Class1();
myClassA.int1 = 11; myClassA.string1 =
"AAA..is..threaded!"; //*** //??!! not captured despite being before
t.Start?! why?
Thread t = new Thread(delegate() { Print(myClassA); });
t.Start(); ////*** outer-variable capture as per p. 643
Albahari *anyway!* despite being here, upstream of below?! Because
static? No!
myClassA.int1 = 22; myClassA.string1 = "BBB...is not
threaded here";
Class1 myClassB = new Class1();
myClassB = Print(myClassA);
int i = myClassB.int1; string s1 = myClassB.string1;
Debug.WriteLine("myClassB is like BBB?: " + i.ToString()
+ "," + s1); //yes, works fine
Debug.WriteLine(".............");
Class1 myClassC = new Class1();
myClassC.int1 = 33; myClassC.string1 =
"CCC..is..threaded!"; //*** //??!! not captured despite being before
t.Start?!
Thread tt = new Thread(delegate() { Print2(myClassC); });
tt.Start(); //no outer variable capture since not
static!? No. Same as before--why?
myClassC.int1 = 333; myClassC.string1 = "DDD is not
threaded here?! compare with above";
Print2(myClassC);
static Class1 Print(object messageObject)
//static version of function Print, but makes no difference if not
static
{
Class1 returnCl1 = (Class1)messageObject;
Debug.WriteLine("inside Print now");
Debug.WriteLine("int, string are: " +
returnCl1.int1.ToString() + ", " + returnCl1.string1);
return returnCl1;
}
Class1 Print2(object messageObject)
{
Class1 returnCl1 = (Class1)messageObject;
Debug.WriteLine("inside Print2 now");
Debug.WriteLine("int, string are: " +
returnCl1.int1.ToString() + ", " + returnCl1.string1);
return returnCl1;
}
public class Class1
{
public string string1;
public int int1;
public Class1()
{
}
}