473,385 Members | 1,642 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

How to read a file in a timer function in Visual C++ using .NET

How can you read a file in a timer function? I am currently using a do while statement to read a file which works fine. The implementation is in C but I've added the code to a C++ .NET framework GUI app. This is the implementation that works:

Expand|Select|Wrap|Line Numbers
  1. void findPacketHeaders(char *inputFile, char *outputFile)
  2. {
  3.     /* Open file */
  4.     fpInput = fopen(inputFile, "rb");
  5.     fpOutput = fopen(outputFile, "w");
  6.  
  7.     /* check if fpInput exists */
  8.     if (!fpInput)
  9.     {
  10.         fprintf(stderr, "Unable to open file %s", inputFile);
  11.         return;
  12.     }
  13.  
  14.  
  15.     /* Read fpInput contents into buffer */
  16.     do 
  17.     {
  18.  
  19.         if (fgetc(fpInput) == 0x25 && fgetc(fpInput) == 0xEB)
  20.         {
  21.  
  22.             getPacketHeaderSegments(fpInput, pktHeader, fpOutput);
  23.         }
  24.  
  25.     }while(!feof(fpInput));
  26.  
  27.  
  28.     fclose(fpInput);
  29.     fclose(fpOutput);
  30. }
  31.  
What I would like to do is implement this in a timer function so that the computer won't lag because the file I open is very large and when its reading the file with the do while, the CPU usage goes to 100% with no response although it does finish after a while. I have used timer functions in the passed to solve "while" issues but I can't seem to get it to read this file.

FILE *fpInput, *fpOutput;
packetHeader pktHeader;

are global variables and they do work correctly.

This is my implementation of the timer:

Expand|Select|Wrap|Line Numbers
  1.  
  2.  
  3. void findPacketHeaders(char *inputFile, char *outputFile)
  4. {
  5.     //FILE *fpInput, *fpOutput;
  6.     //packetHeader pktHeader;
  7.  
  8.     pktHeader.packetSyncPattern = 0xEB25;
  9.  
  10.     /* Open file */
  11.     fpInput = fopen(inputFile, "rb");
  12.     fpOutput = fopen(outputFile, "w");
  13.  
  14.     /* check if fpInput exists */
  15.     if (!fpInput)
  16.     {
  17.         fprintf(stderr, "Unable to open file %s", inputFile);
  18.         return;
  19.     }
  20.  
  21.     processTimer->Interval = 10;
  22.     processTimer->Enabled = true;
  23.  
  24.     fclose(fpInput);
  25.     fclose(fpOutput);
  26.  
  27. }
  28.  
  29. private: System::Void processTimer_Tick(System::Object^  sender, System::EventArgs^  e) 
  30.          {
  31.             /* Read fpInput contents into buffer */
  32.             richTextBox_console->AppendText(fgetc(fpInput).ToString("X") + "\n");
  33.  
  34.             if (fgetc(fpInput) == 0x25 && fgetc(fpInput) == 0xEB)
  35.             {
  36.  
  37.                 getPacketHeaderSegments(fpInput, pktHeader, fpOutput);                
  38.             }
  39.  
  40.             if(feof(fpInput))
  41.             {
  42.                 /* break out of this */
  43.                 processTimer->Enabled = false;
  44.             }
  45.  
  46.          }
  47.  
  48.  
[\CODE]
Mar 13 '09 #1
4 2136
madankarmukta
308 256MB
Hi..

Did you tried debugging this functions..?

That may help you a lot..!!

Thanks!
Mar 13 '09 #2
Well, I tried printing what fgetc outputs which is -1 all the time or the hex representation is FFFFFFFF. It seems like the fgetc isn't working correctly inside the timer function.
Mar 13 '09 #3
tlhintoq
3,525 Expert 2GB
Maybe you should read the file on a different thread and raise an event when it is done. Sure enough duct tape can fix anything, but patches on top of patches is never the way to go.
Mar 13 '09 #4
I found out what the problem was, when the timer started, the file was already closed because of the fclose statements at the bottom of findPacketHeader. So I rearranged the code to something similar to this. Thanks for replying, I appreciate the inputs.

Expand|Select|Wrap|Line Numbers
  1.  
  2. void findPacketHeaders(char *inputFile, char *outputFile) 
  3.     pktHeader.packetSyncPattern = 0xEB25; 
  4.  
  5.     /* Open file */ 
  6.     fpInput = fopen(inputFile, "rb"); 
  7.     fpOutput = fopen(outputFile, "w"); 
  8.  
  9.     /* check if fpInput exists */ 
  10.     if (!fpInput) 
  11.     { 
  12.         fprintf(stderr, "Unable to open file %s", inputFile); 
  13.         return; 
  14.     } 
  15.  
  16.     processTimer->Interval = 10; 
  17.     processTimer->Enabled = true; 
  18.  
  19.  
  20. private: System::Void processTimer_Tick(System::Object^  sender, System::EventArgs^  e)  
  21.          { 
  22.             /* Read fpInput contents into buffer */ 
  23.  
  24.             if (fgetc(fpInput) == 0x25 && fgetc(fpInput) == 0xEB) 
  25.             { 
  26.  
  27.                 getPacketHeaderSegments(fpInput, pktHeader, fpOutput);                 
  28.             } 
  29.  
  30.             if(feof(fpInput)) 
  31.             { 
  32.                 /* break out of this */ 
  33.                 processTimer->Enabled = false; 
  34.  
  35.                 fclose(fpInput); 
  36.                 fclose(fpOutput); 
  37.  
  38.             } 
  39.  
  40.          }
  41.  
  42.  
Mar 14 '09 #5

Sign in to post your reply or Sign up for a free account.

Similar topics

3
by: Al Cohen | last post by:
I'll start by warning that I'm a newbie to C# (but I've been programming for 25 years), so I may just be doing something reallyreally dumb. I'm writing a C# wrapper for a command-line application...
4
by: Kenneth Keeley | last post by:
Hi, I have a page that uploads files to my server and I wish to display a "Please wait while uploading" page to the user while the file is uploading. I have been able to redirect the user once the...
3
by: zurg | last post by:
As far as I remember somewhere here was posted a question about Timer that was stoping at midnight(0.00 a.m.). I'm going to face this problem tommorow (implemeting a long working timer) so if...
1
by: mulham.haffar | last post by:
hi guys.. im writing an application that uses windows service to listen (as a tcplistener) for any data sent (by a tcpclient) ... one kind of the requests might be a file sent by client and the...
2
by: zamir.khan | last post by:
Hello all, New to the groups, sorry if this the wrong forum/etiquette. I am coding a c++ application that requires the use of a timer-triggered event handler. I decided to use the timer provided...
7
by: tshad | last post by:
I have a problem with a VS 2003 project. This project was designed and works fine in VS 2003. But trying to open the project I get the following error....
4
by: Musty | last post by:
hi there I've just finished programming an application that is a 20 question multiple-choice test - but would very much like a timer that enables it so shut down automatically after say, 15 or 10...
12
by: Zytan | last post by:
I have a Timer class set to trigger every second. The Tick function that is called every second uses a lock to prevent multiple ticks from executing the same code at the same time. The code...
3
by: Steve | last post by:
Hi All I am using VB.net 2008 and use timer controls within my applications Question Does the code in a Timer control.tick event run on a different thread to the main Application thread (UI...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.