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).
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
Re: Using one thread at a time with CSemaphore problem
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
Re: Using one thread at a time with CSemaphore problem
Originally Posted by VictorN
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.
Re: Using one thread at a time with CSemaphore problem
Originally Posted by eclessiastes
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:
Originally Posted by VictorN
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
Re: Using one thread at a time with CSemaphore problem
Originally Posted by VictorN
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 ?
Re: Using one thread at a time with CSemaphore problem
Originally Posted by eclessiastes
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).
* The Best Reasons to Target Windows 8
Learn some of the best reasons why you should seriously consider bringing your Android mobile development expertise to bear on the Windows 8 platform.