防止长时间不操作掉线和进度条

平时在使用培训系统时,总是会出现长时间不操作系统掉线需要重新登陆的情况,之前一个小伙伴分享给了我一段防止掉线的代码,现分享如下:

DATA : TEXT TYPE STRING,
       TIME TYPE I.
TIME = 0.
CONCATENATE 'Please open another session for working' '!' INTO TEXT.
DO.
  CALL FUNCTION 'SAPGUI_PROGRESS_INDICATOR'
    EXPORTING
      PERCENTAGE = TIME
      TEXT       = TEXT
    EXCEPTIONS
      OTHERS     = 1.
  TIME = TIME + 1.
  IF TIME = 999.
    TIME = 0.
  ENDIF.
  WAIT UP TO 10 SECONDS.
ENDDO.

主要原理就是利用SAPGUI_PROGRESS_INDICATOR这个函数死循环报消息,这个函数还可以用来提示进度哦,具体使用方法如下:

*&---------------------------------------------------------------------*
*&      Form  frm_gui_progress
*&---------------------------------------------------------------------*
*       text 进度条 加强版
*----------------------------------------------------------------------*
*      -->P_PERCENT  text
*      -->P_STRING   text
*----------------------------------------------------------------------*
FORM frm_gui_progress_xxf  USING  p_string  p_count p_all.
  DATA: l_all(10)          TYPE n,
        l_count(10)        TYPE n.
  DATA: l_message(150)     TYPE c,
        l_string(100)      TYPE c.
  DATA: l_percent          TYPE i.
  DATA: l_tmp TYPE i.

  l_tmp = p_count MOD 500."每500个报一次
  IF l_tmp = 0.

    l_count  = p_count."当前数目
    l_all    = p_all."总数目
    l_string = p_string.
    CONCATENATE 'Processing ( ' l_count '/' l_all  ' ) -- ' l_string INTO l_message."此处为拼接出的消息

    l_percent = l_count * 100 / l_all.
    IF l_percent > 100.
      l_percent = 100.
    ENDIF.


    IF sy-batch <> 'X'.
      CALL FUNCTION 'SAPGUI_PROGRESS_INDICATOR'
        EXPORTING
          percentage = l_percent
          text       = l_message.
    ENDIF.
  ENDIF.
ENDFORM.                    " FRM_GUI_PROGRESS

后来发现了一个防止掉线的优化版本,也就是在前面加了一个判断当前页面是不是本系统唯一的,如果是就再打开一个页面:

*&---------------------------------------------------------------------*
*& Report Z20043_CONTINUE2
*&---------------------------------------------------------------------*
*&
*&---------------------------------------------------------------------*
REPORT Z20043_CONTINUE2.
 DATA: text TYPE string,
      time TYPE i.
time = 0.
IF sy-langu = '1'.
  CONCATENATE '请使用其他会话进行工作' '!' INTO text.
ELSE.
  CONCATENATE 'Please use another session for working' '!' INTO text.
ENDIF.

DATA:gt_info TYPE TABLE OF uinfo2,
      gv_num TYPE i.

"获取当前用户的会话数
CALL FUNCTION 'TH_LONG_USR_INFO'
  EXPORTING
    user      = sy-uname
  TABLES
    user_info = gt_info.
DESCRIBE TABLE gt_info LINES gv_num.

"对当前打开的会话数进行判断,如果只打开了一个,那么再另外打开一个新的会话供用户使用
IF gv_num = 1.
  CALL FUNCTION 'TH_CREATE_FOREIGN_MODE'
    EXPORTING
      client           = sy-mandt
      user             = sy-uname
*     TCODE            =
*     RETURN_ERROR     = 1
*     CREATE_EXCLUSIVE = 0
    EXCEPTIONS
      user_not_found   = 1
      cant_create_mode = 2
      OTHERS           = 3.
  IF sy-subrc <> 0.
* Implement suitable error handling here
  ENDIF.

ENDIF.

DO.
  CALL FUNCTION 'SAPGUI_PROGRESS_INDICATOR'
    EXPORTING
      percentage = time
      text       = text
    EXCEPTIONS
      OTHERS     = 1.
  time = time + 1.
  IF time = 101.
    time = 0.
  ENDIF.
  WAIT UP TO 10 SECONDS.
ENDDO.

上一段代码源自
SAP刘梦的博客:http://blog.sina.com.cn/s/blog_c0978c9b0102x4wk.html
侵删。

你可能感兴趣的:(ABAP,乱乱记)