Is this code valid?

Hi, just trying to create some good template code. I was wondering if this is valid?

public partial class Program
    {.   
        void ProgramStarted()
        {
           
            Debug.Print("Program Started");
        }

        public static void Main()
        {
            new Thread(Thread2).Start();
            new Thread(Thread1).Start();
            Thread.Sleep(Timeout.Infinite);
        }
        static void Thread1()
        {

        }
        static void Thread2()
        {

        }

I read that it is bad practice to run code in the program started because it can freeze the board. Will the Main() function run? Is this good template code to start projects on? Im just curious because it didn’t come with a main method, just the program started.

You are mixing Gadgeteer and plain NETMF code.

In the Gadgeteer world the main function is in the generated code file. It will call you ProgramStarted method and then will go to Infinite sleep. So this should work:



public partial class Program
     {.   
         void ProgramStarted()
         {
             new Thread(Thread2).Start();
             new Thread(Thread1).Start();
            
             Debug.Print("Program Started");
         }

     static void Thread1()
         {

         }
         static void Thread2()
         {

         }


1 Like

Sweet, Thanks!

@ MRTFEREN - you should use call stack while stopped by breakpoint in ProgramStarted method to see the call sequence. I think there is another main() method already declared as start point.

Im just trying to get my code setup before the fez arrives

@ MRTFEREN - One other thing you will need to consider is that components added via the designer are members of the Program class, so your static threads will not have easy access to the module instances or the dispatcher. So you need to consider how you will niter act with the modules from the threads as well as ensure you do not experience any race conditions.

If your only concern is moving long running code out of the ProgramStarted, you can definitely start a thread with the thread function being a instance member rather than static.

1 Like

Your code is not valid because I see a dot at second line. :smiley:

1 Like

I can make the thread instance by just making it
void thread1()
instead of
static void thread1()
correct?

@ MRTFEREN - That is correct.

Unbalanced curly brackets too.