Click here to Skip to main content
15,886,199 members
Home / Discussions / C#
   

C#

 
GeneralRe: Error in printing Pin
future38399-Jun-10 14:20
future38399-Jun-10 14:20 
GeneralRe: Error in printing Pin
Luc Pattyn9-Jun-10 14:36
sitebuilderLuc Pattyn9-Jun-10 14:36 
QuestionExceptions From Threads Pin
JohnLBevan9-Jun-10 12:03
professionalJohnLBevan9-Jun-10 12:03 
AnswerRe: Exceptions From Threads Pin
Luc Pattyn9-Jun-10 12:52
sitebuilderLuc Pattyn9-Jun-10 12:52 
GeneralRe: Exceptions From Threads Pin
JohnLBevan10-Jun-10 1:52
professionalJohnLBevan10-Jun-10 1:52 
QuestionThreads, Static Fields & Abstract Classes Problem Pin
JohnLBevan9-Jun-10 11:53
professionalJohnLBevan9-Jun-10 11:53 
AnswerRe: Threads, Static Fields & Abstract Classes Problem Pin
Luc Pattyn9-Jun-10 12:43
sitebuilderLuc Pattyn9-Jun-10 12:43 
GeneralRe: Threads, Static Fields & Abstract Classes Problem Pin
JohnLBevan10-Jun-10 0:55
professionalJohnLBevan10-Jun-10 0:55 
Hey Luc,

that's a great idea; thanks again for your help.

For anyone following this thread (no pun intended), below is an example of the demo code, updated to use this idea (hopefully this agrees with what Luc's described - it definitely works as hoped).

class Example4
{
    public static void Test()
    {
        Base4 one = new SubClass4_1();
        Base4 two = new SubClass4_2();
        one.Go();
        two.Go();
        System.Threading.Thread.Sleep(20000); //give the threads time to do stuff before terminating
        one.Stop();
        two.Stop();
    }
}

abstract class Base4
{
    DateTime lastRun = DateTime.UtcNow;
    Timer timer = new Timer();
    public void Go()
    {
        timer.AutoReset = false; //run once (until manually enabled in Again())
        timer.Elapsed += new ElapsedEventHandler(OnElapsedTime);
        timer.Interval = GetInterval();
        timer.Enabled = true;
    }
    public void Stop()
    {
        timer.Close();
        timer.Dispose();
        //nb: code in thread runs to completion even after timer has been disposed of
    }
    private void OnElapsedTime(object source, ElapsedEventArgs e)
    {
        lastRun = DateTime.UtcNow;
        RunSubClassCode();
        Again();
    }

    private void Again()
    {
        //acount for delay caused by the app running
        int interval = GetIntervalAccountForProcessingTime();
        try //stops the "loop" when the timer is disposed of, without exception
        {
            timer.Interval = interval;
            timer.Enabled = true;//kick things off again
        }
        catch (ObjectDisposedException) { }

    }
    private int GetIntervalAccountForProcessingTime()
    {
        int interval = GetInterval();
        interval -= DateTime.UtcNow.Subtract(lastRun).Milliseconds;
        interval = Math.Max(interval, 1);                                   //if we've gone past the period, kick it off straight away
        //while (interval < GetInterval()) interval += GetInterval();       //if we want to wait for the next interval/slot, replace the above line with this code
        return interval;
    }
    protected abstract int GetInterval();
    protected abstract void RunSubClassCode();
}

class SubClass4_1 : Base4
{
    protected override void RunSubClassCode()
    {
        Console.WriteLine("1a" + DateTime.UtcNow.ToString("mm ss ffff"));//easy way to see when things are kicking off
        System.Threading.Thread.Sleep(2000);
        Console.WriteLine("1b");
    }
    protected override int GetInterval()
    {
        return 3000;
    }
}
class SubClass4_2 : Base4
{
    protected override void RunSubClassCode()
    {
        Console.WriteLine("2a" + DateTime.UtcNow.ToString("mm ss ffff"));//easy way to see when things are kicking off
        System.Threading.Thread.Sleep(2000);
        Console.WriteLine("2b");
    }
    protected override int GetInterval()
    {
        return 2000;
    }
}


Cheers,

JB
GeneralRe: Threads, Static Fields & Abstract Classes Problem Pin
Luc Pattyn10-Jun-10 1:05
sitebuilderLuc Pattyn10-Jun-10 1:05 
Questionmaking auto serial number for each build of project Pin
kasraa000980009-Jun-10 9:47
kasraa000980009-Jun-10 9:47 
AnswerRe: making auto serial number for each build of project Pin
#realJSOP9-Jun-10 10:02
mve#realJSOP9-Jun-10 10:02 
GeneralRe: making auto serial number for each build of project Pin
Jason Vetter9-Jun-10 10:11
Jason Vetter9-Jun-10 10:11 
GeneralRe: making auto serial number for each build of project Pin
#realJSOP9-Jun-10 23:45
mve#realJSOP9-Jun-10 23:45 
AnswerRe: making auto serial number for each build of project Pin
Tony Richards9-Jun-10 10:44
Tony Richards9-Jun-10 10:44 
QuestionHow to build reportviewer using existing dynamic dataset without report wizard Pin
roman_s9-Jun-10 8:24
roman_s9-Jun-10 8:24 
QuestionFor Multilingual Application Pin
Andy Rama9-Jun-10 7:59
Andy Rama9-Jun-10 7:59 
AnswerRe: For Multilingual Application Pin
Dimitri Witkowski9-Jun-10 8:50
Dimitri Witkowski9-Jun-10 8:50 
QuestionRe: For Multilingual Application Pin
Andy Rama10-Jun-10 6:07
Andy Rama10-Jun-10 6:07 
AnswerRe: For Multilingual Application Pin
Dimitri Witkowski10-Jun-10 6:16
Dimitri Witkowski10-Jun-10 6:16 
GeneralRe: For Multilingual Application Pin
Andy Rama10-Jun-10 6:18
Andy Rama10-Jun-10 6:18 
QuestionHow to return a collection from a method. Pin
Chiman19-Jun-10 6:28
Chiman19-Jun-10 6:28 
AnswerRe: How to return a collection from a method. Pin
Luc Pattyn9-Jun-10 6:50
sitebuilderLuc Pattyn9-Jun-10 6:50 
AnswerRe: How to return a collection from a method. Pin
Not Active9-Jun-10 6:57
mentorNot Active9-Jun-10 6:57 
GeneralRe: How to return a collection from a method. [modified] Pin
#realJSOP9-Jun-10 7:43
mve#realJSOP9-Jun-10 7:43 
GeneralRe: How to return a collection from a method. Pin
Not Active9-Jun-10 7:47
mentorNot Active9-Jun-10 7:47 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.