logging System Time

N

Newbie

Hi,

How do you retrieve the system date and time from the
Device (PocketPC or WinCE device) ?

Im writing a UDP sockets comms application and want to
write contents of each packet received/sent to a text
file along with the specific time of the communication.

Would this be API programming, if so will retrieving the
date time be fast enough? because my app may send/recieve
e.g 10 packets in 2 secs.

Any code examples?
Many Thanks.
 
N

NewBie

Hi,

Also, can anyone advise on how to handle writing to the
same text file from 2 different threads? Should I use a
dummy lock and ensure other threads obtain it before file
can be written to by other threads?

Many Thanks.
 
N

Newbie

Thanks Chris.

I should focus on using the help system thoroughly before
jumping to the newsgroup.

Any advice on writing to files from multiple threads?
..
 
P

Paul G. Tobey [eMVP]

The architecture usually is better if you write from a single thread,
sending messages from other threads, if they need to have information put in
the file. This automatically handles the serialization of the writes by
pushing them through a queue (the thread message queue for the writer
thread). What sort of application is it that requires multiple threads to
write to the same file?

Paul T.
 
N

Newbie

Hi Paul,

My app has a thread (A) that listens for packets received
(UDP comms) and a thread (B) that sends packets. Thread A
will call procedure "writepackettofile" to write all
received packets to a text file. I was thinking of
creating a new thread to execute "writepackettofile". The
same applies to Thread B, it will call the same procedure
in a new thread to write all the sent packets to a file.
Both threads write the packets content to the same file.

I DONT anticipate heavy traffic (of packets) ALL the time
but there certainly will be times of high traffic.

Is this bad practice? Whats the correct/best way.
 
P

Paul G. Tobey [eMVP]

I'd build a logging thread and have each source thread post some sort of a
message with the data it wants 'logged' to the logging thread. This will
handle the serialization for you without having to use mutexes, etc.

Paul T.
 
N

Newbie

Thanks for your swift replies.

Im a little confused when you say "post some sort of
message to the logging thread". What do you mean by this?
How do you send messages to other threads? At moment Im
using global variables that Thread A and B read.

Do you mean I use a Array in which I store the packets
and which the logging thread uses to write to the file?

Do you suggest that this logging thread run through the
whole duration of the application - because Thread A and
B are set up to run through the whole duration of the
application.

Many thanks.
 
P

Paul G. Tobey [eMVP]

Yes, if you expect to log data for the duration of the application, I'd run
that logging thread the whole time. You could run it at a lower priority so
that logging doesn't interfere with packet processing, if you like, too.

In this case, there's a disconnect between how you'd like to do things using
the Win32 API and how .NET CF will support you doing it. Sometimes I lose
track of which newsgroup I'm in; sorry about that. Using C/C++, you'd have
the OS create a message queue for you and use PostTheadMessage() to send the
logging information from the reader and writer threads to the logging
thread. In .NET CF, you may need to create a managed version of this
"message queue" which is thread-safe. The collection classes have the
necessary tools for doing this. Here's a paragraph from a recent post by
Alex F. on this topic:

Yes, the collection objects (List, Queue, Hashtable etc) have a property
called SyncRoot. When you use lock to protect access to a collection, make
sure you lock this SyncRoot and not the collection object itself. Most of
the collection objects also provide static method Synchronized() that return
a wrapper of the same class which is thread-safe, i.e. you don't need locks
when accessing it. Neither method will prevent you from modifying a
collection while another thread tries to enumerate it. To handle this
situation, you will need to set a lock on the collection object itself.

You'd use a Queue for this application and create some sort of object class
which gives the logging thread the necessary information to put the right
data in the file. The logging thread would read from the head, and the
reader and writer threads would add items to the end. Since I don't think
that you need to enumerate the collection, you can probably just use the
Synchronized() static method to get a wrapper which is thread safe and use
that from all of your threads, rather than using locking.

Paul T.
 
N

Newbie

Brilliant.

I'l try puting it to practice. Whats the/is there
a 'best' way to write to a file? My packets are stored
as Objects and its members are primitive data.

Many Thanks.
 
P

Paul G. Tobey [eMVP]

I guess that depends on the format of the file you're trying to create...

Paul T.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top