I’m just getting started with gadgeteer stuff and I don’t understand how we get our own Main function working without modifying the program.generated.cs file. The main() is located in that file. This auto generated file must be new because the online tutorials, examples and manuals don’t cover it.
I have found out that events wont work if until after program.startup is finished executing.
I ended up putting an endless loop at the end of the Main() method in program.generated.cs to get them to work even though we are not supposed to modify that file.
So my question is how do we hook into the main() method in program.generated.cs to run our own function without modifying the automatically generated program.generated.cs?
Why do you insist on modifying main()? In Gadgeteer, you really never should need to do that. If you have a loop that needs to run for the duration of the program, then you do that by creating a new thread or timer inside of ProgramStarted. See this example:
The samples showing code in ‘main()’ are pure .NETMF applications and while you can definitely due pure .NETMF development on a Gadgeteer board the Gadgeteer approach is slightly different.
In pure .NETMF you would have a main that runs an endless loop, while a Gadgeteer project hides all that and exposes an almost pure event driven interface, that is my view of it, I am not sure what the official view is.
From our perspective, our code in a Gadgeteer project starts life in the ‘ProgramStarted()’ method in the Program.cs file, so you should start putting your code in here. However, and this is very important, you should never block the ProgramStarted() method, it must return.
Typically you would do any or all of the following in ProgramStarted()
Any upfront initialization required for your application
Wire some events up in this method and then let it return
Start a timer and wire an event that is called periodically by the timer
Start a thread which will run your long running process in the background
And of course anything that did not come to mind when I made the above list, as long as you never block ProgramStarted().
I get it now, I just need to start up threads in programstarted() and that’s where I would put my code that would normally be in main.
I see why you can’t block the program.started, when it ends in main() in the hidden program.generated.cs file a “program.run” method occurs next which must start everything up.
that hidden file also holds all the LED,Joystick, or whatever module, declarations. it sort of bothers me that some of that stuff is hidden because I always like to understand how things work. and why we can magically refer to a joystick or LED object that is never declared/initiated anywhere. (until i found it in the hidden program.generated.cs)
@ Victor - I understand your desire to see what is happening underneath the hood.
Fortunately this is actually quite consistent with how the designer works in general, when you build a .NET Windows Forms application something similar happens, code is generated to represent your GUI components also in a .generated.cs/vb file and you implement your logic of the user file. So I take comfort in the fact that it is at least consistent and I know where to find what I want
All the auto generated code is done by the help of a special Visual Studio add-in. It is a great example of what “Visualization & Modeling SDK” can do. Unfortunately Gadgeteer add-in source code is not available, or at least I couldn’t find it. You can find information about the SDK at the following link: