Search code examples
c++wxwidgets

fill database table in wxListCtrl using wxThread- i can fill but system is going to hang


Firstly i make a program for showing table in wxListCtrl, it worked but for limited amount of data.. it shows a problem like:- when i execute the program . frame do the visible after some time... but it works then i turns to use wxThread now everthing is going fine, now when i execute the program frame immediately visible because i write Sleep(1000), so it add a line in wxListCtrl one by one , but it is giving unexpected result depend upon how many rows are in database.. my code is:-

# include "thread.h"
# include "login.h"
# include "sql.h"
# include <mysql.h>

class List_Ctrl_Data;

MyThread :: MyThread(login* login_obj)
{
     this->temp = login_obj;
}
void *MyThread :: Entry()
{
    int i=1,j,k=0   ;
    while(i!=100)
    {
            long index=this->temp->data_list_control->InsertItem(i,wxT("amit"));
            for(j=1;j<3;j++)
            {
               this->temp->data_list_control->SetItem(index,j,wxT("pathak"));
            }
            k++;
            if(k==1)
            {
                            k=10;
                       this->Sleep(1000);
            }
            i++;                                                
    }
}

here data_list_control is the object of wxListCtrl , with the help of thread i m filling value inside the wxListCtrl. some people advised me that here u r knocking frame control( wxListCtrl) again and again from thread entry , thats why frame getting hanged you should use wxPost or AddPendingRequest for this, i dont think that it would work, i tried to explain you my prob, still u feel to ask anything , u r welcome.. if you will help me, it would be a lot for me


Solution

  • ********************************SOLUTION IS HERE******************

    i used the code in thread like[ it get a row from database and pass to event] void *MyThread :: Entry()

    {
    
        List_Ctrl_Data obj1 ;
        MYSQL_RES *database_table_data;
        database_table_data=obj1.getdata();
        MYSQL_ROW row;
        while((row=mysql_fetch_row(database_table_data))!=NULL)
            {
    
                wxCommandEvent event( wxEVT_COMMAND_TEXT_UPDATED, 100000 );
                void *row_data;
                row_data=(void *)row;
                event.SetClientData(row_data);
                temp->GetEventHandler()->AddPendingEvent( event );
                this->Sleep(1000);
    
            }
    
    
    }
    

    and for handling this we create a event table and a function to handle this value- void onNumberUpdate(wxCommandEvent& evt); private: DECLARE_EVENT_TABLE() in header file and in cpp file we write

    void login::onNumberUpdate(wxCommandEvent& evt)

    {
    
        int i=0,j;
    
        void* hold_row;
    
        hold_row=(void *)evt.GetClientData();
    
        MYSQL_ROW row;
    
        row=(MYSQL_ROW)hold_row;
    
        //while(row!=NULL)
    
        //{
    
            //wxPuts(wxT("kjhjkh"));
    
            const char* chars1 = row[0];
    
            wxString mystring1(chars1, wxConvUTF8);
    
            long index=data_list_control->InsertItem(this->counter,mystring1);
        this->counter++;
    
            for(j=1;j<3;j++)
    
                {
    
                const char* chars2=row[j];
    
                wxString mystring2(chars2,wxConvUTF8);
    
                data_list_control->SetItem(index,j,mystring2);
                }
    
        //}
    
    }
    

    BEGIN_EVENT_TABLE(login, wxFrame) EVT_COMMAND (100000, wxEVT_COMMAND_TEXT_UPDATED, login::onNumberUpdate)

    END_EVENT_TABLE()

    and finally i got the solution of my problem//////

    www.rohitworld.site90.net OR [email protected]