Newbie Stupid Question On Timers

A

Andy Straw

Hi, I've been going nuts trying to work out how to do this. I've got an
array list and I've worked out how to get a timer working but I want to
actually use the Timer to do operations on the ArrayList. How do I pass
it a refernce to the arraylist I create?

Any help would be fantastic. TIA

Andy

using System;
using System.Timers;
using System.Collections;

namespace Testing03
{
/// <summary>
/// Summary description for Class1.
/// </summary>
///
class Class1
{


[STAThread]
static void Main(string[] args)
{
ArrayList list1 = new ArrayList();

list1.Add("foo");
list1.Add("bar");

foreach (String s in list1)
{
Console.WriteLine("String...." + s);
}

Timer RefreshTimer = new Timer();

RefreshTimer.Elapsed +=
new ElapsedEventHandler(RefreshTimer_Elapsed);

RefreshTimer.Interval = 10000; //10 secs
RefreshTimer.Enabled = true;

Console.WriteLine("Press Enter to exit....");
Console.Read();

// Keep the timer alive until the end of Main.
GC.KeepAlive(RefreshTimer);
}

private static void
RefreshTimer_Elapsed(object sender, ElapsedEventArgs e)
{
Console.WriteLine("Timer Event");

// really want to do something with list1 here!!
// i.e get rid of the comments around this
// following code block

/*
foreach (String s in list1)
{
Console.WriteLine("String...." + s);
}
*/
}
}
}
 
L

Lars Behrmann

Hi Andy,

you have to declare the ArrayList list1 as a private static meber
of this class. Then the timer method which is called could access
the list.

......
private static ArrayList list1;

