auto dispose objects?

T

Tahir

Hi,
i am develoing a wireless network simulator for my university thesis. the
thing i cannot do is the message transmission. the transmitter releases a
message to air, and the receivers in the range grab it. i have a message
object that is saved into an array and can be visible to nearby devices.
this message should be visible for a short time. how can i make the object
auto disposed in a default timeout?
i need a quick help pls, thanks,
 
T

Tom Shelton

Hi,
i am develoing a wireless network simulator for my university thesis. the
thing i cannot do is the message transmission. the transmitter releases a
message to air, and the receivers in the range grab it. i have a message
object that is saved into an array and can be visible to nearby devices.
this message should be visible for a short time. how can i make the object
auto disposed in a default timeout?
i need a quick help pls, thanks,
---

Well, I might change the design a bit and instead of an array, you could have
a collection that monitors the contents (maybe a background thread), removing
and disposing anything older then a certain amount.
 
S

Scott M.

I assume each "message" object that is placed in your array has a timestamp
property, that indicates the creation time of the message?

If so, you could scan your array, at regular intervals (however long your
message "timeout" period should be), iterating over each message object,
looking for messages that were created prior to the current time minus your
timeout.

-Scott
 
G

Gregory A. Beamer

i am develoing a wireless network simulator for my university thesis.
the thing i cannot do is the message transmission. the transmitter
releases a message to air, and the receivers in the range grab it. i
have a message object that is saved into an array and can be visible
to nearby devices. this message should be visible for a short time.
how can i make the object auto disposed in a default timeout?

I don't see any reason to save an array of messages for a wireless
network simulator, unless you are trying to show what is moving across
the wire. Unless I am missing something, I would consider rethinking to
a system that is more event handler based (message based).

As for expiring out of an array, you can custom "garbage collect" by
looking at when the message entered the system. A Queue can also work by
deleting first in at a specific time interval. I do agree with the idea
of a collection of messages, but you need some type of order to your
collection, so you know the age of the message.

Without further understanding of the project, I cannnot really help much
more.

Peace and Grace,



--
Gregory A. Beamer (MVP)

Twitter: @gbworld
Blog: http://gregorybeamer.spaces.live.com

*******************************************
| Think outside the box! |
*******************************************
 
P

Peter Duniho

Tom said:
Well, I might change the design a bit and instead of an array, you could have
a collection that monitors the contents (maybe a background thread), removing
and disposing anything older then a certain amount.

To elaborate on Tom's suggestion (which IMHO is a good way to go):

Instead of the array, use a Queue<T> to manage the collection. You can
call ToArray() on the queue in order to do whatever processing is
necessary to make existing messages visible to nearby devices. The key
of using Queue<T> is that it's implemented efficiently, as a circular
buffer, and you can pull message instances from the front of the queue
until the first message available is young enough to be kept.

Use a dedicated thread to deal with removing messages at the appropriate
time, or just check the queue every so often (e.g. once a second).

For example (warning: uncompiled, untested):

// Keep the messages ten seconds
const TimeSpan ktsTimeToLive = new TimeSpan(0, 0, 10);

struct Message
{
public DateTime Expires { get; private set; }
public string Message { get; private set; }

public Message(String strMessage)
{
Message = _strMessage;
Expires = DateTime.Now + ktsTimeToLive;
}
}

class MessageManager
{
private Queue<Message> _qmsg = new Queue<Message>();
private readonly object _objLock = new object();
private bool _fDone;
private bool _fStarted;

public void Start()
{
lock (_objLock)
{
if (_fStarted)
{
throw new InvalidOperationException("Manager is already
started");
}

new Thread(_ExpireMessages).Start();
_fStarted = true;
_fDone = false;
}
}

public void Stop()
{
lock (_objLock)
{
_fDone = true;
Monitor.Pulse(_objLock);
}
}

public void Add(Message msg)
{
lock (_objLock)
{
if (!_fStarted)
{
throw new InvalidOperationException("Manager has not been
started");
}

if (_fDone)
{
throw new InvalidOperationException("Manager has been stopped");
}

_qmsg.Enqueue(msg);

// Only need to wake up monitoring thread if the queue
// was empty. Otherwise, it will wake up on its own.
if (_qmsg.Count == 1)
{
Monitor.Pulse(_objLock);
}
}
}

public Message[] Messages
{
get { lock (_objLock) return _qmsg.ToArray(); }
}

private void _ExpireMessages()
{
lock (_objLock)
{
while (!_fDone)
{
// Remove any messages that have expired
while (_qmsg.Count > 0 &&
_qmsg.Peek().Expires < DateTime.Now)
{
_qmsg.Dequeue();
}

if (_qmsg.Count > 0)
{
// Wait until the next expiration time
Monitor.Wait(_objLock, _qmsg.Peek().Expires - DateTime.Now);
}
else
{
// Wait until the queue has contents again
Monitor.Wait(_objLock);
}
}

_fStarted = false;
}
}
}

Note that the above assumes that all messages expire at the same amount
of time after they are created. If different messages can have
different amounts of time they stay alive, then you'll either need to
use an ordered list instead of a queue, or you'll need a different queue
for each time delta possible (only practical if there are only a small
number of different deltas possible).

Pete
 

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