Click here to Skip to main content
15,905,508 members
Home / Discussions / C / C++ / MFC
   

C / C++ / MFC

 
AnswerRe: sort structures Pin
Chris Losinger29-Oct-05 12:03
professionalChris Losinger29-Oct-05 12:03 
AnswerRe: sort structures Pin
Joe Woodbury29-Oct-05 13:11
professionalJoe Woodbury29-Oct-05 13:11 
AnswerRe: sort structures Pin
Ghasrfakhri29-Oct-05 19:21
Ghasrfakhri29-Oct-05 19:21 
Questioncant signin to homtmail using Pin
whatever8929-Oct-05 9:29
whatever8929-Oct-05 9:29 
Questionfile system type Pin
Chintoo72329-Oct-05 7:26
Chintoo72329-Oct-05 7:26 
AnswerRe: file system type Pin
Mircea Puiu29-Oct-05 7:45
Mircea Puiu29-Oct-05 7:45 
Questiontooltip for CDC drawing areas ?? Pin
tbrake29-Oct-05 6:15
tbrake29-Oct-05 6:15 
Questionmulti thread sinz Pin
jojojojoj29-Oct-05 3:51
jojojojoj29-Oct-05 3:51 
hello list, i am having some trouble with thread sinc in a multithread program i am developing.

the problems is i dont know how many threads i will have in my program, here is some code:

void CAsyncClientDlg::CleanupThread()
{
CString n;
GetDlgItemText(IDC_SIMULTANEOS, n);
int a = _ttoi(n.GetBuffer(n.GetLength()));


for(LONG i=0; i<a; i++)="" {


="" if="" (m_pclientthread[i])
="" {=""
="" (::postthreadmessage(m_pclientthread[i]-="">m_nThreadID, WM_TERM_THREAD, 0, 0) == 0)
TRACE(_TEXT("Thread 0x%02x possibly already terminated\n"), m_pClientThread[i]->m_nThreadID);

// wait up to 1s for secondary threads to terminate
// termEvent will be signaled when thread count reaches 0
if (termEvent.Lock(1000))
TRACE(_TEXT("Threads terminated gracefully\n"));
else
TRACE(_TEXT("WARNING: All secondary thread(s) not gracefully terminated.\n"));
}


}
}

void CAsyncClientDlg::OnConnect()
{
CString str;
GetDlgItemText(IDC_SERVERNAME, str);
CString n;
GetDlgItemText(IDC_SIMULTANEOS, n);
int a = _ttoi(n.GetBuffer(n.GetLength()));
// validate that the number is <= MAX_NUMBER_OF_THREADS
for(LONG variable=0; variable<a; variable++)="" {
=""

="" create="" a="" thread="" to="" handle="" the="" connection.="" is="" created="" suspended="" so="" that="" we="" can
="" set="" variables="" in="" cclientthread="" before="" it="" starts="" executing.
="" cclientthread*="" pthread="(CClientThread*)AfxBeginThread(RUNTIME_CLASS(CClientThread)," thread_priority_normal,="" 0,="" create_suspended);
="" if="" (!pthread)
="" setdlgitemtext(idc_connectionstatus,="" cstring("could="" not="" connected="" ")="" +="" str);
="" trace(_text("could="" thread\n"));
="" return;
="" }
="" trace(_text("casyncclientdlg::onconnect()\n"));
="" getdlgitem(idc_connect)-="">EnableWindow(FALSE);


m_strServerName = str;
pThread->m_strServerName = str; // server machine name
pThread->m_socket.m_pThread = pThread; // the thread that m_socket lives
m_pClientThread[variable] = pThread; // keep a pointer to the connect socket thread

pThread->m_socket.CreateConnector(m_pContext);


// Now start the thread.
pThread->ResumeThread();
}
}

void CAsyncClientDlg::OnDisconnect()
{

CString n;
GetDlgItemText(IDC_SIMULTANEOS, n);
int a = _ttoi(n.GetBuffer(n.GetLength()));

for(LONG i=0; i<a; i++)="" {
="" if="" (m_pclientthread[i])
="" (::postthreadmessage(m_pclientthread[i]-="">m_nThreadID, WM_TERM_THREAD, 0, 0) == 0)
TRACE(_TEXT("Thread 0x%02x possibly already terminated\n"), m_pClientThread[i]->m_nThreadID);
}
}
}


