locked
Windows form from a console

    Question

  • hi everyone,
     I've recently started creating my own operating system from a template called Cosmos (some of you may be familiar with it) which gives you the basic starting code. At the minute all that it does is boot on virtual PC and display a message (eg hello thank you for booting ROS). Is it possible to bring in windows forms to the jumble? if you need me too just ask and ill post the code. 
    MelonSponge 
    Friday, September 11, 2009 5:18 PM

Answers

  • Knowing that this code works makes it highly likely that you'll have no trouble creating a form.  First, use Project + Add Reference, select System.Windows.Forms.  I'll use a thread so the rest of your 'operating system's operatipm won't be affected.  Make your code resemble this:

    using System;
    using System.Windows.Forms;
    using System.Threading;

    namespace ConsoleApplication1 {
      class Program {
        static void Main(string[] args) {
          BootUserInterface();
          Console.ReadLine();
        }
        private static void BootUserInterface() {
          Thread t = new Thread(UserInterfaceThread);
          t.IsBackground = true;
          t.SetApartmentState(ApartmentState.STA);
          t.Start();
        }
        private static void UserInterfaceThread(object arg) {
          Form frm = new Form();  // use your own
          Application.Run(frm);
        }
      }
    }


    Hans Passant.
    Saturday, September 12, 2009 9:18 PM

