Click to See Complete Forum and Search --> : Multihreading Query from a newcomer?
dp_76
August 12th, 2005, 02:14 AM
Hi
This is my first query in the Multithreading section.
I have created an SDI MFC application.
My application is not multithreaded. There is only one main application thread.
Now what is happening is that:
whenever my application is doing some heavy process such as "Saving a large file" or "search something in the hard disk" & change the focus to some other application or minimise the application
& then when i maximise or bring the focus back to my application, all the items such as menus, toolbars, status log disappears & only the whilte screen is left.
Some times the application is not maximised or focussed.
I think it has something to do with multi threading.
Waiting for suggestions
D_Drmmr
August 12th, 2005, 05:02 AM
You can indeed solve this by using multithreading. What is happening is that your main thread has to redraw the windows after they have been minimised. However, that thread is still busy and won't redraw the window until the processing is completed. To overcome this, you need to create a new thread (called a worker thread) in which you do the heavy processing. That is, when you want to perform some action that may take some time, you create a new thread in which this action takes place. When the action is completed, the thread exits. The main thread can then still process the redrawing messages it receives, since it is not busy.
dp_76
August 12th, 2005, 06:18 AM
Hi
Thanks for the suggestions.
I have tried to implement what u have suggested, but there is one more strange problem that is cuming.
I wud also like to remind that my application is an MFC SDI explorer type application with a tree view & a list view.
The tree view class(CLeftView) is derived from CTreeView & the list view class(CRightView) is derived from CListView.
The tree view is used to show the directories & the list view is used to show the contents of a particular directory on whose icon the user has clicked.
Here i summarize the situation where the problem is cuming:
there are 3 files involved:
--------->a.cpp
--------->b.cpp
--------->LeftView.cpp
In a.cpp in the function << bigProcess()>> i m creating a worker thread.
From this worker thread i m calling the function <<HeavyProcessing()>> of the class b.cpp.
Now from the function <<HeavyProcessing()>>, one more function is being called name <<check()>> .
I am encountering a problem in the <<check()>> function.
Defination of <<check()>>....
BOOL b::check(DWORD dwFileId)
{
CStellApp *pApp = ( CStellApp* )AfxGetApp();
NSS* NDL_temp = (pFrame->GetLeftPane())->DirsOnlyStart;
while(NDL_temp)
{
if(NDL_temp->dwFileId == dwFileId)
return TRUE;
NDL_temp = NDL_temp->next;
}
return FALSE;
}
The error is cuming in the second line...
NSS*NDL_temp = (pFrame->GetLeftPane())->DirsOnlyStart;
the code (pFrame->GetLeftPane()) causes the control to jump to the function GetLeftPane() of the class CMainFrame.
CLeftView* CMainFrame::GetLeftPane()
{
CWnd* pWnd = m_wndSplitter.GetPane(0, 0);
CLeftView *pView = DYNAMIC_DOWNCAST(CLeftView, pWnd);
return pView;
}
the error comes in the line:
CWnd* pWnd = m_wndSplitter.GetPane(0, 0);
Can one thread access the resources (such as pointers, handles) of another thread.
Wating for suggestions
Regards
:(
dp_76
August 17th, 2005, 12:39 AM
Hi
I am waiting for some suggestions.
:(
PadexArt
August 17th, 2005, 05:28 AM
MFC objects are not ment to be used from multiple threads ( they are not thread safe). Besides, you should not operate directlly from the worker thread on the GUI thread as it might create deadlocks in your app.
The solution is to use PostMessage to communicate with and from the worker thread.
PadexArt
August 17th, 2005, 05:31 AM
Although I prefer using threads in such situations, they are not the only solution to this type of problem. Another solution is the "idle loop processing".
Here are the Google search results (http://www.google.ro/search?hl=ro&q=%22idle+loop+processing%22&btnG=Caut%C4%83&meta=)
codeguru.com
Copyright Internet.com Inc., All Rights Reserved.