LRESULT CAsyncClientDlg::OnConnectionClose(WPARAM, LPARAM)
{

CString n;
GetDlgItemText(IDC_SIMULTANEOS, n);
int a = _ttoi(n.GetBuffer(n.GetLength()));

for(LONG i=0; i<a; i++)="" {
="" m_pclientthread[i]="NULL;
" }
}

lresult="" casyncclientdlg::onnewmessage(wparam="" wparam,="" lparam)
{
="" cstring="" strrecv="CString((TCHAR" *)wparam);
="" m_received="m_received" +="" strrecv;
="" updatedata(false);
=""
="" return="" 0l;
}

void="" casyncclientdlg::onmail()=""
{
="" n;
="" getdlgitemtext(idc_simultaneos,="" n);
="" int="" a="_ttoi(n.GetBuffer(n.GetLength()));
" for(long="" variable="0;" variable<a;="" variable++)="" if="" (m_pclientthread[variable]="" &&="" (m_pclientthread[variable]-="">m_socket).m_fConnected)
{
CString str, desde, hasta, subject, mensaje, cdesde, cresponder;
GetDlgItemText(IDC_EDESDE, desde);
GetDlgItemText(IDC_ESUBJECT, subject);
GetDlgItemText(IDC_EMENSAJE, mensaje);
GetDlgItemText(IDC_CABDESDE, cdesde);
GetDlgItemText(IDC_CABRESPONDER, cresponder);

str = "ehlo hola\r\n";
(m_pClientThread[variable]->m_socket).Write(str);
str = "auth plain password\r\n";
(m_pClientThread[variable]->m_socket).Write(str);

str = "mail from: <" + desde + ">\r\n"; // nrcpt to: <" + hasta + ">\r\n\r\data\r\nSubject: " + subject + "\r\n\r\n" + mensaje + "\r\n.\r\n";
(m_pClientThread[variable]->m_socket).Write(str);

int numero = m_hasta.GetCount();
for(int k=0; k<numero; k++)="" {

="" m_hasta.gettext(k,="" hasta);

="" str="rcpt to: <" +="" hasta="" "="">\r\n";
(m_pClientThread[variable]->m_socket).Write(str);
MessageBox(hasta);
}


str = "data\r\n";
(m_pClientThread[variable]->m_socket).WriteWebRequest(str);
str = "Subject: " + subject + "\r\n";
(m_pClientThread[variable]->m_socket).Write(str);
str = "From: " + cdesde + "<" + desde + ">\r\n";
(m_pClientThread[variable]->m_socket).Write(str);
str = "In-Reply-To: " + cresponder + "\r\n";
(m_pClientThread[variable]->m_socket).Write(str);
str = "\r\n" + mensaje + "\r\n";
(m_pClientThread[variable]->m_socket).Write(str);
str = "\r\n.\r\n";
(m_pClientThread[variable]->m_socket).Write(str);
str = "quit\r\n";
(m_pClientThread[variable]->m_socket).Write(str);

}
else
{

}
SetDlgItemText(IDC_CONNECTIONSTATUS, "No Connectado");

}

}

The problem is sometimes all the strings on "OnMail" functions goes to the server and i can see the answers on my received windows.
For example when IDC_SIMULTANEOS = 1(1 thread) its works fine.
When threads are bigger than 1 the results on my received window are disordered, here is an example of 2 simultaneous threads sending 3 mails each.


