Click here to Skip to main content
15,903,856 members
Home / Discussions / C#
   

C#

 
Questioncrystal reports and c# (report directly with parameters). Pin
Absoluto9-Jun-10 22:10
Absoluto9-Jun-10 22:10 
AnswerRe: crystal reports and c# (report directly with parameters). Pin
Adam R Harris10-Jun-10 5:52
Adam R Harris10-Jun-10 5:52 
QuestionIntelliSense bug in VS2005 Pin
Mc_Topaz9-Jun-10 21:54
Mc_Topaz9-Jun-10 21:54 
AnswerRe: IntelliSense bug in VS2005 Pin
#realJSOP10-Jun-10 4:29
professional#realJSOP10-Jun-10 4:29 
GeneralRe: IntelliSense bug in VS2005 Pin
Mc_Topaz10-Jun-10 4:38
Mc_Topaz10-Jun-10 4:38 
GeneralRe: IntelliSense bug in VS2005 Pin
dybs12-Jun-10 10:19
dybs12-Jun-10 10:19 
Question.net framework 4 Pin
michaelgr19-Jun-10 20:46
michaelgr19-Jun-10 20:46 
AnswerRe: .net framework 4 Pin
Abhinav S9-Jun-10 21:01
Abhinav S9-Jun-10 21:01 
AnswerRe: .net framework 4 Pin
Pete O'Hanlon9-Jun-10 21:38
mvePete O'Hanlon9-Jun-10 21:38 
QuestionMicrosoft sync framework Pin
hadad9-Jun-10 20:37
hadad9-Jun-10 20:37 
Questionhow to recognize the identityfing code in the image Pin
yu-jian9-Jun-10 20:16
yu-jian9-Jun-10 20:16 
QuestionTextureBrush problem Pin
Xmen Real 9-Jun-10 17:41
professional Xmen Real 9-Jun-10 17:41 
AnswerRe: TextureBrush problem Pin
Luc Pattyn9-Jun-10 17:58
sitebuilderLuc Pattyn9-Jun-10 17:58 
GeneralRe: TextureBrush problem Pin
Xmen Real 10-Jun-10 4:31
professional Xmen Real 10-Jun-10 4:31 
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 

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.