PostMessage from WorkerThread to Main Window in MFC - multithreading

I have a MFC application, which has a worker thread, what I want to do is to post message from worker thread to the Main GUI thread to update some status messages on GUI. What I have done so far is Registered a new window message
//custom messages
static UINT FTP_APP_STATUS_UPDATE = ::RegisterWindowMessageA("FTP_APP_STATUS_UPDATE");
Added this message to the message map of dialog class
ON_MESSAGE(FTP_APP_STATUS_UPDATE, &CMFC_TestApplicationDlg::OnStatusUpdate)
The prototype of OnStatusUpdate is
afx_msg LRESULT OnStatusUpdate(WPARAM, LPARAM);
and definition is
LRESULT CMFC_TestApplicationDlg::OnStatusUpdate(WPARAM wParam, LPARAM lParam)
{
//This function is not called at all.
return 0;
}
and the worker thread calling code is
void CMFC_TestApplicationDlg::OnBnClickedMfcbutton1()
{
ThreadParams params;
params.m_hWnd = m_hWnd;
params.FTPHost = "test_host";
params.FTPUsername = "test";
params.FTPPassword = "test";
AfxBeginThread(FTPConnectThread,&params);
}
and Worker thread code is
//child thread function
UINT FTPConnectThread( LPVOID pParam )
{
if(pParam == NULL)
{
return 0;
}
ThreadParams *params = (ThreadParams*)pParam;
OutputDebugString(params->FTPHost);
Sleep(4000); //simulating a network call
CString * message = new CString("Conencted");
PostMessage(params->m_hWnd,FTP_APP_STATUS_UPDATE,0,(LPARAM)message);
//PostMessage do nothing? what I am doing wrong?
return 1;
}
the problem is when the PostMessage function is called the OnStatusUpdate should be called, but it is not being called, no exception or assertion is thrown, What I am doing wrong? I have tried ON_REGISTERED_MESSAGE and ON_MESSAGE but no success, any help?

CMFC_TestApplicationDlg::OnBnClickedMfcbutton1() may return before the thread starts. This causes your ThreadParams to go out of scope, so when you access it from the thread, you are accessing freed memory. You need to allocate it some other way, such as:
void CMFC_TestApplicationDlg::OnBnClickedMfcbutton1()
{
ThreadParams* params = new ThreadParams();
params->m_hWnd = m_hWnd;
params->FTPHost = "test_host";
params->FTPUsername = "test";
params->FTPPassword = "test";
AfxBeginThread(FTPConnectThread,params);
}
//child thread function
UINT FTPConnectThread( LPVOID pParam )
{
if(pParam == NULL)
{
return 0;
}
ThreadParams *params = (ThreadParams*)pParam;
OutputDebugString(params->FTPHost);
Sleep(4000); //simulating a network call
CString * message = new CString("Conencted");
PostMessage(params->m_hWnd,FTP_APP_STATUS_UPDATE,0,(LPARAM)message);
delete params;
return 1;
}

Related

NODE N-API When using functions from addon blocks the main process node js?

Implemented a wrapper for the function using N-API. It works, but the problem is that when it works the main thread of the node does not work? How can I rewrite this function so that it does not block the main thread? What do I need to use async, callback, thread for this?
napi_value readDataSerial(napi_env env, napi_callback_info info) {
napi_status status;
size_t argc = 1;
napi_value args[1], object;
napi_value id, data;
status = napi_get_cb_info(env, info, &argc, args, NULL, NULL);
assert(status == napi_ok);
if (argc < 1) {
napi_throw_type_error(env, NULL, "Wrong number of arguments");
return NULL;
}
//....
Ibox_ReadData *context = calloc(1, sizeof(Ibox_Context));
//this function takes a long time and blocks the main process
Ibox_Result_Submit *submitResult = Ibox_Controller_ReadData(context);
status = napi_create_object(env, &object);
status = napi_create_string_utf8(env, submitResult->id, NAPI_AUTO_LENGTH, &id);
status = napi_set_named_property(env, object, "id", id);
status = napi_create_string_utf8(env, submitResult->date, NAPI_AUTO_LENGTH, &date);
status = napi_set_named_property(env, object, "data", data);
assert(status == napi_ok);
return object;
})
It's not trivial, but this example shows how to create an async worker thread and return results without blocking.

