newbie's first question(s)- threads and beeps

  • Thread starter Thread starter Keith
  • Start date Start date
K

Keith

I have experience with VBA (o97), but just got my first XP
box and dotnet, because I need to set up a data collection
program on an IPaq.

My goal is to have a small data collection program (user
selects info from radiobuttons, listboxes, etc) but to
also have the program beep every 15 minutes to signify the
beginning of a new data collection period. So far, it
appears that I need to use a thread(?) to accomplish the
timed beeping.

I've never used threads before, but I think I understand
them at the highest conceptual level- I (the main thread)
keep working but I send my assistant out to do some other
tasks, like watching a stopwatch and hitting me every 15
minutes to keep me awake. The problem is that the code
examples (for a non-programmer) are kinda confusing- all
the samples I've found jump into many lines of code, too
much for me to pick apart and figure out what the minimum
code I need to do this would be.

Does anyone have any references to much simplified
(threads for dummies) snippets?

The overall power consumption of the solution will matter,
as the device needs to last as long as possible, even
small gains in total runtime before running down the
battery could be significant. Not sure if this affects
what kind of thread solution I should implement.

Many thanks,
Keith
 
The need for power management means just spawning a worker thread for a
15-minute timer is probably out - the timer gets "paused" if the device goes
to sleep. You'll need to use something like CeRunAppAtTime or
CeSetUserNotification. These are dfairly easy to implement using the
OpenNETCF Notifications library, but I'm not aware of any samples as of yet.

-Chris
 
If you need help the beep functionality the following code should help:

Chris Craft,
http://www.cjcraft.com/

[DllImport("Coredll.dll")]
private static extern void MessageBeep(int Flags);

private const int MB_ICONASTERISK = 0x40;
private const int MB_ICONEXCLAMATION = 0x30;
private const int MB_ICONHAND = 0x10;
private const int MB_ICONQUESTION = 0x20;
private const int MB_OK = 0x0;

public void Beep()
{
MessageBeep(MB_OK);
}
 
you could use timer, couldn't you ?

any way a threads are very simple, which is a bit more difficult is
understand which function is not thread safe ;-)
(but don't worry in your case)

I will explain you some pseudo code out of my mind (don't have the API
graved in it)

you just need to have a thread which loop for ever, sleeping 15 seconds and
doing a beep.

a thread is initialized with a ThreadStart, a delegate, a method, which it
will call in ..... a separate thread :-)

just an additional things, it's better to make your thread background or
daemon (can't remember which one is .NET terminology) (RTFM)

roughly, in pseudo code, it's like this:

static void Main()
{
Thread t = new Thread(new ThreadStart(DoBeep)); // create
t.Background = true; // make it daemon
t.Start(); /// now it's running in parralel

// the rest of your program
}
static void DoBeep()
{
while(true)
{
Thread.Sleep(15000);
MessageBeep(MB_ICONASTERISK);
}
}

simple as that !
 
Back
Top