SAP 使用cl_gui_timer自动刷新屏幕的用法详解

2023-10-13 16:13:12 浏览数 (2)

SAP 使用cl_gui_timer自动刷新屏幕的用法详解

这个类在初始化的时候会设置一个定时间隔,每隔这个时间就会触发一次FINISHED事件。利用这个类的特性,可以实现很多东西,让程序自动化起来

比如定时刷新ALV,但是这个事件不能触发PBO ,此时需要去强制触发PAI,让程序走一次PBO,如果不涉及屏幕字段啥的,只是刷新ALV就不必要了,直接调用REFRESH就行了

代码语言:javascript复制
*&---------------------------------------------------------------------*
*& Report Y_XB_TEST1
*&---------------------------------------------------------------------*
*&
*&---------------------------------------------------------------------*
REPORT Y_XB_TEST1 NO STANDARD PAGE HEADING.

CLASS lcl_event_handler DEFINITION DEFERRED.

DATA: go_timer    TYPE REF TO cl_gui_timer,

      go_evt_hndl TYPE REF TO lcl_event_handler,

      gv_datum    TYPE sy-datum,
      gv_uzeit    TYPE sy-uzeit.

*&---------------------------------------------------------------------*

*&      START-OF-SELECTION

*&---------------------------------------------------------------------*

START-OF-SELECTION.
  gv_datum = sy-datum.
  gv_uzeit = sy-uzeit.

  PERFORM list_output.

  PERFORM init_timer.

*&---------------------------------------------------------------------*

*&      USER-COMMAND

*&---------------------------------------------------------------------*

AT USER-COMMAND.

  IF sy-ucomm = 'RFSH'.
    sy-lsind = sy-lsind - 1.

    PERFORM list_output.
  ENDIF.

*&---------------------------------------------------------------------*

*&      CLASS lcl_event_handler DEFINITION

*&---------------------------------------------------------------------*

CLASS lcl_event_handler DEFINITION.

  PUBLIC SECTION.

    METHODS:

      handle_timer FOR EVENT finished OF cl_gui_timer.

ENDCLASS.                    "lcl_event_handler DEFINITION

*&---------------------------------------------------------------------*

*&      CLASS lcl_event_handler IMPLEMENTATION

*&---------------------------------------------------------------------*

CLASS lcl_event_handler IMPLEMENTATION.

  METHOD handle_timer.

    CALL METHOD cl_gui_cfw=>set_new_ok_code
      EXPORTING
        new_code = 'RFSH'.

    CALL METHOD go_timer->run "必须重新激活定时器
      EXCEPTIONS
        OTHERS = 9.

  ENDMETHOD.                 "handle_timer

ENDCLASS.                    "lcl_event_handler IMPLEMENTATION

*&---------------------------------------------------------------------*

*&      Form  INIT_TIMER

*&---------------------------------------------------------------------*

FORM init_timer.

  CHECK go_timer IS INITIAL.

  CREATE OBJECT go_timer
    EXCEPTIONS
      OTHERS = 9.

  CREATE OBJECT go_evt_hndl.

  SET HANDLER go_evt_hndl->handle_timer FOR go_timer.


  go_timer->interval = 5.  "设置间隔为10秒

  CALL METHOD go_timer->run "激活定时器
    EXCEPTIONS
      OTHERS = 9.

ENDFORM.                    " INIT_TIMER
*&---------------------------------------------------------------------*
*& Form LIST_OUTPUT
*&---------------------------------------------------------------------*
*& text
*&---------------------------------------------------------------------*
*& -->  p1        text
*& <--  p2        text
*&---------------------------------------------------------------------*
FORM list_output .
  DATA: datediff TYPE p,
        timediff TYPE p DECIMALS 2.

  datediff = sy-datum - gv_datum.
  timediff = ( sy-uzeit - gv_uzeit ) / 60.

  WRITE: /5 '本次开始时间:' ,AT 30 gv_datum , AT 45 gv_uzeit.
  WRITE: /5 '本次运行时长:' ,AT 30 datediff LEFT-JUSTIFIED,
                              AT 38 '天' ,
                              at 45 timediff LEFT-JUSTIFIED ,
                              AT 51 '分'.

