Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. CListBox

CListBox

Scheduled Pinned Locked Moved C / C++ / MFC
questionhelp
18 Posts 5 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • C Cedric Moonen

    The simple answer is: you don't. You should never access UI elements from a different thread than the UI thread. If you have to do such a thing, rethink your design. A better option would be to "generate" your data in the thread and then send a user defined message to your window telling it to refresh itself. When receiving the message, your window will then fetch the data and add it to the list control. I think this article[^] will help you a lot for threading.

    Cédric Moonen Software developer
    Charting control [v1.5] OpenGL game tutorial in C++

    D Offline
    D Offline
    Davitor
    wrote on last edited by
    #3

    Ok here i am try to do.i don't know it's a right or not.Code is here

    UINT WorkerThreadProc( LPVOID Param );
    UINT WorkerThreadProc(LPVOID Param)
    {

    CStdioFile file;
    CString readstring;
    

    if(file.Open(log1, CFile::modeRead | CFile::modeNoTruncate))
    {

    CString readstring
    while(file.end)
    {
    int i=0;

    			file47.ReadStringreadstring.Trim());
    			
    			if(readstring.IsEmpty())
    			{
    				
    			break;
    			}
    			else
    			{
    				for(;i<1000;)
    				{
    				m\_listba.InsertString(i,readstring);
    				break;
    				}
    				i++;
    					
    			}
    		
    		}	
    
    
    file.Close();	
    

    }

    return TRUE;

    }

    void CABC::OnBnClickedButton3()
    {

    AfxBeginThread(WorkerThreadProc,NULL,THREAD\_PRIORITY\_NORMAL,0,0,NULL);
    			WaitForSingleObject(WorkerThreadProc, INFINITE);
    

    }

    It's working but when i click on OKButton then it's show Assertion Failed Afxwil2.inl Line 731. Plz help me

    C C 3 Replies Last reply
    0
    • D Davitor

      Ok here i am try to do.i don't know it's a right or not.Code is here

      UINT WorkerThreadProc( LPVOID Param );
      UINT WorkerThreadProc(LPVOID Param)
      {

      CStdioFile file;
      CString readstring;
      

      if(file.Open(log1, CFile::modeRead | CFile::modeNoTruncate))
      {

      CString readstring
      while(file.end)
      {
      int i=0;

      			file47.ReadStringreadstring.Trim());
      			
      			if(readstring.IsEmpty())
      			{
      				
      			break;
      			}
      			else
      			{
      				for(;i<1000;)
      				{
      				m\_listba.InsertString(i,readstring);
      				break;
      				}
      				i++;
      					
      			}
      		
      		}	
      
      
      file.Close();	
      

      }

      return TRUE;

      }

      void CABC::OnBnClickedButton3()
      {

      AfxBeginThread(WorkerThreadProc,NULL,THREAD\_PRIORITY\_NORMAL,0,0,NULL);
      			WaitForSingleObject(WorkerThreadProc, INFINITE);
      

      }

      It's working but when i click on OKButton then it's show Assertion Failed Afxwil2.inl Line 731. Plz help me

      C Offline
      C Offline
      CPallini
      wrote on last edited by
      #4

      That's exactly what Cédric suggested you NOT to do. :rolleyes: :)

      If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
      This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke
      [My articles]

      1 Reply Last reply
      0
      • D Davitor

        Ok here i am try to do.i don't know it's a right or not.Code is here

        UINT WorkerThreadProc( LPVOID Param );
        UINT WorkerThreadProc(LPVOID Param)
        {

        CStdioFile file;
        CString readstring;
        

        if(file.Open(log1, CFile::modeRead | CFile::modeNoTruncate))
        {

        CString readstring
        while(file.end)
        {
        int i=0;

        			file47.ReadStringreadstring.Trim());
        			
        			if(readstring.IsEmpty())
        			{
        				
        			break;
        			}
        			else
        			{
        				for(;i<1000;)
        				{
        				m\_listba.InsertString(i,readstring);
        				break;
        				}
        				i++;
        					
        			}
        		
        		}	
        
        
        file.Close();	
        

        }

        return TRUE;

        }

        void CABC::OnBnClickedButton3()
        {

        AfxBeginThread(WorkerThreadProc,NULL,THREAD\_PRIORITY\_NORMAL,0,0,NULL);
        			WaitForSingleObject(WorkerThreadProc, INFINITE);
        

        }

        It's working but when i click on OKButton then it's show Assertion Failed Afxwil2.inl Line 731. Plz help me

        C Offline
        C Offline
        Cedric Moonen
        wrote on last edited by
        #5

        So, you didn't even read my reply did you ?

        Cédric Moonen Software developer
        Charting control [v1.5] OpenGL game tutorial in C++

        1 Reply Last reply
        0
        • D Davitor

          Ok here i am try to do.i don't know it's a right or not.Code is here

          UINT WorkerThreadProc( LPVOID Param );
          UINT WorkerThreadProc(LPVOID Param)
          {

          CStdioFile file;
          CString readstring;
          

          if(file.Open(log1, CFile::modeRead | CFile::modeNoTruncate))
          {

          CString readstring
          while(file.end)
          {
          int i=0;

          			file47.ReadStringreadstring.Trim());
          			
          			if(readstring.IsEmpty())
          			{
          				
          			break;
          			}
          			else
          			{
          				for(;i<1000;)
          				{
          				m\_listba.InsertString(i,readstring);
          				break;
          				}
          				i++;
          					
          			}
          		
          		}	
          
          
          file.Close();	
          

          }

          return TRUE;

          }

          void CABC::OnBnClickedButton3()
          {

          AfxBeginThread(WorkerThreadProc,NULL,THREAD\_PRIORITY\_NORMAL,0,0,NULL);
          			WaitForSingleObject(WorkerThreadProc, INFINITE);
          

          }

          It's working but when i click on OKButton then it's show Assertion Failed Afxwil2.inl Line 731. Plz help me

          C Offline
          C Offline
          Cedric Moonen
          wrote on last edited by
          #6

          By the way, two remarks: 1) How does this code even compile ? How can you access your m_listba variable from your thread ? Is it a global variable X| ? 2) What's the purpose of doing that in a thread if you call WaitForSingleObject just afterwards anyway ? :doh:

          Cédric Moonen Software developer
          Charting control [v1.5] OpenGL game tutorial in C++

          D 1 Reply Last reply
          0
          • C Cedric Moonen

            By the way, two remarks: 1) How does this code even compile ? How can you access your m_listba variable from your thread ? Is it a global variable X| ? 2) What's the purpose of doing that in a thread if you call WaitForSingleObject just afterwards anyway ? :doh:

            Cédric Moonen Software developer
            Charting control [v1.5] OpenGL game tutorial in C++

            D Offline
            D Offline
            Davitor
            wrote on last edited by
            #7

            yaa m_listba is global variable.Just i try to impliment it from example of Codeproject.

            C C 2 Replies Last reply
            0
            • C Cedric Moonen

              The simple answer is: you don't. You should never access UI elements from a different thread than the UI thread. If you have to do such a thing, rethink your design. A better option would be to "generate" your data in the thread and then send a user defined message to your window telling it to refresh itself. When receiving the message, your window will then fetch the data and add it to the list control. I think this article[^] will help you a lot for threading.

              Cédric Moonen Software developer
              Charting control [v1.5] OpenGL game tutorial in C++

              S Offline
              S Offline
              Sophiya Chen
              wrote on last edited by
              #8

              Cedric Moonen is right,it's better that UI put in the UI thread.generate data in other thread. :-D

              1 Reply Last reply
              0
              • D Davitor

                yaa m_listba is global variable.Just i try to impliment it from example of Codeproject.

                C Offline
                C Offline
                Cedric Moonen
                wrote on last edited by
                #9

                (I'm gonna reply to your original message that you deleted)

                MsmVc wrote:

                Plz help me

                Help yourself and read the article I linked to you. Threading is not a simple subject that can be explained in 3 lines. If you want to use multithreading and understand what you are doing, then you'll need to spend a bit more time than asking a question on a forum.

                MsmVc wrote

                Yes m_listba is global variable.

                That's really ugly, you should make it a member of your dialog class. You can't access it from your thread so why making it a global varialbe ? And by the way, the "m_" suffix is usually applied to class member variables (m for member). If you use it on a global variable, it is totally confusing.

                Cédric Moonen Software developer
                Charting control [v1.5] OpenGL game tutorial in C++

                D 1 Reply Last reply
                0
                • D Davitor

                  yaa m_listba is global variable.Just i try to impliment it from example of Codeproject.

                  C Offline
                  C Offline
                  CPallini
                  wrote on last edited by
                  #10

                  Davitor wrote:

                  yaa m_listba is global

                  To fool the enemy? :-D

                  If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
                  This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke
                  [My articles]

                  1 Reply Last reply
                  0
                  • C Cedric Moonen

                    (I'm gonna reply to your original message that you deleted)

                    MsmVc wrote:

                    Plz help me

                    Help yourself and read the article I linked to you. Threading is not a simple subject that can be explained in 3 lines. If you want to use multithreading and understand what you are doing, then you'll need to spend a bit more time than asking a question on a forum.

                    MsmVc wrote

                    Yes m_listba is global variable.

                    That's really ugly, you should make it a member of your dialog class. You can't access it from your thread so why making it a global varialbe ? And by the way, the "m_" suffix is usually applied to class member variables (m for member). If you use it on a global variable, it is totally confusing.

                    Cédric Moonen Software developer
                    Charting control [v1.5] OpenGL game tutorial in C++

                    D Offline
                    D Offline
                    Davitor
                    wrote on last edited by
                    #11

                    sorry m_listba is member variable..

                    C 1 Reply Last reply
                    0
                    • D Davitor

                      sorry m_listba is member variable..

                      C Offline
                      C Offline
                      Cedric Moonen
                      wrote on last edited by
                      #12

                      Then your code cannot compile (unless I missed something or you didn't post everything). Anyway, we can discuss the whole day about minor details as this, it still won't make your code work because you can't access UI elements (like the list box) from a separate thread. Serioulsy, I have the impression to talk to a wall here :sigh: ... Did you read that damn article finally ?

                      Cédric Moonen Software developer
                      Charting control [v1.5] OpenGL game tutorial in C++

                      D 1 Reply Last reply
                      0
                      • C Cedric Moonen

                        Then your code cannot compile (unless I missed something or you didn't post everything). Anyway, we can discuss the whole day about minor details as this, it still won't make your code work because you can't access UI elements (like the list box) from a separate thread. Serioulsy, I have the impression to talk to a wall here :sigh: ... Did you read that damn article finally ?

                        Cédric Moonen Software developer
                        Charting control [v1.5] OpenGL game tutorial in C++

                        D Offline
                        D Offline
                        Davitor
                        wrote on last edited by
                        #13

                        yaa i read it.My problem is that i have a file which have a somany data and i want to insertdata into ListBox through thread.I am useing thread becouse without thread my Dialog is showing Not responding. I read article which is given by you.But i need some example to do that.Plz help me

                        modified on Friday, March 27, 2009 5:28 AM

                        C C 2 Replies Last reply
                        0
                        • D Davitor

                          yaa i read it.My problem is that i have a file which have a somany data and i want to insertdata into ListBox through thread.I am useing thread becouse without thread my Dialog is showing Not responding. I read article which is given by you.But i need some example to do that.Plz help me

                          modified on Friday, March 27, 2009 5:28 AM

                          C Offline
                          C Offline
                          CPallini
                          wrote on last edited by
                          #14

                          The worker thread shouldn't directly manipulate the GUI. It should instead post a message to the GUI thread, with in turns updates the GUI. It's not difficult, instead of calling InsertString from within the thread, call PostMessage with appropriate data. On receiving the message, the GUI thread will call InsertString. :)

                          If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
                          This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke
                          [My articles]

                          C 1 Reply Last reply
                          0
                          • D Davitor

                            yaa i read it.My problem is that i have a file which have a somany data and i want to insertdata into ListBox through thread.I am useing thread becouse without thread my Dialog is showing Not responding. I read article which is given by you.But i need some example to do that.Plz help me

                            modified on Friday, March 27, 2009 5:28 AM

                            C Offline
                            C Offline
                            Christian Graus
                            wrote on last edited by
                            #15

                            The basic issue is that you're a retard. You've had explained to you why you can't do it, the framework it throwing exceptions, because you can't do it, and you're still trying to do it. Perhaps you should learn VB, or even Logo ?

                            Christian Graus Driven to the arms of OSX by Vista.

                            C 1 Reply Last reply
                            0
                            • C CPallini

                              The worker thread shouldn't directly manipulate the GUI. It should instead post a message to the GUI thread, with in turns updates the GUI. It's not difficult, instead of calling InsertString from within the thread, call PostMessage with appropriate data. On receiving the message, the GUI thread will call InsertString. :)

                              If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
                              This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke
                              [My articles]

                              C Offline
                              C Offline
                              Cedric Moonen
                              wrote on last edited by
                              #16

                              The point is that everything is explained in the article, with an example and everything. And the example is about a list box being "populated" from a worker thread (through a message sent to the UI). So, I think this is a lost cause :sigh:

                              Cédric Moonen Software developer
                              Charting control [v1.5] OpenGL game tutorial in C++

                              D 1 Reply Last reply
                              0
                              • C Christian Graus

                                The basic issue is that you're a retard. You've had explained to you why you can't do it, the framework it throwing exceptions, because you can't do it, and you're still trying to do it. Perhaps you should learn VB, or even Logo ?

                                Christian Graus Driven to the arms of OSX by Vista.

                                C Offline
                                C Offline
                                CPallini
                                wrote on last edited by
                                #17

                                Christian Graus wrote:

                                Perhaps you should learn VB

                                I supposed it was already in the 'Retard's Toolbox'. :-D

                                If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
                                This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke
                                [My articles]

                                1 Reply Last reply
                                0
                                • C Cedric Moonen

                                  The point is that everything is explained in the article, with an example and everything. And the example is about a list box being "populated" from a worker thread (through a message sent to the UI). So, I think this is a lost cause :sigh:

                                  Cédric Moonen Software developer
                                  Charting control [v1.5] OpenGL game tutorial in C++

                                  D Offline
                                  D Offline
                                  Davitor
                                  wrote on last edited by
                                  #18

                                  Thanks solved

                                  1 Reply Last reply
                                  0
                                  Reply
                                  • Reply as topic
                                  Log in to reply
                                  • Oldest to Newest
                                  • Newest to Oldest
                                  • Most Votes


                                  • Login

                                  • Don't have an account? Register

                                  • Login or register to search.
                                  • First post
                                    Last post
                                  0
                                  • Categories
                                  • Recent
                                  • Tags
                                  • Popular
                                  • World
                                  • Users
                                  • Groups