creating global objects and threads.......... RRS feed

  • Question

  • Hello everybody,

    I m using a class named "general" to fill data in a static dataset named DS using a function named getdata. This dataset(DS) is static and in the getdata function i m inserting new tables in the DS by passing query and datatable name with primary field column to the function. The general class structure is :

    class general
    public static OleDbConnection con = new OleDbConnection();
    public OleDbCommand com = new OleDbCommand();
    public static DataSet DS = new DataSet();
    public OleDbDataAdapter DA;
    public OleDbCommandBuilder myCommandBuilder;
    public DataColumn[] columns;

    public void getdata(string query, string table, string prime)
    if (general.con.State == ConnectionState.Closed)
    general.con.ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + Application.StartupPath + "\\sweets.mdb";

    columns = new DataColumn[1];
    com.Connection = general.con;
    com.CommandText = query;
    DA = new OleDbDataAdapter(com);
    DA.Fill(general.DS, table);
    columns[0] = general.DS.Tables[table].Columns[prime];
    general.DS.Tables[table].PrimaryKey = columns;
    myCommandBuilder = new OleDbCommandBuilder(DA);

    catch (Exception ex)
    MessageBox.Show("Error while database handling.. " + ex.Message);


    above piece of code is working nicely. Now i want that when the main form is loading(the first main page), i start threads like this :

    Thread attthread = new Thread(new ThreadStart(attfunction));
    //many more threads declaration here

    and the attfunction is like this :

    public void attfunction()
    general attendance;
    string str1;
    attendance = new general();
    str1 = "SELECT * FROM attendance";
    attendance.getdata(str1, "attendance", "ID");


    //more function definitions here

    and then to start threads like this :

    // more thread starts here

    By using these threads, i want to fill data in dataset(DS) so that when the user open any form where i show datagridview, the data is already available and i bind the datagridview at form load so that the project works fastly and user dont have to wait when he open any form for data to be inserted into the dataset.

    My problem is that as i creating and running threads at main form, i cant access these threads at any other windows forms and as i m creating object in function invoked by threads also at main form, i cant use these objects at any other windows form. So i cant use the dataadapter.update() mehod to set the chages in datasourse and also i cant assess the status of the threads whether they have completed or not.

    In other words, i can use dataset(DS) at all the forms bcs its static but i cant use the threads and objects of the general class at forms others than main form, can anyone tell me how i can do this???

    Thanx & Regards,
    Tuesday, August 17, 2010 3:57 PM


  • hello Ricky,


    First thing i want to tell is that you can't access any member of forms as they are private moreever they are instance member to access those member you need to make them static then they can be accessible by Form.Varible


    I hope this will help you to build up your methods as it.

    Thanks and Regards, Shailesh B. Davara
    Thursday, August 19, 2010 11:49 AM