Click to See Complete Forum and Search --> : Really, Really Need Help on Threads
Jackal
June 21st, 1999, 07:39 AM
Oka I really need some help...maybe I overlooked something. I can display values to an edit box from threads...8 of them to be exact. The problem is that when I call the function to display the values to the edit boxes it places them in the correct edit box for all of a couple of seconds and then randomly places them in another edit box.
My display command is the following:
switch (x)
{
case 0x41: //Session A//
(CEdit*)GetDlgItem(IDC_EDITA);
break;
case 0x42: //Session B//
(CEdit*)GetDlgItem(IDC_EDITA);
break;
}
And I do this for all the sessions 'A' - 'H
Thanks in advance.
Jackal
ric
June 21st, 1999, 07:51 AM
If you really, really need help, then explain your problem better. I do not understood what you are doing and what is your question. You have 8 threads or 8 edit boxes??! And what is this switch supposed to do. As I see it it does actually nothing.
Put a better explanation to your question if you really need help.
Jackal
June 21st, 1999, 08:14 AM
Okay, I have 8 threads one thread for each sessions that I need open. When I open each session I obtain an ID from it. I then need to display each ID in its corresponding edit box.
CMyDialog::InitDialog()
{
AfxBeginThread(SESSIONS,(CEdit*)GetDlgItem(IDC_EDITA));
AfxBeginThread(SESSIONS,(CEdit*)GetDlgItem(IDC_EDITB));
AfxBeginThread(SESSIONS,(CEdit*)GetDlgItem(IDC_EDITC));
AfxBeginThread(SESSIONS,(CEdit*)GetDlgItem(IDC_EDITD));
AfxBeginThread(SESSIONS,(CEdit*)GetDlgItem(IDC_EDITE));
AfxBeginThread(SESSIONS,(CEdit*)GetDlgItem(IDC_EDITF));
AfxBeginThread(SESSIONS,(CEdit*)GetDlgItem(IDC_EDITG));
AfxBeginThread(SESSIONS,(CEdit*)GetDlgItem(IDC_EDITH));
return TRUE;
}
//////////////////////////////////////////////////
I Open 8 Threads
//////////////////////////////////////////////////
Now I need to display them in the correct edit boxes. You know Thread 'A' with Edit box 'A' and so on.
The switch was my error disregard it.
(minus pts for me)
UINT SESSIONS (LPVOID PHWndView)
{
do
{
for(x = 0x41; x<0x49; x++)
{
session.function1();
err_code = session.function2(x);
if(err_code == 0 )
{
session.function3();
session.function4(screen_buffer);
session.function5(screen_buffer);
((CEdit*)PHWndView)->SetWindowText(luid);
}
}
}while(0==0);
}
Does explain it a little better.
ric
June 21st, 1999, 08:32 AM
OK,
this is a little better,
first something not on the threads -- I think it is good to call CDialog::InitDialog();
second -- on threads
as I see you have nice variables in your SESSION function, but as I see they are not declared in it, but somewhere else. So all your each of your threads does not create them, but use them. So there are 4 varialbles used by 8 threads in the same way. Wow! I do not may be I do not understand this threads matter very well, but as far as I understand, I nowhere saw a piece of SYNCRONIZATION. See the reference help of:
CMutex, CCriticalSection, ::WaitForSingleObject()
this may help you to put a little bit of syncronization in your code.
I think you should pay a great attention on syncronization with your 8 threads. One easy solution I see is to make all the variable you can local to your thread function. Thus, each thread will have his own data and will not overwrite other threads' data.
Regards,
ric
Jackal
June 21st, 1999, 09:10 AM
Okay is it possible to display these variables in the edit boxes without using CMutex, CCriticalSection, ::WaitForSingleObject()
Point being can one do it without using Syncronization.
Question is it possible to open 1 thread and run it through a for loop that runs through each of the 8 sessions and can display the IDs to the correct edit boxes. Do every thing that i have been trying to do with 8 threads but with 1 thread.
regards
Jackal
Jerry Coffin
June 21st, 1999, 12:11 PM
It looks to me like your fundamental design should be changed -- if at all possible, you should restrict all direct access to the UI to ONE thread. It's generally best to have other threads send messages to the UI thread, and let it actually update the display.
The universe is a figment of its own imagination.
Gordito Supreme
June 21st, 1999, 05:32 PM
The way I update editboxes in the dialog window from a thread ... is to post a windows message to the hwnd of the dialog from the thread, and the appropriate handling function in the dialog class updates the appropriate edit box.
Gordito Supreme
ric
June 22nd, 1999, 04:38 AM
You can do it if you declare local variables to your thread function. This way each thread will have his own variables and only he will write to them. So I think the solution is for each edit box - a thread, and for each thread - a set variables. Like:
UINT ThreadProc(LPVOID pParam)
{
int value;
CString str;
for(int x=0;x<8;x++);
return 0;
}
But if you need to use the same variables for all the threads, this program cries for a very good syncronization. This is not so complex so that you are trying to pass without it. Example:
CCriticalSection g_cs;
CString text;
UINT ThreadProc(LPVOID pParam)
{
for(int x=0;x<8;x++)
{
cs.Lock();
text = "new text";
cs.UnLock();
pDlg->SetDlgItemText(IDC_EDIT,text);
}
return 0;
}
Regards,
ric
codeguru.com
Copyright Internet.com Inc., All Rights Reserved.