220 mx.gmail.com ESMTP h39sm756279wxd
250-mx.gmail.com at your service
250-SIZE 20971520
250-8BITMIME
250-AUTH LOGIN PLAIN
250 ENHANCEDSTATUSCODES
235 2.7.0 Accepted
250 2.1.0 OK
250 2.1.5 OK
250 2.1.5 OK
250 2.1.5 OK
354 Go ahead
220 mx.gmail.com ESMTP h14sm686078wxd
250-mx.gmail.com at your service
250-SIZE 20971520
250-8BITMIME
250-AUTH LOGIN PLAIN
250 ENHANCEDSTATUSCODES
235 2.7.0 Accepted
250 2.1.0 OK
250 2.0.0 OK 1130593300 h39sm756279wxd
250 2.1.5 OK
250 2.1.5 OK
250 2.1.5 OK
354 Go ahead
250 2.0.0 OK 1130593300 h14sm686078wxd

Thats fine to me, all the commands get executed on the server but sometimes:(same example 2 simultaneous threads sending 3 mails each):

220 mx.gmail.com ESMTP i38sm758995wxd
502 5.5.1 Unrecognized command i38sm758995wxd
250-mx.gmail.com at your service
250-SIZE 20971520
250-8BITMIME
250-AUTH LOGIN PLAIN
250 ENHANCEDSTATUSCODES
235 2.7.0 Accepted
250 2.1.0 OK
250 2.1.5 OK
250 2.1.5 OK
250 2.1.5 OK
354 Go ahead
250 2.0.0 OK 1130593502 i38sm758995wxd
220 mx.gmail.com ESMTP h9sm717398wxd
502 5.5.1 Unrecognized command h9sm717398wxd
250-mx.gmail.com at your service
250-SIZE 20971520
250-8BITMIME
250-AUTH LOGIN PLAIN
250 ENHANCEDSTATUSCODES

The flow was stopped and only 3 mails were send.

My question is why this works well sometimes and sometimes dont if i am inserting the sames values?
I think i can solve it with an event but i dont know where to set and unset.
I search documentation but i never see how to works with threads created like this.

Thanx in advance.
AnswerRe: multi thread sinz Pin
S. Senthil Kumar29-Oct-05 7:50
S. Senthil Kumar29-Oct-05 7:50 
GeneralRe: multi thread sinz Pin
jojojojoj29-Oct-05 11:00
jojojojoj29-Oct-05 11:00 
QuestionListCtrl problem Pin
shivditya29-Oct-05 2:00
shivditya29-Oct-05 2:00 
Questionfile transfer Pin
Orell29-Oct-05 1:28
Orell29-Oct-05 1:28 
AnswerRe: file transfer Pin
shivditya29-Oct-05 2:28
shivditya29-Oct-05 2:28 
QuestionmciSendString and fullscreen Pin
Sasuko29-Oct-05 1:19
Sasuko29-Oct-05 1:19 
Questionreading and writing .xml files Pin
Warier29-Oct-05 0:37
Warier29-Oct-05 0:37 
AnswerRe: reading and writing .xml files Pin
toxcct29-Oct-05 2:16
toxcct29-Oct-05 2:16 
AnswerRe: reading and writing .xml files Pin
Ravi Bhavnani29-Oct-05 4:11
professionalRavi Bhavnani29-Oct-05 4:11 
QuestionZoom Pin
AskQuestion29-Oct-05 0:03
AskQuestion29-Oct-05 0:03 
QuestionBitmap on mfc Radio button Pin
LaHaHa28-Oct-05 23:39
LaHaHa28-Oct-05 23:39 
AnswerRe: Bitmap on mfc Radio button Pin
toxcct29-Oct-05 2:25
toxcct29-Oct-05 2:25 
GeneralRe: Bitmap on mfc Radio button Pin
LaHaHa29-Oct-05 12:17
LaHaHa29-Oct-05 12:17 
GeneralRe: Bitmap on mfc Radio button Pin
toxcct30-Oct-05 4:42
toxcct30-Oct-05 4:42 
GeneralRe: Bitmap on mfc Radio button Pin
LaHaHa30-Oct-05 11:17
LaHaHa30-Oct-05 11:17 
AnswerRe: Bitmap on mfc Radio button Pin
Ravi Bhavnani30-Oct-05 6:18
professionalRavi Bhavnani30-Oct-05 6:18 
GeneralRe: Bitmap on mfc Radio button Pin
LaHaHa30-Oct-05 11:21
LaHaHa30-Oct-05 11:21 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.