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: -
void findPacketHeaders(char *inputFile, char *outputFile)
-
{
-
/* Open file */
-
fpInput = fopen(inputFile, "rb");
-
fpOutput = fopen(outputFile, "w");
-
-
/* check if fpInput exists */
-
if (!fpInput)
-
{
-
fprintf(stderr, "Unable to open file %s", inputFile);
-
return;
-
}
-
-
-
/* Read fpInput contents into buffer */
-
do
-
{
-
-
if (fgetc(fpInput) == 0x25 && fgetc(fpInput) == 0xEB)
-
{
-
-
getPacketHeaderSegments(fpInput, pktHeader, fpOutput);
-
}
-
-
}while(!feof(fpInput));
-
-
-
fclose(fpInput);
-
fclose(fpOutput);
-
}
-
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: -
-
-
void findPacketHeaders(char *inputFile, char *outputFile)
-
{
-
//FILE *fpInput, *fpOutput;
-
//packetHeader pktHeader;
-
-
pktHeader.packetSyncPattern = 0xEB25;
-
-
/* Open file */
-
fpInput = fopen(inputFile, "rb");
-
fpOutput = fopen(outputFile, "w");
-
-
/* check if fpInput exists */
-
if (!fpInput)
-
{
-
fprintf(stderr, "Unable to open file %s", inputFile);
-
return;
-
}
-
-
processTimer->Interval = 10;
-
processTimer->Enabled = true;
-
-
fclose(fpInput);
-
fclose(fpOutput);
-
-
}
-
-
private: System::Void processTimer_Tick(System::Object^ sender, System::EventArgs^ e)
-
{
-
/* Read fpInput contents into buffer */
-
richTextBox_console->AppendText(fgetc(fpInput).ToString("X") + "\n");
-
-
if (fgetc(fpInput) == 0x25 && fgetc(fpInput) == 0xEB)
-
{
-
-
getPacketHeaderSegments(fpInput, pktHeader, fpOutput);
-
}
-
-
if(feof(fpInput))
-
{
-
/* break out of this */
-
processTimer->Enabled = false;
-
}
-
-
}
-
-
[\CODE]
4 2156
Hi..
Did you tried debugging this functions..?
That may help you a lot..!!
Thanks!
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.
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.
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. -
-
void findPacketHeaders(char *inputFile, char *outputFile)
-
{
-
pktHeader.packetSyncPattern = 0xEB25;
-
-
/* Open file */
-
fpInput = fopen(inputFile, "rb");
-
fpOutput = fopen(outputFile, "w");
-
-
/* check if fpInput exists */
-
if (!fpInput)
-
{
-
fprintf(stderr, "Unable to open file %s", inputFile);
-
return;
-
}
-
-
processTimer->Interval = 10;
-
processTimer->Enabled = true;
-
-
}
-
-
private: System::Void processTimer_Tick(System::Object^ sender, System::EventArgs^ e)
-
{
-
/* Read fpInput contents into buffer */
-
-
if (fgetc(fpInput) == 0x25 && fgetc(fpInput) == 0xEB)
-
{
-
-
getPacketHeaderSegments(fpInput, pktHeader, fpOutput);
-
}
-
-
if(feof(fpInput))
-
{
-
/* break out of this */
-
processTimer->Enabled = false;
-
-
fclose(fpInput);
-
fclose(fpOutput);
-
-
}
-
-
}
-
-
Sign in to post your reply or Sign up for a free account.
Similar topics |
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...
|
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.
|
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...
|
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...
|
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...
| |
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...
|
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.
|
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...
|
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
|
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,...
|
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...
| |
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...
|
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...
|
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...
|
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...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
|
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |
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...
| |