如何在不同的线程中执行 [英] How to perform in different thread
问题描述
嗨!
我想为WriteDataToPort创建一个单独的线程。如何创建cstring并将其作为参数传递给它。
下面是我的代码片段。
Hi!
I would like to create a separate thread for WriteDataToPort. How to create and pass cstring as parameter to it.
Below is my code snippet.
CString ss=_T("");
while(!feof(fpRead))
{
size_t count = fread(file_buffer, sizeof(char), l_size, fpRead);
for(int i=0; i< count; i++)
{
ss = file_buffer[i];
m_CommPort->WriteDataToPort(ss.GetBuffer(ss.GetLength()));
}
}
void CCommPort::WriteDataToPort(TCHAR* f_WriteData)
{
WaitForSingleObject(m_hWriteCompleteEvent,INFINITE);
ResetEvent(m_hWriteCompleteEvent);
m_bKeyboardWrite =TRUE;
memset(m_szWriteBuff, 0, sizeof(m_szWriteBuff));
tcscpy(m_szWriteBuff, f_WriteData);
SetEvent(m_hWriteEvent);
}
i have tried in this way, but getting exception. Please do the needful.
hThread = CreateThread(NULL,0,WriteDataToPortThread,LPVOID)ss.GetBuffer(ss.GetLength()),0,NULL);
DWORD __stdcall CMainFrame::WriteDataToPortThread(LPVOID lpParam)
{
CString* ss = (CString*)lpParam;
((CMainFrame*)AfxGetMainWnd())->m_CommPort->WriteDataToPort(ss->GetBuffer(ss->GetLength()));
return 0;
}
非常感谢
Sam。
Thanks much
Sam.
推荐答案
请请参阅: http://www.computersciencelab.com/MultithreadingTut1.htm [ ^ ]。
找到创建线程部分在Windows下,使用_beginthread
的代码示例。以下是这个想法:最后一个参数arglist
是无效指针。它用于将任何数据传递给作为第一个参数start_address
传递的线程入口点函数。执行此操作时,将启动start_address
,因为它是作为唯一参数传递的arglist
指针调用的。在start_address
的实现中,您应该将此void指针强制转换为指向您以前传递的数据类型的指针arglist
当你打电话给_beginthread
。
请参阅:
http://msdn.microsoft.com/en-us/library/aa246693%28v = vs.60%29.aspx [ ^ ]。
另请参阅我过去的答案以获取更多想法:从班级中的一个主题开始一个主题 [ ^ ]。
-SA
Please see: http://www.computersciencelab.com/MultithreadingTut1.htm[^].
Locate the section "Creating Threads Under Windows", and the code sample using_beginthread
. Here is the idea: last parameterarglist
is the void pointer. It is used to pass any data to the thread entry point function passed as the first parameterstart_address
. When you do that,start_address
will be started as it is called with thearglist
pointer passed as its only argument. In your implementation ofstart_address
, you should cast this void pointer to the pointer to the data type you used to pass as thearglist
when you called_beginthread
.
Please see:
http://msdn.microsoft.com/en-us/library/aa246693%28v=vs.60%29.aspx[^].
See also my past answer for some more ideas: Starting a thread from a thread within a class[^].
—SA
1)分配一个字符串(obj等等CString类) - 但不在堆栈上。您可以使它成为全局或应用程序类的成员,甚至可以动态创建它。
2)使用AfxBeginThread创建一个新线程并将其传递给字符串对象的地址,例如
1) Allocate a string (object of CString class) - but not on stack. You can make it a global or a member of the application class, or even create it dynamically.
2) Create a new thread using AfxBeginThread and pass it the address of your string object, e.g.
AfxBeginThread(YourThreadFunction, &g_MyString, THREAD_PRIORITY_NORMAL);
3)在您的线程函数内(例如YourThreadFunction),在开头将LPVOID p转换为CString *然后您可以将它用作指向您线程中字符串的指针'循环:
3) inside your thread function (e.g. YourThreadFunction), cast LPVOID p to CString * in the beginning and then you can use it as a pointer to string in your thread''s loop:
INT YourThreadFunction(LPVOID p)
{
CString *pStr = (CString *)p;
while (!gCancel)
{
// your loop
}
return 0;
}
它当然是粗糙的,但你明白了。所有关于同时访问,重新分配等的常见注意事项仍然适用。
我通常会创建一个线程接口类,其中包含传递数据所需的所有内容两个线程之间 - 可能包含字符串等。
It is of course crude as, but you get the idea. All the usual pre-cautions about simultaneous access, reallocation and so on will still apply.
I normally create a thread interface class that contains everything that is needed to pass data between 2 threads - which could contain strings amongst other things.
这篇关于如何在不同的线程中执行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!