为什么计时器在显示新表格后停止工作?

时间:2016-12-05 17:38:33

标签: c# windows timer

我正在开发一个c#项目。在MainForm.cs我使用计时器。 60秒后,将显示计时器控制信息和新表格。

     private void timer1_Tick(object sender, EventArgs e)
     {
        timer1.Enabled = true;
        timer1.Interval = 60000;

        ///new message if exist

       ...Check Database Statement
            frm_newmessage frm_message = new frm_newmessage();
            frm_show.ShowDialog();
        }

    }

显示新消息表单并返回mainForm计时器后停止工作。

    private void MainForm_Load(object sender, EventArgs e)
    {
        timer1_Tick(sender, e);
    }

如何在关闭新邮件表单后重新启动计时器并返回MainForm

先谢谢

1 个答案:

答案 0 :(得分:1)

您需要在主窗体加载中启动计时器。 Tick事件处理程序将每X毫秒调用一次(其中X是您在计时器上设置Interval属性的任何内容)。当Tick事件发生时,您需要停止计时器,并在显示的对话窗口关闭后再次启动计时器。

import Foundation

// Soldiers have weapons and soldiers can fight

protocol Weapon {
    func fire()
}

protocol Soldier {
    associatedtype W: Weapon

    var weapon: W { get }

    func fight()
}

extension Soldier {
    func fight() { weapon.fire() }
}

// Here are some weapons

struct Rifle : Weapon {
    func fire() { print("Bullets away!") }
}

struct Rocket : Weapon {
    func fire() { print("Rockets away!") }
}

struct GrenadeLauncher : Weapon {
    func fire() { print("Grernades away!") }
}

// Here are some soldiers

struct Sniper : Soldier {
    var weapon = Rifle()
}

struct Infantryman : Soldier {
    var weapon = Rifle()
}

struct Artillaryman : Soldier {
    var weapon = Rocket()
}

struct Grenadier : Soldier {
    var weapon = GrenadeLauncher()
}

// Now I would like to have an army of soldiers but the compiler will not let me.
// error: protocol 'Soldier' can only be used as a generic constraint because it has Self or associated type requirements

class Army {
    var soldiers = [Soldier]()

    func join(soldier: Soldier) {
        soldiers.append(soldier)
    }

    func makeWar() {
        for soldier in soldiers { soldier.fight() }
    }
}

// So, let's try the type erasure technique:

struct AnySoldier<W: Weapon> : Soldier {
    var weapon: W
    private let _fight: () -> Void

    init<S: Soldier>(soldier: S) where S.W == W {
        _fight = soldier.fight
        weapon = soldier.weapon
    }

    func fight() { _fight() }
}

var s1 = AnySoldier(soldier: Sniper())
print (type(of: s1)) // AnySoldier<Rifle>
s1.fight() // Bullets away!
s1.weapon.fire() // Bullets away!
s1 = AnySoldier(soldier: Infantryman()) // Still good; Infantrymen use rifles
s1 = AnySoldier(soldier: Grenadier()) // Kaboom! Grenadiers do not use rifles


// So now I can have an army of Rifle wielding Soldiers

class Army {
    var soldiers = [AnySoldier<Rifle>]()

    func join(soldier: AnySoldier<Rifle>) {
        soldiers.append(soldier)
    }

    func makeWar() {
        for soldier in soldiers { soldier.fight() }
    }
}
let army = Army()
army.join(soldier: AnySoldier(soldier: Sniper()))
army.join(soldier: AnySoldier(soldier: Infantryman()))
army.join(soldier: AnySoldier(soldier: Grenadier())) // Kaboom! Rifles only
army.makeWar()

// But, what I really want is an army wherein the weapons are unrestricted.
class Army {
    var soldiers = [AnySoldier]()

    func join(soldier: AnySoldier) {
        soldiers.append(soldier)
    }

    func makeWar() {
        for soldier in soldiers { soldier.fight() }
    }
}