|
Ah. Sorry, I was hasty in replying!
/ravi
My new year's resolution: 2048 x 1536
Home | Articles | Freeware | Music
ravib@ravib.com
|
|
|
|
|
Hi
I have an application which opens a file multiple times. The series of operations are as follows:
1. Open File when an OPEN button is pressed.
2. Close file when a CLOSE button is pressed.
After the 2. operation is complete and 1. option is given the same file is opened again.
But at this point, the OPEN fails and the application freezes.
Sreekanth Muralidharan,
Corporate Systems Consultant [Embedded Systems],
INDIA
|
|
|
|
|
If you open file Exclusivly and Doesn't free the Memory associated with FILE handle at close. when you again try to open that file, this type proplem always persist.
"Opinions are neither right nor wrong. I cannot change your opinion. I can, however, change what influences your opinion." - David Crow
cheers,
Alok Gupta
|
|
|
|
|
Should closing the file do the job? Or else, is there any explicit API to free the memory associated with the file handle? Please help.
|
|
|
|
|
Should closing the file do the job? Or else, is there any explicit API to free the memory associated with the file handle? Please help.
Sreekanth Muralidharan,
Corporate Systems Consultant [Embedded Systems],
INDIA
|
|
|
|
|
Sreekanth Muralidharan wrote:
there any explicit API to free the memory associated with the file handle?
Hi Mr MuraliDharan,
Could you please tell Which api are you using to Open the File?.
"Opinions are neither right nor wrong. I cannot change your opinion. I can, however, change what influences your opinion." - David Crow
cheers,
Alok Gupta
|
|
|
|
|
Hi Alok
I am not using any APIs for this. I am using the CFile class in MFC for managing the file operations. The file operations are simple read and write and the file has the contents downloaded via a USB interface.
The flow is somewhat like this:
1. When user presses PLAY button, a play thread is started.
2. The PLAY function opens the file and assigns the handle to a CFile object.
3. When he presses STOP the thread is terminated and the file is closed.
The play thread goes on like this:
1. Read a block of 64 bytes from the file.
2. Write the block to the USB handle.
NOTE: The file is made by a separate module in the same application. So the application which downloads the USB contents is independant of the module which reads from it.
Expecting your help again....
Sreekanth Muralidharan,
Corporate Systems Consultant [Embedded Systems],
INDIA
|
|
|
|
|
Sreekanth Muralidharan wrote:
When he presses STOP the thread is terminated and the file is closed
Hi SreeKanth,
Sorry for replying late,
Sreekanth Muralidharan wrote:
I am using the CFile class in MFC for managing the file
according to Oops Concept, the CFile will close the File and frees memory related to it in Destructor.
Sreekanth Muralidharan wrote:
assigns the handle to a CFile object.
Are you calling Corressponding CFile::Detach() as you must be calling CFile::Attach to attach File Handle.
Have you checked the thread Stop gracefully without any problem before second time play button hit!.
"Opinions are neither right nor wrong. I cannot change your opinion. I can, however, change what influences your opinion." - David Crow
cheers,
Alok Gupta
|
|
|
|
|
Hi Alok
Thanks a lot for your quick response.
#####assigns the handle to a CFile object.
Are you calling Corressponding CFile::Detach() as you must be calling CFile::Attach to attach File Handle.
#####
Is this required since a CFile::Open would return a handle to the file object. Could you please tell me what is the hidden functionality involved in CFile::Detach()/Attach().
$$$$$
Have you checked the thread Stop gracefully without any problem before second time play button hit!.
$$$$$
Is there any method for it? Any way I think we can use the concept of critical sections for making sure that all the resources alloted to the thread has been returned back before it is terminated. But this did not solve my problem still. Is there any tool to analyze the thread process so that I could see the resource leaks inside the thread?
And again I have an FTDI USB driver installed in the system where I am reading and writing to USB. We are able to read and write the flat file contents in the system to the USB. But when I try to write a single character (say 'C'), I get a block.
The application is for a Solid State Cockpit Voice Recorder where we have an MPU player attached to the system via a USB and speakers attached to MPU. The system writes data to the USB and the controller in the USB handles the data and transfers it to the four voice channels connected to speakers. When user presses the pause button the application suspends the write thread acting on the USB. When the user presses the same pause button again, he sends a character 'P' to the USB and the controller acknowledges the command by returning the same character. We have a condition check in the application to ensure that the pause command worked properly. It is this write which creates all the trouble. I think you got it.
The same application has another module which downloads data from flash memory cards connected to the SSCVR and this data rounds to about 400MB. We have no problem as said above in handling this bulk of data, but the single character acknowledgement fails quite too often.
That's the project on.
Please read the mail in detail and your suggestions are very valuable not only for me but for the entire codeproject world.
Sreekanth Muralidharan,
Corporate Systems Consultant [Embedded Systems],
INDIA
|
|
|
|
|
Hi SreeKanth!
Look like lot's responsibility are on your Shoulder!
Anyway let's get back to your problem:-
Sreekanth Muralidharan wrote:
this required since a CFile::Open would return a handle to the file object. Could you please tell me what is the hidden functionality involved in CFile::Detach()/Attach().
actually you have mentioned that you are opening the file and passing the handle to CFile, so I make a guess you must be calling CFile::Attach File handle and CFile::Detach to detach file handle.
Sreekanth Muralidharan wrote:
any tool to analyze the thread process so that I could see the resource leaks inside the thread?
Process Viewer supplied with MS Visual Studio will help you track Thread opening/closing.
Sreekanth Muralidharan wrote:
We have no problem as said above in handling this bulk of data, but the single character acknowledgement fails quite too often.
As you very well know, Every HARDWARE device work on a well defined Set of commands.I had programmed hardware device only once i.e. Smartcards using Serial port communication. Try sending "C" instead of 'c' may be it's required NULL character to End Communication.
Sreekanth Muralidharan wrote:
Please read the mail in detail and your suggestions are very valuable not only for me but for the entire codeproject world.
That's a Great compliment, but i am stillin phase of learning. You can consider me beginner to Visual C++ world. you urself have more than 4 times exp then me
"Opinions are neither right nor wrong. I cannot change your opinion. I can, however, change what influences your opinion." - David Crow
cheers,
Alok Gupta
|
|
|
|
|
Hello Alok
Thanks a lot for the immediate response.
******
That's a Great compliment, but i am stillin phase of learning. You can consider me beginner to Visual C++ world. you urself have more than 4 times exp then me
******
Yaar, my experience well doesn't count and could not match your in depth knowledge of Windows programming, I really feel.
Regards,
Sreekanth Muralidharan,
Corporate Systems Consultant [Embedded Systems],
INDIA
|
|
|
|
|
Sreekanth Muralidharan wrote:
my experience well doesn't count and could not match your in depth knowledge of Windows programming, I really feel
That's greatest compliment I ever got and too from a programming GURU!. Well there is no alternative for EXPERIENCE atleast in INDIA!
"Opinions are neither right nor wrong. I cannot change your opinion. I can, however, change what influences your opinion." - David Crow
cheers,
Alok Gupta
|
|
|
|
|
Hi Alok
And one more thing to ask for.
I have a USB driver from FTDI. And the FTDI library functions and the user manual are very useful for calling the driver functions in your application. In the so called driver library, everything is ported to a DLL which is loaded at run time by the application. The exported DLL functions are ported individually by the application viz., write, read and blah blah blah... I have a pointer to all the functions loaded in the application. Using the pointer I am calling the functions. One of the pointers return a handle to the USB driver and the handle is used for read and write operations.
When I click STOP, I do not close the driver handle. When I click play again, I use the same handle to write to the file and it annoyingly blocks and I am into trouble.
Can you find out a solution to this?
Regards
Sreekanth Muralidharan,
Corporate Systems Consultant [Embedded Systems],
INDIA
|
|
|
|
|
HI Sreekanth,
Due to CodeProject Script Error, I have to write my reply third time
Anyways Please Check that Driver Handle is Still Valid after you call STOP.
Secondly, As you have mentioned that your program only get halt when you send single character to DRIVER otherwise it's work well with BULK DATA/String. May FTDI Library fails to add END of Command Character to the character, which help Driver determine that's it's receieved complete command and it can proceed execution.
"Opinions are neither right nor wrong. I cannot change your opinion. I can, however, change what influences your opinion." - David Crow
cheers,
Alok Gupta
|
|
|
|
|
Hi Alok.
I have a thread which is the only one thread in the application. The thread is started at a PLAY button click event and it continues till STOP button is pressed. A PAUSE button suspends the thread at a point. The PAUSE button next time resumes the thread from where it stopped. While the thread is running, can we inform the thread of the PAUSE event so that the thread can be suspended very fast without corrupting the data the thread is handling. The particular data the thread handles is very vital for the consistent operation of the application.
Please extend your invaluable help. The other problems are almost solved now.
||| SREEKANTH MURALIDHARAN |||,
Corporate Systems Consultant [Embedded Systems],
INDIA
|
|
|
|
|
Sreekanth Muralidharan wrote:
While the thread is running, can we inform the thread of the PAUSE event
There are two ways of doing this! but first let me know which type thread's are you using, i.e. UI based Thread (thread derived from CWinThread ) or Simple Worker thread created from _BeginThread or CreateThread or AfxBeginthread Function!
"Opinions are neither right nor wrong. I cannot change your opinion. I can, however, change what influences your opinion." - David Crow
cheers,
Alok Gupta
|
|
|
|
|
Hi Alok
I am using a worker thread created from AfxBeginThread which returns a CWinThread pointer.
Sreekanth Muralidharan,
Corporate Systems Consultant [Embedded Systems],
INDIA
|
|
|
|
|
HI Sreekanth!
here a small piece of code that will help you in PAUSING you application
CYourDlg
{
public:
--- your function---
--- your variable---
BOOL isStop;
BOOL isPause
}
CYourDlg::OpenThread()
{
this->isStop=FALSE;
this->isPause=FALSE;
AfxBeginThread(&yourWorkerThread,reinterpret_cast<LPVOID>(this));
}
UINT yourWorkerThread(LPVOID lpParam)
{
CYourDlg *m_Dlg=( CYourDlg *)lpParam;
while(!m_Dlg->isStop)
{
while(m_Dlg->isPause)
Sleep(20);
----Do you reading writing blah blah-------
}
return 0;
}
Tthis is just sample logic, you can use waitable Timer to get more accurate result!
"Opinions are neither right nor wrong. I cannot change your opinion. I can, however, change what influences your opinion." - David Crow
cheers,
Alok Gupta
|
|
|
|
|
Hi Alok
Thanks a lot for the prompt response. Will try testing your code and will revert back. If success, we will open BOTTLES !!!, Okay ?
Regards
Sreekanth Muralidharan,
Corporate Systems Consultant [Embedded Systems],
INDIA
|
|
|
|
|
Sreekanth Muralidharan wrote:
we will open BOTTLES !!!,
Sorry,I like to stay away from BOTTLES! but thanks for Invitation!
"Opinions are neither right nor wrong. I cannot change your opinion. I can, however, change what influences your opinion." - David Crow
cheers,
Alok Gupta
|
|
|
|
|
No probs !!
Regards,
Sreekanth Muralidharan,
Corporate Systems Consultant [Embedded Systems],
INDIA
|
|
|
|
|
Hi Alok
I think we have become real friends by now. Please help me again. How can you write a handler which is called when a thread is terminated? This is required to clean up things properly.
Regards
Sreekanth Muralidharan,
Corporate Systems Consultant [Embedded Systems],
INDIA
|
|
|
|
|
Hi SreeKanth,
Sreekanth Muralidharan wrote:
I think we have become real friends by now
Offcourse
Sreekanth Muralidharan wrote:
How can you write a handler which is called when a thread is terminated?
Let me demonstrate whole logic using above sample code ok!
HANDLE g_hThreadWaitHandle;
CYourDlg {
public:
--- your function---
--- your variable---
BOOL isStop;
BOOL isPause
}
CYourDlg::OnPLAY()
{
this->isPause=FALSE;
g_hThreadWaitHandle=CreateEvent(0,FALSE,FALSE,"SREEKANTH");
AfxBeginThread(&yourWorkerThread,reinterpret_cast(this));
}
CYourDlg::OnPAUSE()
{
this->isPause!= this->isPause;
}
CYourDlg::OnSTOP()
{
this->isSTOP= TRUE;
WaitForSingleObject(g_hThreadWaitHandle,INFINITE);
}
UINT yourWorkerThread(LPVOID lpParam)
{
CYourDlg *m_Dlg=( CYourDlg *)lpParam;
while(!m_Dlg->isStop)
{
while(m_Dlg->isPause)
Sleep(20);
----Do you reading writing blah blah-------
}
SetEvent(g_hThreadWaitHandle);
return 0;
}
"Opinions are neither right nor wrong. I cannot change your opinion. I can, however, change what influences your opinion." - David Crow
cheers,
Alok Gupta
|
|
|
|
|
Hi Alok
Thanks again for your valuable suggestion. I will try this part and give you the result.
By the way, are you working anywhere now? I think you my be having good experience in Windows programming domain, if I am not wrong.
Regards,
Sreekanth Muralidharan,
Corporate Systems Consultant [Embedded Systems],
INDIA
|
|
|
|
|
Sreekanth Muralidharan wrote:
Thanks again for your valuable suggestion.
Mention Not!!
Sreekanth Muralidharan wrote:
I will try this part and give you the result
Yeah i am waiting for that!
Sreekanth Muralidharan wrote:
are you working anywhere now?
Offering me A JOB
Sreekanth Muralidharan wrote:
I think you my be having good experience in Windows programming domain
You have to decide that!
"Opinions are neither right nor wrong. I cannot change your opinion. I can, however, change what influences your opinion." - David Crow
cheers,
Alok Gupta
|
|
|
|
|