[STAThread]
static void Main(string[] args)
{
list1 = new ArrayList();
....

Cheers
Lars Behrmann

Nothing is impossible. UML is the key for all your problems.
AODL - Make your .net apps OpenOffice ready
http://aodl.sourceforge.net/
 
J

James

Try declaring your array list at the Class level i.e.:

class Class1
{
ArrayList list1 = new ArrayList();

[STAThread]
static void Main(string[] args)
 
A

Andy Straw

Woohoo... fantastic! Thanks Lars. Shouldn't have skipped first few
chapters of my text book.

Lars said:
Hi Andy,

you have to declare the ArrayList list1 as a private static meber
of this class. Then the timer method which is called could access
the list.

.....
private static ArrayList list1;

[STAThread]
static void Main(string[] args)
{
list1 = new ArrayList();
...

Cheers
Lars Behrmann

Nothing is impossible. UML is the key for all your problems.
AODL - Make your .net apps OpenOffice ready
http://aodl.sourceforge.net/


Andy Straw schrieb:

Hi, I've been going nuts trying to work out how to do this. I've got an
array list and I've worked out how to get a timer working but I want to
actually use the Timer to do operations on the ArrayList. How do I pass
it a refernce to the arraylist I create?

Any help would be fantastic. TIA

Andy

using System;
using System.Timers;
using System.Collections;

namespace Testing03
{
/// <summary>
/// Summary description for Class1.
/// </summary>
///
class Class1
{


[STAThread]
static void Main(string[] args)
{
ArrayList list1 = new ArrayList();

list1.Add("foo");
list1.Add("bar");

foreach (String s in list1)
{
Console.WriteLine("String...." + s);
}

Timer RefreshTimer = new Timer();

RefreshTimer.Elapsed +=
new ElapsedEventHandler(RefreshTimer_Elapsed);

RefreshTimer.Interval = 10000; //10 secs
RefreshTimer.Enabled = true;

Console.WriteLine("Press Enter to exit....");
Console.Read();

// Keep the timer alive until the end of Main.
GC.KeepAlive(RefreshTimer);
}

private static void
RefreshTimer_Elapsed(object sender, ElapsedEventArgs e)
{
Console.WriteLine("Timer Event");

// really want to do something with list1 here!!
// i.e get rid of the comments around this
// following code block

/*
foreach (String s in list1)
{
Console.WriteLine("String...." + s);
}
*/
}
}
}
 
A

Andy Straw

Thanks for your help, got that one nailed. No doubt more stupid
questions to come!

Cheers,

Andy
Try declaring your array list at the Class level i.e.:

class Class1
{
ArrayList list1 = new ArrayList();

[STAThread]
static void Main(string[] args)



Hi, I've been going nuts trying to work out how to do this. I've got an
array list and I've worked out how to get a timer working but I want to
actually use the Timer to do operations on the ArrayList. How do I pass it
a refernce to the arraylist I create?

Any help would be fantastic. TIA

Andy

using System; using System.Timers;
using System.Collections;

namespace Testing03
{
/// <summary>
/// Summary description for Class1.
/// </summary>
///
class Class1
{


[STAThread]
static void Main(string[] args)
{
ArrayList list1 = new ArrayList();

list1.Add("foo");
list1.Add("bar");

foreach (String s in list1)
{
Console.WriteLine("String...." + s);
}

Timer RefreshTimer = new Timer();

RefreshTimer.Elapsed +=
new ElapsedEventHandler(RefreshTimer_Elapsed);

RefreshTimer.Interval = 10000; //10 secs
RefreshTimer.Enabled = true;

Console.WriteLine("Press Enter to exit....");
Console.Read();

// Keep the timer alive until the end of Main.
GC.KeepAlive(RefreshTimer);
}

private static void
RefreshTimer_Elapsed(object sender, ElapsedEventArgs e)
{
Console.WriteLine("Timer Event");

// really want to do something with list1 here!!
// i.e get rid of the comments around this
// following code block

/*
foreach (String s in list1)
{
Console.WriteLine("String...." + s);
}
*/ }
}
}
 
J

James Curran

The real trick is that you don't want to use the Timer class in
System.Timers at all. You really want to use the Timer in System.Threading:

using System;
using System.Threading;
using System.Collections;

namespace Testing03
{
/// <summary>
/// Summary description for Class1.
/// </summary>
///
class Class1
{
static void Main(string[] args)
{
ArrayList list1 = new ArrayList();

list1.Add("foo");
list1.Add("bar");

foreach (String s in list1)
{
Console.WriteLine("String...." + s);
}

Timer RefreshTimer = new Timer(new
TimerCallback(RefreshTimer_Callback),list1,10000, 10000);

Console.WriteLine("Press Enter to exit....");
Console.Read();

// Keep the timer alive until the end of Main.
GC.KeepAlive(RefreshTimer);
}

private static void
RefreshTimer_Callback(object listObj)
{
Console.WriteLine("Timer Event");

ArrayList list1 = listObj as ArrayList;
foreach (String s in list1)
{
Console.WriteLine("String...." + s);
}
}
}
 
N

news.ntlword.com

James said:
The real trick is that you don't want to use the Timer class in
System.Timers at all. You really want to use the Timer in System.Threading:

James,

Thanks. I'm not sure what the advantages are, does this mean I could
give me multiple timer events all in different threads?

Cheers,

Andy
using System;
using System.Threading;
using System.Collections;

namespace Testing03
{
/// <summary>
/// Summary description for Class1.
/// </summary>
///
class Class1
{
static void Main(string[] args)
{
ArrayList list1 = new ArrayList();

list1.Add("foo");
list1.Add("bar");

foreach (String s in list1)
{
Console.WriteLine("String...." + s);
}

Timer RefreshTimer = new Timer(new
TimerCallback(RefreshTimer_Callback),list1,10000, 10000);

Console.WriteLine("Press Enter to exit....");
Console.Read();

// Keep the timer alive until the end of Main.
GC.KeepAlive(RefreshTimer);
}

private static void
RefreshTimer_Callback(object listObj)
{
Console.WriteLine("Timer Event");

ArrayList list1 = listObj as ArrayList;
foreach (String s in list1)
{
Console.WriteLine("String...." + s);
}
}
}


Hi, I've been going nuts trying to work out how to do this. I've got an
array list and I've worked out how to get a timer working but I want to
actually use the Timer to do operations on the ArrayList. How do I pass
it a refernce to the arraylist I create?

Any help would be fantastic. TIA

Andy

using System;
using System.Timers;
using System.Collections;
 
A

Andy Straw

Oh I see, then I can include it in the passed parameters and don't have
to have it as a class static member... yes, I think that maybe even
neater! Many thanks again.

James said:
The real trick is that you don't want to use the Timer class in
System.Timers at all. You really want to use the Timer in System.Threading:

using System;
using System.Threading;
using System.Collections;

namespace Testing03
{
/// <summary>
/// Summary description for Class1.
/// </summary>
///
class Class1
{
static void Main(string[] args)
{
ArrayList list1 = new ArrayList();

list1.Add("foo");
list1.Add("bar");

foreach (String s in list1)
{
Console.WriteLine("String...." + s);
}

Timer RefreshTimer = new Timer(new
TimerCallback(RefreshTimer_Callback),list1,10000, 10000);

Console.WriteLine("Press Enter to exit....");
Console.Read();

// Keep the timer alive until the end of Main.
GC.KeepAlive(RefreshTimer);
}

private static void
RefreshTimer_Callback(object listObj)
{
Console.WriteLine("Timer Event");

ArrayList list1 = listObj as ArrayList;
foreach (String s in list1)
{
Console.WriteLine("String...." + s);
}
}
}


Hi, I've been going nuts trying to work out how to do this. I've got an
array list and I've worked out how to get a timer working but I want to
actually use the Timer to do operations on the ArrayList. How do I pass
it a refernce to the arraylist I create?

Any help would be fantastic. TIA

Andy

using System;
using System.Timers;
using System.Collections;
 

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