Jump to content

Creating Threads on Program Start?

- - - - -

  • Please log in to reply
1 reply to this topic

#1
reed

reed

    Newbie

  • Members
  • PipPip
  • 15 posts
I'm a beginner with C#...I have experience with C++, but am completely lost in this C# business...

I'm experimenting with starting some threads that will run in the background. Eventually, I would like to work up to establishing a database connection on start-up along with having a server thread running that will pass data from some clients into the database. The application will have a GUI with all of the features that the client has (and more)...

BUT, right now, I am stuck on actually starting the thread...It's meant to start as soon as the program starts, but isn't executing until AFTER I hit the exit button on the main form...WHY?

Main Program File:

#region Using Declarations


using ADOX;

using System;

using System.Collections.Generic;

using System.Data;

using System.Data.OleDb;

using System.IO;

using System.Linq;

using System.Threading;

using System.Windows.Forms;


#endregion


namespace Trap_App

{

    //Main Class

    public class Program

    {

        /// <summary>

        /// The main entry point for the application.

        /// </summary>

        [STAThread]

        static void Main()

        {

            Application.EnableVisualStyles();

            Application.SetCompatibleTextRenderingDefault(false);

            Application.Run(new Form1());


            //Begin Data Handler

            DataHandler Database = new DataHandler();

            Thread data = new Thread(Database.CreateDB);

            data.Start();


        }

    }

}


Database Handler

#region Using Declarations


using ADOX;

using System;

using System.Data;

using System.Data.OleDb;

using System.IO;

using System.Windows.Forms;


#endregion


namespace Trap_App

{


    class DataHandler

    {

        private const string DB_LOCATION = @".\ATAMaster.accdb";

        private OleDbConnection myconnection;

        private OleDbCommand command;

        private OleDbDataAdapter adapter;

        private DataSet dataset;


        //Constructor

        public DataHandler()

        {

        }


        //Begin Methods


        //This method creates an Access 2002-2003 database at DB_LOCATION

        //params: in

        public void CreateDB()

        {

            try

            {

                if (!File.Exists(DB_LOCATION))

                {

                    Catalog cat = new Catalog();

                    cat.Create("Provider=Microsoft.JET.OLEDB.4.0;" +

                               "Data Source= " + DB_LOCATION + ";" +

                               "Jet OLEDB:Engine Type=5");


                    MessageBox.Show("New Master Database has been created.");

                    cat = null;

                }

            }

            catch (Exception e)

            {

            }

        }

    }

}


Thanks!

EDIT: I feel like I'm completely off base with this code >.>

#2
Roy192

Roy192

    Newbie

  • Members
  • Pip
  • 4 posts
In the main program file, in the static Main method you have three lines of code starting with Application. The third one is the one that runs your form (Application.Run(new Form1());). It will not continue to the next line until it's done running your form (it's done when it's closed). so that's the reason why your thread only starts after you close the form.

To get the desired result, there are three options:
  • Start the thread before calling Application.Run.
  • Start the thread in the constructor of the form.
  • Or handle the Form_Loaded event and start it there.





1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users