CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 6 of 6
  1. #1
    Join Date
    Sep 2007
    Posts
    5

    Main Method, Threading & GUIs

    Good Morning Folks,

    I am an experienced programmer in a handful of languages, however I have no experience in C# (little in C, but only an introduction course at Uni). At the moment my main development language is Java, so I'm trying to understand how to use familiar concepts in C#. I am using Visual C# as part of Visual Studio Pro 2005.

    The Problem

    At the moment I cannot seem to create application that does not instantiate a GUI from the main method. When I create a new project in VS it gives me a Form1, and the main method uses Application.Run to instantiate.

    However, I would *NEVER* drive the core processing of an application from a GUI in JAVA (or any other language, for that matter). So I want to create a class, called Core, which performs the bulk of computation. The GUI thread will then use the methods of Core.

    (Core will also eventally include networking, DB access, etc. methods)

    I have the generated Program.cs, which I would like to instantiate Core within the main method. During the instantiation of Core, it would then instantiate Form1.

    The idea is that the process will not terminate because the process for Form1 is still running. However when I debug the application it launches Form1 (for a split second) before I get the console message "The program '[3476] WindowsApplication1.vshost.exe: Managed' has exited with code 0 (0x0)."

    Any thoughts? Many Thanks!

  2. #2
    Join Date
    Nov 2004
    Posts
    105

    Re: Main Method, Threading & GUIs

    You can Instantiatea class object instead of from, In application.Run(..);

    something like this
    Code:
    static class Program
        {
            /// <summary>
            /// The main entry point for the application.
            /// </summary>
            [STAThread]
            static void Main()
            {
                Application.EnableVisualStyles();
                Application.SetCompatibleTextRenderingDefault(false);
                clsSAAMInit objInit = new clsInit();
                objInit.InitForm();
            }
        }
    In InitFrom() method you can invoke form or what ever you want to do.
    Code:
    class clsSAAMInit
    {
           private static Form1 m_Frm;// = new Form1();
           public void InitForm()
           {
                 m_Frm = new Form1();
                 Application.Run((Form)m_Frm);
           }
    }

    Regards
    Ravi.Battula

  3. #3
    Join Date
    Sep 2007
    Posts
    5

    Re: Main Method, Threading & GUIs

    Many thanks Ravi, I feel like i'm slowly but surely getting somewhere... but it is, unfortunately, time for the next problem:

    Code:
    static class Program
        {
            [STAThread]
            static void Main()
            {
                Application.EnableVisualStyles();
                Application.SetCompatibleTextRenderingDefault(false);
                Program_Manager objInit = new Program_Manager();
                objInit.InitForm();
            }
    
        }
    Code:
    class Program_Manager
        {
            
            private static Form1 m_Frm;
    
            public void InitForm()
            {
                m_Frm = new Form1();
                System.Console.WriteLine("showing form...");
                Application.Run((Form)m_Frm);
                System.Console.WriteLine("Form loaded... continuing main thread");
            }
        }
    Gives the console output:

    Code:
    'WindowsApplication1.vshost.exe' (Managed): Loaded 'C:\WINDOWS\assembly\GAC_32\mscorlib\2.0.0.0__b77a5c561934e089\mscorlib.dll', Skipped loading symbols. Module is optimized and the debugger option 'Just My Code' is enabled.
    'WindowsApplication1.vshost.exe' (Managed): Loaded 'C:\WINDOWS\assembly\GAC_MSIL\Microsoft.VisualStudio.HostingProcess.Utilities\8.0.0.0__b03f5f7f11d50a3a\Microsoft.VisualStudio.HostingProcess.Utilities.dll', Skipped loading symbols. Module is optimized and the debugger option 'Just My Code' is enabled.
    'WindowsApplication1.vshost.exe' (Managed): Loaded 'C:\WINDOWS\assembly\GAC_MSIL\System.Windows.Forms\2.0.0.0__b77a5c561934e089\System.Windows.Forms.dll', Skipped loading symbols. Module is optimized and the debugger option 'Just My Code' is enabled.
    'WindowsApplication1.vshost.exe' (Managed): Loaded 'C:\WINDOWS\assembly\GAC_MSIL\System\2.0.0.0__b77a5c561934e089\System.dll', Skipped loading symbols. Module is optimized and the debugger option 'Just My Code' is enabled.
    'WindowsApplication1.vshost.exe' (Managed): Loaded 'C:\WINDOWS\assembly\GAC_MSIL\System.Drawing\2.0.0.0__b03f5f7f11d50a3a\System.Drawing.dll', Skipped loading symbols. Module is optimized and the debugger option 'Just My Code' is enabled.
    'WindowsApplication1.vshost.exe' (Managed): Loaded 'C:\WINDOWS\assembly\GAC_MSIL\Microsoft.VisualStudio.HostingProcess.Utilities.Sync\8.0.0.0__b03f5f7f11d50a3a\Microsoft.VisualStudio.HostingProcess.Utilities.Sync.dll', Skipped loading symbols. Module is optimized and the debugger option 'Just My Code' is enabled.
    'WindowsApplication1.vshost.exe' (Managed): Loaded 'C:\Documents and Settings\Jamie W. Astin\My Documents\Visual Studio 2005\Projects\WindowsApplication1\WindowsApplication1\bin\Debug\WindowsApplication1.vshost.exe', Skipped loading symbols. Module is optimized and the debugger option 'Just My Code' is enabled.
    'WindowsApplication1.vshost.exe' (Managed): Loaded 'C:\WINDOWS\assembly\GAC_32\System.Data\2.0.0.0__b77a5c561934e089\System.Data.dll', Skipped loading symbols. Module is optimized and the debugger option 'Just My Code' is enabled.
    'WindowsApplication1.vshost.exe' (Managed): Loaded 'C:\WINDOWS\assembly\GAC_MSIL\System.Deployment\2.0.0.0__b03f5f7f11d50a3a\System.Deployment.dll', Skipped loading symbols. Module is optimized and the debugger option 'Just My Code' is enabled.
    'WindowsApplication1.vshost.exe' (Managed): Loaded 'C:\WINDOWS\assembly\GAC_MSIL\System.Xml\2.0.0.0__b77a5c561934e089\System.Xml.dll', Skipped loading symbols. Module is optimized and the debugger option 'Just My Code' is enabled.
    The thread 0xe5c has exited with code 0 (0x0).
    The thread 0xb08 has exited with code 0 (0x0).
    'WindowsApplication1.vshost.exe' (Managed): Loaded 'C:\Documents and Settings\Jamie W. Astin\My Documents\Visual Studio 2005\Projects\WindowsApplication1\WindowsApplication1\bin\Debug\WindowsApplication1.exe', Symbols loaded.
    showing form...
    Until I close the form, when it then says:

    Code:
    Form loaded... continuing main thread
    I would have expected (from JAVA background) for the form thread to start and allow computation in the Program_Manager to continue?

    Thoughts?

  4. #4
    Join Date
    Sep 2007
    Posts
    5

    Re: Main Method, Threading & GUIs

    Aha!

    Code:
    class Program_Manager
        {
            
            private static Form1 m_Frm;
    
            public void InitForm()
            {
                m_Frm = new Form1();
                System.Console.WriteLine("showing form...");
                Thread t = new Thread(startGUI);
                t.Start();
                System.Console.WriteLine("Form loaded... continuing main thread");
            }
    
            public void startGUI()
            {
                Application.Run((Form)m_Frm);
            }
        }
    Seems to have cracked it! Thanks for your help Ravi! I'm well on my way now...

  5. #5
    Join Date
    Jan 2002
    Location
    Scaro, UK
    Posts
    5,940

    Re: Main Method, Threading & GUIs

    That's a slightly strange way of doing things... in C#-land you usually have your form on your main thread and create other threads to do work.

    Then you have your UI driving your work code, not your work code driving your UI...

    Darwen.
    www.pinvoker.com - PInvoker - the .NET PInvoke Interface Exporter for C++ Dlls.

  6. #6
    Join Date
    May 2007
    Posts
    1,546

    Re: Main Method, Threading & GUIs

    Do it the standard C# way. You can write all your logic in a separate assembly and then reference that assembly in your GUI. Your separate assembly will contain all your business logic while your GUI just does the display. There's no need to go doing all that crazy threading stuff.

    When your first form instantiates, it starts what is basically a while(true) loop which is ended when the form closes. This is why control doesn't continue when you instantiate the form.
    www.monotorrent.com For all your .NET bittorrent needs

    NOTE: My code snippets are just snippets. They demonstrate an idea which can be adapted by you to solve your problem. They are not 100% complete and fully functional solutions equipped with error handling.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured