Click here to Skip to main content
15,887,135 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
After years without coding I restart programming c# for my own, starting with a XAML.Forms Cross-Platform-App. C# is new for me, but with C and C++ I have years of experience.

What me worries is not coding and testing my code, it's the unusual and unexpected behavior of my environment. After having the same problem with my more complex code, I reduced the problem to very simple stuff like this.
C#
public class Tester
    {
        public static Tester _ttester = null;
        private static bool finit = false;

        public Tester()
        {   
            if (!finit)             // first time?
            {
                finit = true;       // first round done
                _ttester = new Tester();   // initiate a special global object for general use 
            }
        }
    }

With calling the code like this
C#
static Tester tst = null;
tst = new Tester()

at the beginning of my App code.
The app hangs with in "Tester" constructor never terminating.

What I have tried:

If I am using the debugger, setting a Breakpoint at the beginning of my Tester Constructor, I notice that I am not able to view "finit" local static var. "Expression can not be evaluated". And within the program it seems to be the same. With "new Tester" the constructor is called recursively. But the "finit" Member should protect the second Tester calling a third one. But the debugger shows it does not happen like that. The "Tester" constructor is called again and again without ever terminating. May be a very clever optimizer is killing my coding. I do not have any idea and need some help.

Making the same in a small "consoleApp" anything works as expected. But in my more complex Cross-Platform Environment it does not work. May be because of having divided the App in two parts. The general App part and the platform specific code. In former times I could look at the machine code and see what the compiler has generated. I did not found a way to see something like that today.
Thanks in advance.
Posted
Updated 19-Jun-18 3:58am

I do not know about your particular issue....
But you could use static constructor to run that code only once perhaps?
Not sure it's a good idea to call class constructor in a static constructor... but anyway, here is how

Solution 1
C#
public class Tester
{
  readonly static Tester cTest = new Tester();
}


Solution 2
C#
public class Tester
{
  readonly static Tester cTest;

  static Tester()
  {
    cTest = new Tester();
  }
}
 
Share this answer
 
Thank you for your suggestion. I really did it with a static constructor. But because egualy not working I used a "normal" constructor to keep things as "normal" as possible.

After sleeping one night having my computer in sleep mode the following morning every thing works like expected. I only gave the Tester constructor a parameter of type (int i) and changed the calls according to this. I wanted to see if I could view into this type of expression while debugging. But after this little change, anything was fine. The rebuilding could not have changed much, because I have made a lot of little changes ever to get some solution.

After that I removed the additional parameter, added in the morning, it works fine.

I really have no idea what happened. Today, while anything works automatically, its complicated to fix such problems.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900