473,804 Members | 3,958 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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

50 New Member
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 2156
madankarmukta
308 Contributor
Hi..

Did you tried debugging this functions..?

That may help you a lot..!!

Thanks!
Mar 13 '09 #2
bmerlover
50 New Member
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 Recognized Expert Specialist
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
bmerlover
50 New Member
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 findPacketHeade r. 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
10409
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 (pscp.exe, a secure file-copy app that's part of the excellent PuTTY SSH package). Getting pscp.exe to run properly was a piece of cake using the System.Diagnostics.Process class. The thing that I can't get to work is the ability to read...
4
5736
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 file is finished uploading but am not sure how to do it while file is uploading. Some sample code would be welcomed with open arms. Thank You.
3
1311
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 anyone remember the solvation I would be very greatful...
1
1575
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 service should recieve it as number of packets. the problem is: in the windows service when it detects that the data coming is a file it make a loop (which equals the number of packets the client will send the file by it, which will be determined...
2
7318
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 in System::Timers::Timer. My understanding of the next part is not very good, as my code may reveal, but as I understand it, my application is "unmanaged C++", whereas the timer extension from the system DLL is managed. Therefore I needed to use...
7
4664
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. ************************************************************ The class EmailPoller can be designed, but is not the first class in the file. Visual Studio requires that designers use the first class in the file. Move the class code so that it is the first class in the file and try...
4
1869
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 minutes. I'm currently using Microsoft Visual Studio 2003 and programming with VB.NET, does this feature exist? Thanks in advance guys and gals.
12
4498
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 within calls a Visual Basic input message box, which is synchronous, it waits until you enter some input, and press enter. But, this message box pops up every second regardless if the old ones have finished. The timer's Tick function is called over...
3
3891
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 Thread)? In some of the timers I update some UI controls e.g statusbar.labels and I
0
9585
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10586
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10338
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
10082
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
7622
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5525
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5658
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
3823
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2997
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.