DoModal in critical section

in an parallel loop, there is a critical section. I try to execute an mfc dialog with DoModal in the critical section, however since main thread waits for parallel threads, there is no way for my dialog to show up and execute. In order to break this dependency, I create an executable and I run it as a process within my parallel loop. When the process shows dialog and gets the information. It returns and other threads keeps running.
However my team leader insist that there is a better way to do it which I couldn't figure out after doing hours of search :\
I tried a seperate thread in parallel for. It didn't worked.
I tried CWinThread (google say it is gui thread :\ which didn't helped)
I tired creating an exe and running it. That worked :)
int someCriticDialog()
{
#pragma omp critic (showCriticDlg)
{
CMyDialog ccc;
ccc.DoModal();
/* However the code below works
CreateProcess("someCriticDlg.exe", null, &hProcess);
WaitForSingeObject(hProcess, INFINITE);
*/
}
}
#pragma omp parallel
for (int i = 0; i < 5; i++)
someCriticDialog();
Let's say here is the problem:
void trouble_maker()
{
Sleep(10000);//application stops for 10 seconds
}
You can use PostMessage + PeekMessage + modal dialog to wait for it to finish through GUI window:
void PumpWaitingMessages()
{
MSG msg;
while (::PeekMessage(&msg, NULL, NULL, NULL, PM_NOREMOVE))
if (!AfxGetThread()->PumpMessage())
return;
}
BEGIN_MESSAGE_MAP(CMyDialog, CDialog)
ON_COMMAND(2000, OnDoSomething)
ON_COMMAND(IDCANCEL, OnCancel)
END_MESSAGE_MAP()
CMyDialog::CMyDialog(CWnd* par /*=NULL*/) : CDialog(IDD_DIALOG1, par)
{
working = false;
stop = false;
}
BOOL CMyDialog::OnInitDialog()
{
BOOL res = CDialog::OnInitDialog();
//call the function "OnDoSomething", but don't call it directly
PostMessage(WM_COMMAND, 2000, 0);
return res;
}
void CMyDialog::OnCancel()
{
if (working)
{
stop = true;
}
else
{
CDialog::OnCancel();
}
}
void CMyDialog::OnDoSomething()
{
HANDLE h = CreateThread(NULL, 0, (LPTHREAD_START_ROUTINE)&trouble_maker, NULL, 0, NULL);
working = true;
for (;;)
{
if (WAIT_TIMEOUT != WaitForSingleObject(h, 100)) break;
PumpWaitingMessages();
//update progress bar or something...
if (stop)
{
//terminate if it's safe
//BOOL res = TerminateThread(h, 0);
//CloseHandle(h);
//CDialog::OnCancel();
//return;
}
}
working = false;
MessageBox("done");
}

CreateThread doesn't work in my code

I wonder how to solve this problem.
I notice that CreateThread() doesn't work well in this code:
DWORD threadFunc1(LPVOID lParam)
{
int cur = (int)lParam
while(1)
{
//Job1...
//Reason
break;
}
Start(cur + 1);
Sleep(100);
}
void Start(int startNum)
{
....
CreateThread(NULL, NULL, (LPTHREAD_START_ROUTINE)threadFunc1, &startNum, 0, &dwThreadId);
...
}
void btnClicking()
{
Start(0);
}
In this code, there is a thread create by Start() and it calls Start() when the thread ends.
The second created thread does not work. I think the first thread disappeared and the second thread is destroyed.
What is the best way to solve this?
OS: Win 7 64bit Ultimate.
Tool: Visual Studio 2008.
It does not work because your code has bugs in it. The signature of your thread function is wrong, and you are passing the startNum value to the thread in the wrong way.
Try this instead:
DWORD WINAPI threadFunc1(LPVOID lParameter)
{
int cur = (int) lParameter;
while(1)
{
//Job1...
//Reason
break;
}
Start(cur + 1);
Sleep(100);
}
void Start(int startNum)
{
....
HANDLE hThread = CreateThread(NULL, NULL, &threadFunc1, (LPVOID) startNum, 0, &dwThreadId);
if (hThread != NULL)
{
// ... store it somewhere or close it, otherwise you are leaking it...
}
...
}
void btnClicking()
{
Start(0);
}