ENDFORM.
代码语言:javascript复制
DATA GO_TIMER TYPE REF TO CL_GUI_TIMER .
DATA: GV_INDEX TYPE N LENGTH 2 ,
      GV_TMP TYPE N LENGTH 2 ,
      GV_OKCODE TYPE SY-UCOMM .

DATA: HH TYPE C LENGTH 2 , "记录时分秒
      FF TYPE C LENGTH 2 ,
      MM TYPE C LENGTH 2 .

CLASS LCL_EVENT_HANDLER DEFINITION.
  PUBLIC SECTION.
    METHODS:
      HANDLE_TIMER FOR EVENT FINISHED OF CL_GUI_TIMER.
ENDCLASS.                    "lcl_event_handler DEFINITION
*&---------------------------------------------------------------------*
*&      CLASS lcl_event_handler IMPLEMENTATION
*&---------------------------------------------------------------------*
CLASS LCL_EVENT_HANDLER IMPLEMENTATION.
  METHOD HANDLE_TIMER.
    PERFORM FRM_PROCESS_TIMER.

  ENDMETHOD.                 "handle_timer
ENDCLASS.                    "lcl_event_handler IMPLEMENTATION
DATA GO_EVT_HNDL TYPE REF TO LCL_EVENT_HANDLER .

INITIALIZATION .



START-OF-SELECTION  .

  CALL SCREEN 0100 .
*&---------------------------------------------------------------------*
*& Form FRM_PROCESS_TIMER
*&---------------------------------------------------------------------*
*& text
*&---------------------------------------------------------------------*
*& -->  p1        text
*& <--  p2        text
*&---------------------------------------------------------------------*
FORM FRM_PROCESS_TIMER .

  CALL METHOD CL_GUI_CFW=>SET_NEW_OK_CODE "强制触发PAI
    EXPORTING
      NEW_CODE = 'RFSH'.
  CALL METHOD GO_TIMER->RUN "
    EXCEPTIONS
      OTHERS = 9.

ENDFORM.
*&---------------------------------------------------------------------*
*& Form INIT_TIMER
*&---------------------------------------------------------------------*
*& text
*&---------------------------------------------------------------------*
*& -->  p1        text
*& <--  p2        text
*&---------------------------------------------------------------------*
FORM INIT_TIMER .
 IF GO_TIMER IS INITIAL.
    CREATE OBJECT GO_TIMER
      EXCEPTIONS
        OTHERS = 9.
    CREATE OBJECT GO_EVT_HNDL.
    SET HANDLER GO_EVT_HNDL->HANDLE_TIMER FOR GO_TIMER.

    GO_TIMER->INTERVAL = 1.  "设置间隔为1秒
    CALL METHOD GO_TIMER->RUN "激活定时器
      EXCEPTIONS
        OTHERS = 9.
  ELSE .
    CALL METHOD GO_TIMER->RUN "重新激活定时器
     EXCEPTIONS
       OTHERS = 9.
 ENDIF .
ENDFORM.
*&---------------------------------------------------------------------*
*& Module STATUS_0100 OUTPUT
*&---------------------------------------------------------------------*
*&
*&---------------------------------------------------------------------*
MODULE STATUS_0100 OUTPUT.
  SET PF-STATUS 'STATUS_0100'.
* SET TITLEBAR 'xxx'.
  PERFORM INIT_TIMER .
ENDMODULE.
*&---------------------------------------------------------------------*
*&      Module  USER_COMMAND_0100  INPUT
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
MODULE USER_COMMAND_0100 INPUT.
 CASE GV_OKCODE.
 	WHEN 'BACK'.
 	  LEAVE TO SCREEN 0 .
  WHEN  'RFSH'.
    GV_INDEX = GV_INDEX   1 .
    CLEAR GV_TMP.
    GV_TMP = GV_INDEX DIV 3600 . "获取小时
    HH = GV_TMP .
    CLEAR  GV_TMP .
    GV_TMP = GV_INDEX DIV 60 . "获取分钟
    FF = GV_TMP .
    CLEAR GV_TMP .
    GV_TMP  = GV_INDEX MOD 60 . "获取秒
     MM = GV_TMP .
 	WHEN OTHERS.
 ENDCASE.
ENDMODULE.

0 人点赞