All replies

  • Since Windows Forms is based on the .NET Framework, bringing in Windows Forms will mean bringing in the .NET Framework. This is, most likely, not trivial, especially at the low level at which you are dealing. You can take a look at http://www.mono-project.com/ for an open-source implementation of the .NET Framework, though I won't be able to give you any reasonable guidance as to how to fold Mono into your project.
    Friday, September 11, 2009 5:35 PM
  • The Framework is rather dependent upon the presence of Windows.
    There is far more to .NET than just the Framework Class Library and Visual Studio.
    I am sure that you will want to actually run your applications.

    Mark the best replies as answers. "Fooling computers since 1971."
    Friday, September 11, 2009 5:39 PM
  • heres the code if you need it
    using System;
    using Cosmos.Compiler.Builder;
    namespace CosmosBoot
    {
        class Program
        {
            #region Cosmos Builder logic
            // Most users wont touch this. This will call the Cosmos Build tool
            [STAThread]
            static void Main(string[] args)
            {
                BuildUI.Run();
            }
            #endregion
    
            // Main entry point of the kernel
            public static void Init()
            {
                var xBoot = new Cosmos.Sys.Boot();
                xBoot.Execute();
                Console.Clear();
                Console.WriteLine("Congratulations! You just Booted ROS");
                Console.WriteLine("ROS is in Pre-Alpha Development");
                Console.WriteLine("This is all that ROS is capable of perfroming at the minute");
                Console.WriteLine("Designed and Written by Robert Forrest");
                Console.WriteLine("Built with Cosmos C#");
                Console.WriteLine("Press a key to Continue ROS...");
                Console.WriteLine("");
                Console.WriteLine("Copyright 2009 Robert Forrest");
                Console.Read(); 
    
                Cosmos.Sys.Deboot.ShutDown();
            }
        }
    }
    Saturday, September 12, 2009 7:15 PM
  • At this point in time, it is impossible to write an OS in managed code.

    If have written what I think of an OS, you would already have the expertise to know the answer to your original question is a very probable no. 
    Your program snippet appears to fall under the category of "Shell", not a true OS.

    Mark the best replies as answers. "Fooling computers since 1971."
    Saturday, September 12, 2009 7:41 PM
  • Knowing that this code works makes it highly likely that you'll have no trouble creating a form.  First, use Project + Add Reference, select System.Windows.Forms.  I'll use a thread so the rest of your 'operating system's operatipm won't be affected.  Make your code resemble this:

    using System;
    using System.Windows.Forms;
    using System.Threading;

    namespace ConsoleApplication1 {
      class Program {
        static void Main(string[] args) {
          BootUserInterface();
          Console.ReadLine();
        }
        private static void BootUserInterface() {
          Thread t = new Thread(UserInterfaceThread);
          t.IsBackground = true;
          t.SetApartmentState(ApartmentState.STA);
          t.Start();
        }
        private static void UserInterfaceThread(object arg) {
          Form frm = new Form();  // use your own
          Application.Run(frm);
        }
      }
    }


    Hans Passant.
    Saturday, September 12, 2009 9:18 PM
  • im quite a newbie so could you tell me where to place this code? That would be great!
    Monday, September 14, 2009 3:57 PM
  • Just paste it after your Init() method.  The BootUserInterface() method call should probably go inside Init(), before the Read() call.

    Hans Passant.
    Monday, September 14, 2009 5:22 PM
  • ok, the code has no errors but, it does exactly the same. It boots (on virtual PC)  but it does nothing after that. I'll post the new code:

    using System;
    using System.Windows.Forms;
    using System.Threading;
    using Cosmos.Compiler.Builder;
    namespace CosmosBoot
    {
        class Program
        {
            #region Cosmos Builder logic
            // Most users wont touch this. This will call the Cosmos Build tool
            [STAThread]
            static void Main(string[] args)
            {
                BootUserInterface();
                Console.ReadLine();
                BuildUI.Run();
            }
        
            private static void BootUserInterface() {
          Thread t = new Thread(UserInterfaceThread);
          t.IsBackground = true;
          t.SetApartmentState(ApartmentState.STA);
          t.Start();
        }
            private static void UserInterfaceThread(object arg)
            {
                Form frm = new Form();  // use your own
                Application.Run(frm);
            }
    
            #endregion
    
            // Main entry point of the kernel
            public static void Init()
            {
                var xBoot = new Cosmos.Sys.Boot();
                xBoot.Execute();
                Console.Clear();
                Console.WriteLine("Congratulations! You just Booted ROS");
                Console.WriteLine("ROS is in Pre-Alpha Development");
                Console.WriteLine("This is all that ROS is capable of perfroming at the minute");
                Console.WriteLine("Designed and Written by Robert Forrest");
                Console.WriteLine("Built with Cosmos C#");
                Console.WriteLine("Press a key to Continue ROS...");
                Console.WriteLine("");
                            Console.Read();
    
                Cosmos.Sys.Deboot.ShutDown();
            }
        }
    }
           
      
    soon i'll post a youtube video to show you exactly what happens.
    Tuesday, September 15, 2009 3:15 PM
  • Can you define "does nothing"? 
    What is it supposed to do?

    Your code displays a blank windows form. 
    Check the Taskbar when you run it.

    Mark the best replies as answers. "Fooling computers since 1971."
    Tuesday, September 15, 2009 4:24 PM
  • i mean that the windows form doesnt appear at all
    Tuesday, September 15, 2009 5:52 PM
  • Sounds like a bug in your operating system.  That's one problem with making your own, the burden is on you to debug it.

    Hans Passant.
    Tuesday, September 15, 2009 6:07 PM
  • Might be your bug.
    Create a separate test app to test out nobugz' code sample.
    This is what is working for me.
    I do not have your other code so I commented all of that stuff out.


            static void Main(string[] args)
            {
                BootUserInterface();
                Init();
                Console.ReadLine();
                //BuildUI.Run();

            }

    The rest of my code is what you posted above.
    Check the Taskbar for the new Window.  Mine is not appearing as the 'top' window.

    Mark the best replies as answers. "Fooling computers since 1971."
    Tuesday, September 15, 2009 6:21 PM
  • The code nobugz gave is working for me.

    maybe because i don't use a virtual pc in the settings if i do i end up with the following error

    An unhandled exception of type 'System.Reflection.TargetInvocationException' occurred in mscorlib.dll

    if i run it with QEMU (standard setting) then it will do all i want from it.

    All i did was start a new project with the cosmos template.  all is standard

    this is the code..


    using System;
    using System.Windows.Forms;
    using System.Threading;
    //
    using Cosmos.Compiler.Builder;

    namespace CosmosBoot
    {
        class Program
        {
            #region Cosmos Builder logic
            //  Most users wont touch this. This will call the Cosmos Build tool
            [STAThread]
            static void Main(string[] args)
            {
                BuildUI.Run();
                BootUserInterface();
            }
            #endregion

            //  Main entry point of the kernel
            public static void Init()
            {
                var xBoot = new Cosmos.Sys.Boot();
                xBoot.Execute();
                //  if enabled then you have to close the console window yourself,
                //  but you can read the text, that you want to display, else you don't.
                //Console.Clear();
                //Console.WriteLine("   ");
                //Console.Clear();
                //Console.WriteLine("   ");
                //Console.WriteLine(" Congratulations !! You just booted C# code. ");
                //Console.WriteLine("   ");
                //Console.WriteLine(" to create your own Operating System.");
                //Console.WriteLine("   ");
                //Console.WriteLine(" Edit youre - Program.cs ");
                //Console.WriteLine("   ");
                //Console.WriteLine(" Press a key to shutdown... ");
                ////Console.Read();   
                Cosmos.Sys.Deboot.ShutDown();           
            }
       
            private static void BootUserInterface()
            {
                Thread t = new Thread(UserInterfaceThread);
                t.IsBackground = false ;
                t.SetApartmentState(ApartmentState.STA);
                t.Start();
            }
       
            private static void UserInterfaceThread(object arg)
            {
                Form frm = new _Welcome (); 
                Application.Run(frm);
            }
        }
    }


    if you ever find out how to open a WPF window let me know
    i've bin trying for a while now but cant get it to work

    credit should go to nobugz


    More knowledge, means more problems.
    Tuesday, September 15, 2009 10:07 PM
  • i am such an idiot
    what ive been meaning to say but couldnt think of the word, was that i wanted to add a GUI to the system. So i quickly assumed without much thought that a windows form would work but it obviosly doesnt.
    Sorry for wasting all this time 
    Wednesday, September 16, 2009 4:45 PM
  • Wednesday, September 16, 2009 6:59 PM
  • i am currently going to ditch the idea of a windows form GUI, and am pointing myself towards a console based Interface instead, when you read some of the small print on the cosmos website, it says that GUI is currently not supported.
    Wednesday, September 16, 2009 7:09 PM