In 1C there is the possibility of using built-in asynchronous calls using the description of the alert. I need to "wrap" several asynchronous calls into one of my own, so that only one such call is executed from the side of the main code.

Can I create my own asynchronous methods and how?

    2 answers 2

    Good day, the 1C: Enterprise 8 platform does not allow you to implement your asynchronous procedures and functions. Implement asynchronous processing ("wrap"), nevertheless, the possibility is, using the procedure:

    ВыполнитьОбработкуОповещения(<ВыполняемоеОповещение>, <Результат>)

    This method is used to execute an alert handler procedure. To point to this handler, a description of the alert is passed to the method.

    This method is required in complex “multi-level” asynchronous call algorithms, since the description of the alert can be constructed at the beginning of the algorithm, and the actual use of this handler may be needed through several nested procedures. Example:

    Question in the nested procedure, after which there is a code

    Consider a more complicated case when after calling a nested function some code will be executed. For example:

    enter image description here

    It would seem that, following the recommendations, you just need to replace the nested procedure with two procedures:

    enter image description here

    However, in this case, the algorithm following the call to the nested procedure will be executed even before the user answers the question. After all, as we know, the execution of the code does not stop after the execution of the blocking method ( Comparison of modal and non-modal modes of operation ):

    enter image description here

    Therefore, in such a situation, it is necessary not only to use the blocking method with the alert call, but also to make an asynchronous call to the nested procedure itself, also using the alert:

    enter image description here

    Here, in a nested procedure, we immediately send a description of the alert, which contains the code that must be executed after the nested procedure (the last procedure). By calling the blocking method, we give it “its” description of the alert (Attached Procedure Finish), as well as through additional parameters, the description of the alert that will need to be executed after the user's interactive actions are processed (Processing Commands End).

    ( Source )

    PS It must be remembered that when working with synchronous calls, the values ​​are transferred by Ссылке (to synchronous procedures and functions), and for asynchronous calls by Значению . Example:

    The issue is in the form handler.

    The peculiarity of the dialogue with the user in this (and many other) handlers is that, depending on the user's reaction, the decision is made: to continue further actions, or abandon them. To do this, use the parameter Failure. With a single user response, we refuse to continue (Failure = Truth). With a different user response, we continue further actions.

    In this case, the difficulty lies in the fact that we will know the user's response after we leave the context of this handler. In the procedure that handles the alert. And the Failure parameter must be set in this handler.

    Therefore, we act in two steps:

    For the first time, we unconditionally cancel further actions (Failure = Truth) and display the question to the user; In the notification handler, depending on the user's reaction, either we close the form again programmatically or do nothing. The problem is that the handler beforeclosing will be executed twice. And in order to distinguish its first execution from the second (when the user's response is already known), we use the client variable Closing as a flag.

    In the first pass, its value is False, which means that you need to refuse to close and ask a question. In the second pass, its value is True, and this means that the question should not be asked:

    enter image description here

    ( Source )

    • 3
      Great answer! It would be even better if the examples from the links would be arranged directly in the answer. Links may be out of date. - Anatol

    I recommend using background tasks.

     // Формирование рабочего листа // Параметры; // Документ - ДокументСсылка - Пакетный документ // Процедура СформироватьРабочийЛист(Документ) Экспорт Структура = Новый Структура; Структура.Вставить("Документ", Документ); ПараметрыВыполнения = Новый Массив; ПараметрыВыполнения.Добавить(Структура); ФоновыеЗадания.Выполнить("ДлительныеОперации.СформироватьРабочийЛист", ПараметрыВыполнения, Новый УникальныйИдентификатор, "Автоматическое создание пакета документов для рабочего листа"); КонецПроцедуры 

    Background Tasks - Look at the helper syntax (I hope the point is clear)