Home > Software engineering >  After the main thread post website, how to use multithreaded operations
After the main thread post website, how to use multithreaded operations

Time:10-03

The main thread logged in, how to call in other threads? Ace to help!

//1. In MyDlg before me. H a global object defined in the
Public:
IWinHttpRequestPtr pHttpReq;
//2. Then in MyDlg. To log in to add a button function in the CPP
Void CMyDlg: : OnBnClickedLogin ()
{... }
//3. After successful login, use the following code to obtain information

Cstrings strUrl="* * * * * *";
//pHttpReq. CreateInstance (__uuidof (WinHttpRequest));
PHttpReq - & gt; Open (_T (" GET "), strUrl. AllocSysString ());
PHttpReq - & gt; The Send ();

//the problem is coming, the above code to access information on the main thread can get no problem,
//but how to put this code in the other thread?
//I like this operation, will go wrong:
//1. First in MyDlg. H a thread function is defined in
Public:
The static UINT OnGetData (LPVOID lpParam);
//2. Then in MyDlg. CPP implementation function in
UINT CMyDlg: : OnGetData (LPVOID lpParam)
{
DLG CMyDlg *=(CMyDlg *) lpParam;
CoInitialize (NULL);
Cstrings strUrl="* * * * * *";
//DLG - & gt; PHttpReq. CreateInstance (__uuidof (WinHttpRequest));
DLG - & gt; PHttpReq - & gt; Open (_T (" GET "), strUrl. AllocSysString ());
DLG - & gt; PHttpReq - & gt; The Send ();
CoUninitialize ();
return 0;
}
//3. Error when I was call
AfxBeginThread (OnGetData, this);

CodePudding user response:

Did the CreateInstance success first, and then is whether the corresponding API support multiple thread calls

CodePudding user response:

CWinThread * AfxBeginThread (
AFX_THREADPROC pfnThreadProc,
LPVOID pParam,
Int nPriority=THREAD_PRIORITY_NORMAL,
UINT nStackSize=0,
DWORD dwCreateFlags=0,
LPSECURITY_ATTRIBUTES lpSecurityAttrs=NULL
);
PfnThreadProc
Points to the controlling function for the worker thread. Always be NULL. This function must be declared as follows:
UINT __cdecl MyControllingFunction (LPVOID pParam);

CodePudding user response:

You can create a thread to you pHttpReq pointer threads as the parameter passed to the thread function
  • Related