在运行时更改 messageTimer.Interval 即使 messageTimer 在另一个方法中

问题描述

我想在运行时更改调度程序时间的间隔 这是我的代码

InitializeComponent();
        dispatcherTimer messageTimer = new dispatcherTimer();
        messageTimer.Tick += new EventHandler(messageTimer_Tick);
        messageTimer.Interval =  TimeSpan.FromSeconds(1);
        messageTimer.Start();

idk如何在运行时改变dispatcherTimer的时间间隔

解决方法

我创建了两个文本块和一个按钮。

  1. 文本块:x:Name="txt_Count"

  2. 文本块:x:Name="txt_TimeNow"

  3. 按钮:x:Name="btn_changeTime_s"

    import random
    import pygame
    
    # --- constants --- (UPPER_CASE_NAMES)
    
    GREEN1 = (0,255,0)  # Healthy cells
    RED = (255,0)  # Infected cells
    GREEN2 = (0,100,0)  # Healthy cells not susecptible
    BLACK = (0,0)  # Dead cells
    WHITE = (255,255)
    
    BACKGROUND_COLOR = (225,198,153)
    
    SCREEN_SIZE = (800,800)
    
    # --- classes --- (CamelCaseNames)
    
    # class keeep only one cell so it should has name `Cell` instead of `Cells`
    
    class Cell(pygame.sprite.Sprite):
    
        def __init__(self,color,speed,width,height):
            super().__init__()
            
            self.color = color
            self.speed = speed
            
            self.image = pygame.Surface([width,height])
            self.image.fill(WHITE)
            self.image.set_colorkey(WHITE)
            
            self.radius = width//2 # 25 
            center = [width//2,height//2]
            pygame.draw.circle(self.image,self.color,center,self.radius,width=0)
    
            self.rect = self.image.get_rect()
            self.rect.x = random.randint(0,400)
            self.rect.y = random.randint(50,700)
    
        def update(self):
            self.rect.x += random.randint(-10,10)
            self.rect.y += random.randint(-10,10)
    
    # --- functions --- (lower_case_names)
    
    # empty
    
    # --- main --- (lower_case_names)
    
    pygame.init()
    
    screen = pygame.display.set_mode(SCREEN_SIZE)
    pygame.display.set_caption("Covid-19 Simualtion")
    
    speed = [0.5,-0.5]
    
    # - objects - 
    
    all_cells = pygame.sprite.Group()  # PEP8: lower_case_name
    
    for _ in range(5):
        cell = Cell(GREEN1,5,50,50)     # PEP8: lower_case_name
        all_cells.add(cell)
    
    # - loop -
    
    clock = pygame.time.Clock()
    
    end = False   
    while not end:
    
        # - events -
        
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                end = True
    
        # - upadates (without draws) -
        
        all_cells.update()
    
        # - draws (without updates) -
            
        screen.fill(BACKGROUND_COLOR)
        pygame.draw.rect(screen,BLACK,(0,400,700),3)
        
        all_cells.draw(screen)
    
        pygame.display.flip()
        clock.tick(30)  # to use less CPU
    
    # - end
    
    pygame.quit()  # some system may need it to close window
    

如果需要在后台修改Main UI Thread,使用语法

     int count = 0;
     DispatcherTimer messageTimer;
     DispatcherTimer TimeNow;
     public MainWindow()
     {
         InitializeComponent();

         messageTimer = new DispatcherTimer();
         messageTimer.Tick += new EventHandler(messageTimer_Tick);
         messageTimer.Interval = TimeSpan.FromSeconds(1);
         messageTimer.Start();

         TimeNow = new DispatcherTimer();
         TimeNow.Tick += new EventHandler(TimeNow_Tick);
         TimeNow.Interval = TimeSpan.FromMilliseconds(100);
         TimeNow.Start();
     }

     private void TimeNow_Tick(object sender,EventArgs e)
     {
         txt_now.Text = DateTime.Now.ToString();
     }

     private void messageTimer_Tick(object sender,EventArgs e)
     {
         txt_Count.Text = count.ToString();
         count++;
     }

     private void btn_changeTime_s_Click(object sender,RoutedEventArgs e)
     {
         messageTimer.Interval = TimeSpan.FromSeconds(2);
     }