Today's Posts Follow Us On Twitter! TFL Members on Twitter  
Forum search: Advanced Search  
Navigation
Marketplace
  Members Login:
Lost password?
  Forum Statistics:
Forum Members: 24,256
Total Threads: 81,169
Total Posts: 566,839
There are 36 users currently browsing (tf).
 
  Our Partners:
 
  TalkFreelance     Design and Development     Programming     Other Programming Languages :

C# Running a function every x seconds

Thread title: C# Running a function every x seconds
Reply  
Page 1 of 2 1 2 >
    Thread tools Search this thread Display Modes  
07-18-2010, 08:11 PM
#1
geforce is offline geforce
Status: Member
Join date: Jul 2006
Location: UK
Expertise: Programming - PHP & MySQL
Software: Photoshop and Notepad++
 
Posts: 216
iTrader: 2 / 100%
 

geforce is on a distinguished road

Send a message via AIM to geforce

  Old  C# Running a function every x seconds

Hi,

I am wanting to run one function in C# every 5 seconds.
I've googled it and cant get my head round it, it needs to be the most efficient way too.

Any help please?

Reply With Quote
07-18-2010, 08:16 PM
#2
Village Genius is offline Village Genius
Village Genius's Avatar
Status: Geek
Join date: Apr 2006
Location: Denver, CO
Expertise: Software
Software: Chrome, Notepad++
 
Posts: 6,892
iTrader: 18 / 100%
 

Village Genius will become famous soon enough

  Old

Are you running IIS?

Reply With Quote
07-18-2010, 08:17 PM
#3
geforce is offline geforce
Status: Member
Join date: Jul 2006
Location: UK
Expertise: Programming - PHP & MySQL
Software: Photoshop and Notepad++
 
Posts: 216
iTrader: 2 / 100%
 

geforce is on a distinguished road

Send a message via AIM to geforce

  Old

No

Reply With Quote
07-18-2010, 08:49 PM
#4
cableguy is offline cableguy
Status: Member
Join date: Mar 2010
Location:
Expertise:
Software:
 
Posts: 156
iTrader: -1 / 0%
 

cableguy is an unknown quantity at this point

  Old

http://msdn.microsoft.com/en-us/libr...ers.timer.aspx

but as I said in the other thread, you might want to watch for filechanges via FileSystemWatcher class.

Reply With Quote
07-18-2010, 09:17 PM
#5
geforce is offline geforce
Status: Member
Join date: Jul 2006
Location: UK
Expertise: Programming - PHP & MySQL
Software: Photoshop and Notepad++
 
Posts: 216
iTrader: 2 / 100%
 

geforce is on a distinguished road

Send a message via AIM to geforce

  Old

Cheers cableguy looks like that should work, however I get the following error: Error 1 Program '....\ReadLines.exe' has more than one entry point defined: 'Timer1.Main()'. Compile with /main to specify the type that contains the entry point. .....\ReadLines\Timer.cs 8 24 ReadLines


Any ideas?

Reply With Quote
07-18-2010, 09:42 PM
#6
geforce is offline geforce
Status: Member
Join date: Jul 2006
Location: UK
Expertise: Programming - PHP & MySQL
Software: Photoshop and Notepad++
 
Posts: 216
iTrader: 2 / 100%
 

geforce is on a distinguished road

Send a message via AIM to geforce

  Old

Ahh nevermind, I sorted it. Looks like were in business
Will post my results after testing.

Thank you again to all who have helped with my problem(s).

Reply With Quote
07-19-2010, 12:45 AM
#7
geforce is offline geforce
Status: Member
Join date: Jul 2006
Location: UK
Expertise: Programming - PHP & MySQL
Software: Photoshop and Notepad++
 
Posts: 216
iTrader: 2 / 100%
 

geforce is on a distinguished road

Send a message via AIM to geforce

  Old

Another problem.

After a certain trigger is detected, it opens another form which for some reason just freezes and wont do anything.

There is absolutley nothing on it, just a standard windows form. What would be causing this?

EDIT:
I've found that disabling the timer stops this issue, What can I do about this?

Reply With Quote
07-19-2010, 02:20 AM
#8
cableguy is offline cableguy
Status: Member
Join date: Mar 2010
Location:
Expertise:
Software:
 
Posts: 156
iTrader: -1 / 0%
 

cableguy is an unknown quantity at this point

  Old

There's 2 types of timers. System.Timers and System.Windows.Forms timers. Sounds like you're using the wrong one, but without some code snippets, it's hard to diagnose.

Reply With Quote
07-19-2010, 02:46 AM
#9
geforce is offline geforce
Status: Member
Join date: Jul 2006
Location: UK
Expertise: Programming - PHP & MySQL
Software: Photoshop and Notepad++
 
Posts: 216
iTrader: 2 / 100%
 

geforce is on a distinguished road

Send a message via AIM to geforce

  Old

I've run the timer in a htread and that did not help either.

Code:
        public static System.Timers.Timer aTimer;

        public void Form1_Load(object sender, EventArgs e)
        {
            Thread thread = new Thread(new ThreadStart(Timer));
        }

        public static void Timer()
        {
            // Create a timer with a three second interval.
            aTimer = new System.Timers.Timer(3000);

            // Hook up the Elapsed event for the timer.
            aTimer.Elapsed += new ElapsedEventHandler(OnTimedEvent);

            // Set the Interval to 2 seconds (2000 milliseconds).
            aTimer.Interval = 2000;
            aTimer.Enabled = true;
        }
I'm not the best C# coder as I hardly use it, so be gentle lol.

Cheers

Reply With Quote
07-19-2010, 12:25 PM
#10
cableguy is offline cableguy
Status: Member
Join date: Mar 2010
Location:
Expertise:
Software:
 
Posts: 156
iTrader: -1 / 0%
 

cableguy is an unknown quantity at this point

  Old

things looks good above. you don't need the extra thread. the only thing i can tell is maybe the interval is too small so you're calling OnTimedEvent before it finishes?

Reply With Quote
Reply  
Page 1 of 2 1 2 >


Thread Tools
Display Modes

  Posting Rules  
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump:
 
  Contains New Posts Forum Contains New Posts   Contains No New Posts Forum Contains No New Posts   A Closed Forum Forum is Closed