Visual c++ DirectX compilation error

Can any one help me with this error ?
And I have Linked to d3d10.lib and d3dx10.lib.
I am new to directX stuff and I was following wendy jones DirectX 10 toturial
1>t1.obj : error LNK2019: unresolved external symbol _D3D10CreateDeviceAndSwapChain#32 referenced in function "bool cdecl InitDirect3D(struct HWND *,int,int)" (?InitDirect3D##YA_NPAUHWND__##HH#Z)
1>C:\Users\Ehsan\Documents\Visual Studio 2010\Projects\DirectX\t1\Debug\t1.exe : fatal error LNK1120: 1 unresolved externals
source code :
// t1.cpp : Defines the entry point for the application.
//
#include "stdafx.h"
#include "windows.h"
#include "tchar.h"
#include <d3d10.h>
#include <d3dx10.h>
// Global Variables:
HINSTANCE hInst; // global handle to hold the application instance
HWND wndHandle; // global variable to hold the window handle
int width = 640;
int height = 480;
// Direct3D global vars
ID3D10Device * pD3DDevice = NULL;
IDXGISwapChain * pSwapChain = NULL;
ID3D10RenderTargetView * pRenderTargetView = NULL;
// Forward declarations of functions included in this code module:
LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);
bool InitWindow( HINSTANCE hInstance, int width, int height );
void Render();
void ShutDownDirect3D();
bool InitDirect3D(HWND hWnd, int width, int height);
int APIENTRY _tWinMain(HINSTANCE hInstance,
HINSTANCE hPrevInstance,
LPTSTR lpCmdLine,
int nCmdShow)
{
// TODO: Place code here.
MSG msg = {0};
// Perform application initialization:
if ( !InitWindow( hInstance, width, height ) )
{
return FALSE;
}
// called after creating the window
if(!InitDirect3D(wndHandle, width, height))
{
return FALSE;
}
//hAccelTable = LoadAccelerators(hInstance, MAKEINTRESOURCE(IDC_T1));
// Main message loop:
while (WM_QUIT != msg.message)
{
while (PeekMessage(&msg, NULL, 0, 0, PM_REMOVE) == TRUE)
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
// Call the render function
Render();
}
ShutDownDirect3D();
return (int) msg.wParam;
}
bool InitWindow(HINSTANCE hInstance, int width, int height)
{
WNDCLASSEX wcex;
// Fill in the WNDCLASSEX structure. This describes how the window
// will look to the system
wcex.cbSize = sizeof(WNDCLASSEX); // the size of the structure
wcex.style = CS_HREDRAW | CS_VREDRAW; // the class style
wcex.lpfnWndProc = (WNDPROC)WndProc; // the window procedure callback
wcex.cbClsExtra = 0; // extra bytes to allocate for this class
wcex.cbWndExtra = 0; // extra bytes to allocate for this instance
wcex.hInstance = hInstance; // handle to the application instance
wcex.hIcon = 0; // icon to associate with the application
wcex.hCursor = LoadCursor(NULL, IDC_ARROW); // the default cursor to use
wcex.hbrBackground = (HBRUSH)(COLOR_WINDOW+1); // the background color
wcex.lpszMenuName = NULL; // the resource name for the menu
wcex.lpszClassName = TEXT("DirectXExample"); // the class name being created
wcex.hIconSm = 0; // the handle to the small icon
RegisterClassEx(&wcex);
// Resize the window
RECT rect = { 0, 0, width, height };
AdjustWindowRect(&rect, WS_OVERLAPPEDWINDOW, FALSE);
// create the window from the class above
wndHandle = CreateWindow(TEXT("DirectXExample"),
TEXT("DirectXExample"),
WS_OVERLAPPEDWINDOW,
CW_USEDEFAULT,
CW_USEDEFAULT,
rect.right - rect.left,
rect.bottom - rect.top,
NULL,
NULL,
hInstance,
NULL);
if (!wndHandle)
{
return false;
}
// Display the window on the screen
ShowWindow(wndHandle, SW_SHOW);
UpdateWindow(wndHandle);
return true;
}
//
// FUNCTION: WndProc(HWND, UINT, WPARAM, LPARAM)
//
// PURPOSE: Processes messages for the main window.
//
// WM_COMMAND - process the application menu
// WM_PAINT - Paint the main window
// WM_DESTROY - post a quit message and return
//
//
LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
// Check any available messages from the queue
switch (message)
{
// Allow the user to press the Escape key to end the application
case WM_KEYDOWN:
switch(wParam)
{
// Check if the user hit the Escape key
case VK_ESCAPE:
PostQuitMessage(0);
break;
}
break;
// The user hit the close button, close the application
case WM_DESTROY:
PostQuitMessage(0);
break;
}
// Always return the message to the default window procedure for furtherprocessing
return DefWindowProc(hWnd, message, wParam, lParam);
}
/*******************************************************************
* InitDirect3D
* Initializes Direct3D
* Inputs - Parent window handle - HWND,
Window width - int
Window height - int
Updating the Code 31
* Outputs - true if successful, false if failed - bool
*******************************************************************/
bool InitDirect3D(HWND hWnd, int width, int height)
{
// Create the clear the DXGI_SWAP_CHAIN_DESC structure
DXGI_SWAP_CHAIN_DESC swapChainDesc;
ZeroMemory(&swapChainDesc, sizeof(swapChainDesc));
// Fill in the needed values
swapChainDesc.BufferCount = 1;
swapChainDesc.BufferDesc.Width = width;
swapChainDesc.BufferDesc.Height = height;
swapChainDesc.BufferDesc.Format = DXGI_FORMAT_R8G8B8A8_UNORM;
swapChainDesc.BufferDesc.RefreshRate.Numerator = 60;
swapChainDesc.BufferDesc.RefreshRate.Denominator = 1;
swapChainDesc.BufferUsage = DXGI_USAGE_RENDER_TARGET_OUTPUT;
swapChainDesc.OutputWindow = hWnd;
swapChainDesc.SampleDesc.Count = 1;
swapChainDesc.SampleDesc.Quality = 0;
swapChainDesc.Windowed = TRUE;
// Create the D3D device and the swap chain
HRESULT hr = D3D10CreateDeviceAndSwapChain(NULL,
D3D10_DRIVER_TYPE_REFERENCE,
NULL,
0,
D3D10_SDK_VERSION,
&swapChainDesc,
&pSwapChain,
&pD3DDevice);
// Error checking. Make sure the device was created
if (hr != S_OK)
{
return false;
}
// Get the back buffer from the swapchain
ID3D10Texture2D * pBackBuffer;
hr = pSwapChain->GetBuffer(0, __uuidof(ID3D10Texture2D), (LPVOID*) &pBackBuffer);
if (hr != S_OK)
{
return false;
}
// create the render target view
hr = pD3DDevice->CreateRenderTargetView(pBackBuffer, NULL, &pRenderTargetView);
// release the back buffer
pBackBuffer->Release();
// Make sure the render target view was created successfully
if (hr != S_OK)
{
return false;
}
// set the render target
pD3DDevice->OMSetRenderTargets(1, &pRenderTargetView, NULL);
// create and set the viewport
D3D10_VIEWPORT viewport;
viewport.Width = width;
viewport.Height = height;
viewport.MinDepth = 0.0f;
viewport.MaxDepth = 1.0f;
viewport.TopLeftX = 0;
viewport.TopLeftY = 0;
pD3DDevice->RSSetViewports(1, &viewport);
return true;
}
/*******************************************************************
* ShutdownDirect3D
* Closes down and releases the resources for Direct3D
34 Chapter 2 n Your First DirectX Program
* Inputs - void
* Outputs - void
*******************************************************************/
void ShutDownDirect3D()
{
// release the rendertarget
if(pRenderTargetView)
{
pRenderTargetView->Release();
}
// release the swapchain
if(pSwapChain)
{
pSwapChain->Release();
}
// release the D3D Device
if(pD3DDevice)
{
pD3DDevice->Release();
}
}
/*******************************************************************
* Render
* All drawing happens in the Render function
* Inputs - void
* Outputs - void
*******************************************************************/
void Render()
{
if (pD3DDevice != NULL)
{
// clear the target buffer
pD3DDevice->ClearRenderTargetView(pRenderTargetView, D3DXCOLOR(0.0f, 0.0f, 0.0f, 0.0f));
// All drawing will go here.
// display the next item in the swap chain
pSwapChain->Present(0, 0);
}
}
It's a problem of linkage, did you add the right library in your visual studio ?
Add D3D10.lib to the list of linker dependencies.
What about your include folder in your hard drive?
You need to go to that through your includes and also added in lib/86 to library dependances.
Follow the steps in this link: http://www.rastertek.com/dx10tut01.html
This will take you through the process of setting up DirectX. Worst happens and you do it and you still get the same problem, you know its not a linker issue. Which 4 people have now told you it is.
Also this site is great for taking you through DirectX as well. I highly advise.

