Home > Software engineering >  I wrote a worker thread is to open a TXT documents and then send out line by line, in the send butto
I wrote a worker thread is to open a TXT documents and then send out line by line, in the send butto

Time:09-26

Probably process is like this, open the file & gt; Choose to TXT file & gt; The while loop (read the ith line & gt; Write the ith line & gt; Send the ith line & gt; Sleep ()) as follows don't know if it is wrong,
 UINT CNetworkSendDataDlg: : zhuhangTimeThread (LPVOID lpParam) 
{
LpParam CNetworkSendDataDlg * pDlg=(CNetworkSendDataDlg *);
CFileDialog cFileDlg (TRUE, _T (" TXT "), (LPCTSTR) NULL, OFN_ENABLESIZING | OFN_EXPLORER | OFN_PATHMUSTEXIST |
OFN_LONGNAMES | OFN_OVERWRITEPROMPT | OFN_PATHMUSTEXIST | OFN_CREATEPROMPT,
_T (" Text Files (*.txt) | *. TXT | All Files (*. *) | *. * | | "));
CStdioFile file (cFileDlg GetPathName (), CFile: : modeRead);
Cstrings strTime strOneline;
PDlg - & gt; GetDlgItemText (IDC_EDIT_SEND strTime);
While (file. ReadString (strOneline))
{
PDlg - & gt; M_EditSend. ReplaceSel (strOneline + "\ n");
Send (pDlg - & gt; M_clientSocket, strTime GetBuffer (0), strTime. GetLength (), 0);
Sleep (1000);
}
return 0;
}

 void CNetworkSendDataDlg: : OnBnClickedButton3 () 
{
//TODO: add the control notification handler code
CWinThread * testThread;
TestThread=AfxBeginThread (zhuhangTimeThread, NULL);
}

CodePudding user response:

Sleep (1000); Delay to shorten

CodePudding user response:

I this time delay can be change, I just set 1 s now, I changed or not,

CodePudding user response:

 while (file. ReadString (strOneline)) 
{
PDlg - & gt; M_EditSend. ReplaceSel (strOneline + "\ n");
Send (pDlg - & gt; M_clientSocket, LPCTSTR strTime, strTime GetLength () * sizeof (TCHAR), 0);
Sleep (1000);
}
File. The Close ();

CodePudding user response:

Auxiliary thread generally cannot directly access the main thread of window control object and so on, this can cause abnormal program

CodePudding user response:

Worker threads had better not operation interface, if you want to display the information in the window, the worker thread sends a message,

CodePudding user response:

reference 5 floor wangkuo106 reply:
thread had better not operation interface, if you want to display the information in the window, the worker thread sends a message,

Not operating interface, the work interface thread parameter variable called thread? Like the above the worker thread to read the text of each line, each line the TXT the inside of the thread and how to work?

CodePudding user response:

PostMessage () to refresh your corresponding to the function of the interface to access to the contents of the passed as a parameter

CodePudding user response:

If your interface at this time off and see your program will crash

CodePudding user response:

Pay attention to Sleep (1000); Sleep is the main thread,

Void DoEvents ()
{
MSG MSG.
//Process existing messages in the application's message queue.
//When the queue is empty, do the clean up and return.
While (: : PeekMessage (& amp; MSG, NULL, 0, 0, PM_NOREMOVE))
{//from the MSG
if (! AfxGetThread () - & gt; PumpMessage break ());
}
}

Sleep (1000); Instead of DoEvents ()

CodePudding user response:

Multiple Threads in the User Interface of http://msdn.microsoft.com/zh-cn/library/ms810439.aspx

CodePudding user response:

refer to 6th floor qq_37110831 response:
Quote: refer to the fifth floor wangkuo106 reply:

Worker threads had better not operation interface, if you want to display the information in the window, the worker thread sends a message,

Not operating interface, the work interface thread parameter variable called thread? Like the above the worker thread to read the text of each line, each line the TXT the inside of the thread and how to work?

Using PostMessage, listening to the news on the main thread, and then send it to read the text, is OK? As for the transfer and you can define a string, in the child thread assignment, read message

CodePudding user response:

With the void DoEvents (); Don't need a multithreaded

CodePudding user response:

11 references wangkuo106 response:
Quote: refer to the sixth floor qq_37110831 response:

Quote: refer to the fifth floor wangkuo106 reply:

Worker threads had better not operation interface, if you want to display the information in the window, the worker thread sends a message,

Not operating interface, the work interface thread parameter variable called thread? Like the above the worker thread to read the text of each line, each line the TXT the inside of the thread and how to work?

Using PostMessage, listening to the news on the main thread, and then send it to read the text, is OK? As for the transfer and you can define a string, in the child thread assignment, message read

I didn't understand what you mean, I'm still in a learning phase, trouble can specifically

CodePudding user response:

Mainly is the PostMessage method used to remove data from a worker thread is sent to the main thread, all UI operation from the main thread to handle

CodePudding user response:

On the 14th floor oyljerry
reference response:
main way is to use PostMessage send data from a worker thread to the main thread, all UI operation from the main thread to handle

If put the loop in the PostMessage then in message processing function call to go?

CodePudding user response:

11 references wangkuo106 response:
Quote: refer to the sixth floor qq_37110831 response:

Quote: refer to the fifth floor wangkuo106 reply:

Worker threads had better not operation interface, if you want to display the information in the window, the worker thread sends a message,

Not operating interface, the work interface thread parameter variable called thread? Like the above the worker thread to read the text of each line, each line the TXT the inside of the thread and how to work?

Using PostMessage, listening to the news on the main thread, and then send it to read the text, is OK? As for the transfer and you can define a string, in the child thread assignment, message read

I still don't understand, can you say specific point

CodePudding user response:

nullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnull
  • Related