Results 1 to 3 of 3

Thread: C# Start Up Events

  1. #1

    Thread Starter
    Frenzied Member IanRyder's Avatar
    Join Date
    Jan 2013
    Location
    Healing, UK
    Posts
    1,232

    C# Start Up Events

    Hi again everyone, been a long time since I have been on here now but hope you are all well and coping with the current COVID issue.

    During this pandemic I have made the transition form VB.NET to C#, however there is one point that is eluding me at the moment….

    At Application Start Up, In VB.NET, you had the ability to code “Application Events” which occurred before any forms were created and in doing so, I could check to make sure that any needed Servers or other Resources were available before actually starting my application. If they were not present then I could cancel the application start up with the relevant notifications to the user.

    However, “Application Events”, in VS2019 for C# are not available, so my question is What is the Equivalent to application level events in C# using VS2019 and where should I be coding this type of checking before application start up?

    Many thanks for any help and take care everyone.

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,297

    Re: C# Start Up Events

    VB.NET provides the Application Framework, which those events are part of. That hides the Main method - the application entry point - from you and generates the code for it automatically. It is that automatically generated code in the Main method that raises those events.

    In C#, you have direct access to the Main method so you can write whatever code you like in it yourself. In the VB code, the Startup event would be raised and then Cancel property of the StartupEventArgs object tested to decide whether to carry on. In C#, you can write your own method that returns a bool, call it and then test the return value to decide whether to carry on to do effectively the same thing. Whatever actions you would perform in a VB Startup event handler, perform in your C# method. Here's a default C# Main method for a .NET Framework WinForms project:
    csharp Code:
    1. using System;
    2. using System.Windows.Forms;
    3.  
    4. namespace WindowsFormsApp1
    5. {
    6.     static class Program
    7.     {
    8.         /// <summary>
    9.         /// The main entry point for the application.
    10.         /// </summary>
    11.         [STAThread]
    12.         static void Main()
    13.         {
    14.             Application.EnableVisualStyles();
    15.             Application.SetCompatibleTextRenderingDefault(false);
    16.             Application.Run(new Form1());
    17.         }
    18.     }
    19. }
    You might change that to this:
    csharp Code:
    1. using System;
    2. using System.Windows.Forms;
    3.  
    4. namespace WindowsFormsApp1
    5. {
    6.     static class Program
    7.     {
    8.         /// <summary>
    9.         /// The main entry point for the application.
    10.         /// </summary>
    11.         [STAThread]
    12.         static void Main()
    13.         {
    14.             Application.EnableVisualStyles();
    15.             Application.SetCompatibleTextRenderingDefault(false);
    16.  
    17.             if (Startup())
    18.             {
    19.                 Application.Run(new Form1());
    20.             }
    21.         }
    22.  
    23.         private static bool Startup()
    24.         {
    25.             try
    26.             {
    27.                 // Startup code here.
    28.  
    29.                 return true;
    30.             }
    31.             catch (Exception e)
    32.             {
    33.                 return false;
    34.             }
    35.         }
    36.     }
    37. }
    In that case, if the Startup method returns true then the app continues as normal, otherwise the Main method completes without ever creating a startup form and the app will exit.

  3. #3

    Thread Starter
    Frenzied Member IanRyder's Avatar
    Join Date
    Jan 2013
    Location
    Healing, UK
    Posts
    1,232

    Re: C# Start Up Events

    Many thanks jmc,

    I had a funny feeling I was missing something fairly obvious. lol...

    Have a good day and stay safe.

    Ian.

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