2017-01-03 3 views
-3

私のプログラムでは、 "Vehicle"クラスのインスタンスを生成する必要があります。Vehicleクラスには、タイマーがヒットしたときにブール値「vExists」と共にその中にタイマーがあります。間隔。それは「this.vExists = false」に好きではないので、C#クラスインスタンスspecfic timer経過イベント

class VehicalGen 
{ 
    private Random rn1 = new Random(); 

    private int agrotime; 
    private bool vExists; 
    //static AgrivationHandler agroTimeHandler; 
    private static Timer agroTimer = new Timer(100); 

    //agrotime: anywhere between 2 and 4 seconds (2000 to 4000) 

    ///Generates the new Vehicles using GetCarType to change the outcome of the later variables 
    public VehicalGen() 
    { 
     this.vtype = RandomGeneration.GetCarType(rn1); 
     switch (this.vtype) 
     { 
      [....] //This is where the details of the vehicle are generated. 
     } 
     this.agrotime = RandomGeneration.GetRandomTime224(rn1); 
     this.vExists = true; 
     this.serviced = false; 
     //agroTimeHandler = new AgrivationHandler(agrotime); 
     agroTimer.Interval = agrotime; 
     agroTimer.Elapsed += AgroTimerElapsed; 
     agroTimer.Start(); 
    } 

    public static void AgroTimerElapsed(object sender, ElapsedEventArgs e) 
    { 
     //here is where I need to set vExists to "False" 
    } 
} 

しかし、私はそれだけでエラー、限り私はそれを行うために承知しているように、クラスの特定のインスタンスを生成する必要があります。

+0

コードを書いてここに投稿してください(ガイダンスについては[MCVE]を参照してください)。何かを試して(ジャンプ、歌う、走っている)助けに行くつもりはありません。 –

答えて

0

はい可能です。 以下を試してください:

private void LoadTimer() 
    Timer timer = new Timer 
    timer.Interval = 4000; 
    timer.Enabled = true; 
    timer.Tick += new EventHandler(timer1_Tick); 
    timer.Start(); 
} 

private void timer1_Tick(object sender, EventArgs e) 
{ 
    // do your boolean operation where 
} 
関連する問題