Click here to Skip to main content
15,888,984 members
Home / Discussions / C#
   

C#

 
QuestionError in printing Pin
future38399-Jun-10 13:45
future38399-Jun-10 13:45 
AnswerRe: Error in printing Pin
Luc Pattyn9-Jun-10 14:04
sitebuilderLuc Pattyn9-Jun-10 14:04 
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 
Hi Folks,

I'm hoping someone can help me with an issue I've just come across.

I’ve written the demo code below which shows that if I use a timer to schedule events, unless I use a static “sync” variable, a second instance of a class may begin running before the first has completed. Rather than relying on folk remembering and repeating this logic, I’m hoping to put it into an abstract base class, which then calls a method in the subclass to do the processing, safe in the knowledge that another instance of that same class won’t run until this one’s completed. However, if there’s another class deriving from this base class, I’d want instances of the new class to be unaffected by instances of the first subclass (i.e. effectively I want the sync field to be static in the subclass, but available to the base class).

class Program
{
    static void Main(string[] args)
    {
        Base one = new SubClass1();
        Base two = new SubClass2();
        Timer timer1 = new Timer();
        Timer timer2 = new Timer();
        timer1.Interval = 3000;
        timer1.Elapsed += new ElapsedEventHandler(one.Run);
        timer1.Enabled = true;
        timer2.Interval = 5000;
        timer2.Elapsed += new ElapsedEventHandler(two.Run);
        timer2.Enabled = true;System.Threading.Thread.Sleep(20000);
        Console.ReadKey();//allows the program to stay alive whilst the threads continue
    }
}
abstract class Base
{
    private volatile static bool sync = false;
    private static object syncroot = string.Empty;
    public void Run(object source, ElapsedEventArgs e)
    {
        if (sync) return;
        lock (syncroot)
        {
            if (sync) return;
            sync = true;
        }
        RunSubClassCode();
        sync = false;
    }
    protected abstract void RunSubClassCode();
}
class SubClass1: Base
{
    protected override void RunSubClassCode()
    {
        Console.WriteLine("1a");
        System.Threading.Thread.Sleep(2000);
        Console.WriteLine("1b");
    }
}
class SubClass2 : Base
{
    protected override void RunSubClassCode()
    {
        Console.WriteLine("2a");
        System.Threading.Thread.Sleep(2000);
        Console.WriteLine("2b");
    }
}


Has anyone come across a similar requirement before, or can you think of any solutions?

Thanks in advance (and again, later),

JB
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 
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 

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.