How to use CriticalSection - MFC?

I' am working on a small example and am a bit of curious using criticalsection in my example.
What I'am doing is,I have a CStringArray(which has 10 elements added to it).I want to copy
these 10 elements(string) to another CStringArray(am doing this to understand threading and Critical section),I have created 2 threads,Thread1 will copy the first 5 element to another CStringArray and Thread2 will copy the rest.Here two CStringArray are being used,I know only 1 thread can access it at a time.I wanted to know how this can be solved by using criticalsection or any other method.
void CThreadingEx4Dlg::OnBnClickedOk()
{
// TODO: Add your control notification handler code here
thread1 = AfxBeginThread((AFX_THREADPROC)MyThreadFunction1,this);
thread2 = AfxBeginThread((AFX_THREADPROC)MyThreadFunction2,this);
}
UINT MyThreadFunction1(LPARAM lparam)
{
CThreadingEx4Dlg* pthis = (CThreadingEx4Dlg*)lparam;
pthis->MyFunction(0,5);
return 0;
}
UINT MyThreadFunction2(LPARAM lparam)
{
CThreadingEx4Dlg* pthis = (CThreadingEx4Dlg*)lparam;
pthis->MyFunction(6,10);
return 0;
}
void CThreadingEx4Dlg::MyFunction(int minCount,int maxCount)
{
for(int i=minCount;i<=maxCount;i++)
{
CString temp;
temp = myArray.GetAt(i);
myShiftArray.Add(temp);
}
}
The way I'd use a CriticalSection is:
Declare a member variable in your CThreadingEx4Dlg class:
CCriticalSection m_CriticalSection;
Enclose your not thread safe code in a Lock-Unlock block of this CriticalSection:
void CThreadingEx4Dlg::MyFunction(int minCount,int maxCount)
{
m_CriticalSection.Lock();
for(int i=minCount;i<=maxCount;i++)
myShiftArray.Add(myArray.GetAt(i));
m_CriticalSection.Unlock();
}
Consider using CSingleLock so that the constructor takes care of the locking and the destructor automatically takes care of the unlocking
void CThreadingEx4Dlg::MyFunction(int minCount,int maxCount)
{
CSingleLock myLock(&m_CriticalSection, TRUE);
// do work here.
// The critical section will be unlocked when myLock goes out of scope
}

Resources