Hi everyone,

I have an app that needs to get a chunky amount of data from an Access mdb file... This in itself is no problem, I've done it often... The problem is, that this Access database is horribly slow (as you might imagine) and during the looping of the data, my app appears to the user as "Not responding" ...

What I'd like is to have a thread handle the connection to the database and the handling of the data, while triggering events to increment the number of rows processed, which would then be displayed on my GUI...

How would a thread be implemented when dealing with a database connection?

The code below is what I'm working on... Obviously, the count variable currently doesn't show up until everything is done, which kind of defeats its purpose...

GUI class
Code:
public partial class Main : Form
    {
        private Driver driver = Driver.Instance;
        private ArrayList mainData = new ArrayList();
        private ArrayList dispoData = new ArrayList();
        private int count = 0;

        public Main()
        {
            InitializeComponent();
            Driver.CountChangedHandler myCountChangedHandler = Increment;
            driver.CountChanged += myCountChangedHandler;
            gridDispo.AutoGenerateColumns = false;
            gridMain.AutoGenerateColumns = false;
        }

        private void Increment(object sender)
        {
            count++;
            txtCount.Text = count.ToString();
        }

        private void btnExtract_Click(object sender, EventArgs e)
        {
            try
            {
                mainData = driver.GetMainData();
                gridMain.DataSource = mainData;
                dispoData = driver.GetDispoData();
                gridDispo.DataSource = dispoData;
            }
            catch
            {
            }
        }
Driver class (should be called DAO really, but you get the idea)
Code:
    class Driver
    {
        private static Driver instance;
        private String ConnectionString = "";

        //Set up delegates
        public delegate void CountChangedHandler(object sender);
        public event CountChangedHandler CountChanged;
        //Set up events


        private Driver()
        {
            ConnectionString = this.ReadSettings("\\settings.ini", Environment.CurrentDirectory, "Db");
        }

        public void OnCountChanged()
        {
            CountChanged(this);
        }

        /// <summary>
        /// Returns an instance of this class.
        /// Creates a new instance if none exists.
        /// </summary>
        public static Driver Instance
        {
            get
            {
                if (instance == null)
                    instance = new Driver();
                return instance;
            }
        }

        public ArrayList GetMainData()
        {
            using (OleDbConnection connection = new OleDbConnection(ConnectionString))
            {
                String QueryString = "SELECT * FROM Main";
                using (OleDbCommand cmd = new OleDbCommand(QueryString, connection))
                {
                    ArrayList result = new ArrayList();
                    try
                    {
                        cmd.Connection.Open();
                        OleDbDataReader reader = cmd.ExecuteReader();
                        while (reader.Read())
                        {
                            object name = reader["Driver"];
                            result.Add(new TruckDriver(name.ToString()));
                            this.OnCountChanged();
                        }
                        return result;
                    }
                    catch
                    {
                        return null;
                    }
                }
            }
        }
        public ArrayList GetDispoData()
        {
            using (OleDbConnection connection = new OleDbConnection(ConnectionString))
            {
                String QueryString = "SELECT * FROM [Disponering-SAL-Move]";
                using (OleDbCommand cmd = new OleDbCommand(QueryString, connection))
                {
                    ArrayList result = new ArrayList();
                    try
                    {
                        cmd.Connection.Open();
                        OleDbDataReader reader = cmd.ExecuteReader();
                        while (reader.Read())
                        {
                            object id = reader["ID"];
                            object chassis = reader["Chassisnr"];
                            object kørsel = reader["Kørselsdato"];
                            object model = reader["Model"];
                            object postnr = reader["HPostnummer"];
                            result.Add(new Disponering_SAL_Move(Convert.ToInt32(id), chassis.ToString(), kørsel.ToString(), model.ToString(), Convert.ToInt32(postnr)));
                            this.OnCountChanged();
                        }
                        return result;
                    }
                    catch
                    {
                        return null;
                    }
                }
            }
        }