CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Page 2 of 2 FirstFirst 12
Results 16 to 24 of 24

Thread: Using one thread at a time with CSemaphore problem

  1. #16
    Arjay's Avatar
    Arjay is offline Moderator / EX MS MVP Power Poster
    Join Date
    Aug 2004
    Posts
    13,302

    Re: Using one thread at a time with CSemaphore problem

    Btw, read through some of the articles listed in my subject line. They refer to threads and thread synchronization (and updating the UI thread from worker threads).

  2. #17
    Join Date
    Sep 2014
    Location
    Las Vegas, NV
    Posts
    85

    Re: Using one thread at a time with CSemaphore problem

    Now the problem is that with PostMessage the UI list control is blocking while strings are added. When i add strings to list control from thread it is not blocking

  3. #18
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Hanover Germany
    Posts
    19,733

    Re: Using one thread at a time with CSemaphore problem

    Quote Originally Posted by eclessiastes;2164501...
    And this is how i post message from thread
    [CODE
    CString * s = new CString("listitem");
    AfxGetMainWnd()->PostMessageW(UWM_ADDSTRING, 0, (LPARAM)s);
    [/CODE]
    Everything works ok so far. Is this the correct way to do it (postmessage) ?
    It is almost correct.
    Note, however, that PostMessage could fail. In such a case you would have the memory leak.
    To prevent it just write
    Code:
    CSt!ring * s = new CString("listitem");
    if(!AfxGetMainWnd()->PostMessage(UWM_ADDSTRING, 0, (LPARAM)s))
        delete s;
    Besides, don't mix PostMessageW with CString. Either PostMessage and CString
    or PostMessageW and CStringW
    Victor Nijegorodov

  4. #19
    Join Date
    Sep 2014
    Location
    Las Vegas, NV
    Posts
    85

    Re: Using one thread at a time with CSemaphore problem

    Quote Originally Posted by VictorN View Post
    It is almost correct.
    Note, however, that PostMessage could fail. In such a case you would have the memory leak.
    To prevent it just write
    Code:
    CSt!ring * s = new CString("listitem");
    if(!AfxGetMainWnd()->PostMessage(UWM_ADDSTRING, 0, (LPARAM)s))
        delete s;
    Besides, don't mix PostMessageW with CString. Either PostMessage and CString
    or PostMessageW and CStringW
    if i add delete s; i get this error
    Unhandled exception at 0x00355D38 in MFCApplication1.exe: 0xC0000005: Access violation reading location 0xFEEEFEEA.
    Code:
    for (int i = 1; i < 10000;++i)
    	{
    	CStringW * s = new CStringW("listitem");
    	AfxGetMainWnd()->PostMessageW(UWM_ADDSTRING, 0, (LPARAM)s);
    	delete s;
    	}
    Without delete s; it works fine but it blocks the UI while adding strings, unlike adding strings to list control from thread which doesn't block UI. I was thinking to postmessage to a specific thread that is inserting items to list control but it is said its not thread safe. I'm trying to figure out how to insert items to list control without a thread and without blocking the list control UI
    Last edited by eclessiastes; October 8th, 2014 at 02:30 PM.

  5. #20
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Hanover Germany
    Posts
    19,733

    Re: Using one thread at a time with CSemaphore problem

    Quote Originally Posted by eclessiastes View Post
    if i add delete s; i get this error
    Unhandled exception at 0x00355D38 in MFCApplication1.exe: 0xC0000005: Access violation reading location 0xFEEEFEEA.
    Code:
    for (int i = 1; i < 10000;++i)
    	{
    	CStringW * s = new CStringW("listitem");
    	AfxGetMainWnd()->PostMessageW(UWM_ADDSTRING, 0, (LPARAM)s);
    	delete s;
    	}
    Without delete s; it works fine
    Err...!!!!

    Please, compare your "code" with mine:
    Quote Originally Posted by VictorN View Post
    It is almost correct.
    Code:
    CSt!ring * s = new CString("listitem");
    if(!AfxGetMainWnd()->PostMessage(UWM_ADDSTRING, 0, (LPARAM)s))
        delete s;
    Besides, don't mix PostMessageW with CString. Either PostMessage and CString
    or PostMessageW and CStringW
    Don't you see a difference?
    Victor Nijegorodov

  6. #21
    Join Date
    Sep 2014
    Location
    Las Vegas, NV
    Posts
    85

    Re: Using one thread at a time with CSemaphore problem

    Quote Originally Posted by VictorN View Post
    Err...!!!!

    Please, compare your "code" with mine:

    Don't you see a difference?
    I modified and works smoothly now but it freeze the app interface while items are added to list control.
    Code:
    LRESULT CMFCApplication1Dlg::OnAddString(WPARAM, LPARAM lParam)
    {
    	CString * s = (CString *)lParam;
    	listct1.InsertItem(0,(*s));
    	delete s;
    	return 0;
    }
    
    UINT CMFCApplication1Dlg::Test(LPVOID param)
    {
    	THREADSTRUCT*    ts = (THREADSTRUCT*)param;
    	ts->dlsg->progresst.SetRange32(0, 15000);
    	for (unsigned i = 1; i < 15000;++i)
    	{
    		ts->dlsg->progresst.SetStep(1);
    		ts->dlsg->progresst.StepIt();
    		CString * s = new CString("item list");
    		if (!AfxGetMainWnd()->PostMessage(UWM_ADDSTRING, 0, (LPARAM)s))
    			delete s;
    	}
    	return 0;
    }
    The above way the user interface is freeze while adding to list control.
    But this way it doesn't
    Code:
    UINT CMFCApplication1Dlg::Test(LPVOID param)
    {
    	THREADSTRUCT*    ts = (THREADSTRUCT*)param;
    	ts->dlsg->progresst.SetRange32(0, 15000);
    	for (unsigned i = 1; i < 15000;++i)
    	{
    		ts->dlsg->progresst.SetStep(1);
    		ts->dlsg->progresst.StepIt();
    		ts->dlsg->listct1.InsertItem(i, L"item list");
    	}
    	return 0;
    }
    The only way to make it work without freeze the UI is using threads to insert to list control but it is said its not thread safe. How can i add to list control using PostMessage without blocking the user interface ?

  7. #22
    Arjay's Avatar
    Arjay is offline Moderator / EX MS MVP Power Poster
    Join Date
    Aug 2004
    Posts
    13,302

    Re: Using one thread at a time with CSemaphore problem

    Quote Originally Posted by eclessiastes View Post
    How can i add to list control using PostMessage without blocking the user interface ?
    You can minimize the effect of the blocking by pumping messages inside your OnAddString handler. (search this forum for an PumpMessage example method).

    Another way to do this is to use the list control in virtual mode. See my articles for an example of using a virtual list control (that gets populated by a worker thread).

  8. #23
    Join Date
    Nov 2000
    Location
    Voronezh, Russia
    Posts
    6,607

    Re: Using one thread at a time with CSemaphore problem

    Quote Originally Posted by eclessiastes View Post
    How can i add to list control using PostMessage without blocking the user interface ?
    See sample attached. You should not be afraid of using SendMessage. Run and see it working just fine.
    Attached Files Attached Files
    Best regards,
    Igor

  9. #24
    Join Date
    Jun 2002
    Location
    Stockholm, Sweden
    Posts
    1,641

    Re: Using one thread at a time with CSemaphore problem

    If the amount of items that you plan to add isn't huge, also consider this as an alternative:

    http://stackoverflow.com/questions/1...tctrl-in-c-mfc
    Nobody cares how it works as long as it works

Page 2 of 2 FirstFirst 12

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Windows Mobile Development Center


Click Here to Expand Forum to Full Width




On-Demand Webinars (sponsored)