Mastering Long-Running Single and Multi-Threaded Operations in 1C: A Comprehensive Guide

Introduction

In the fast-paced world of software development, efficiency and user experience are paramount. Long-running operations, especially in 1C, can significantly impact both. This guide will walk you through implementing single-threaded and multi-threaded long-running operations in 1C, ensuring your applications remain responsive and efficient.

Understanding Long-Running Operations

Long-running operations in 1C are tasks that take a considerable amount of time to complete, such as processing large datasets or complex calculations. These operations can be executed in the background, allowing users to continue working without interruption.

Why Use Long-Running Operations?

  • Improved User Experience: Users can continue other tasks while the operation runs in the background.
  • Efficiency: Operations can be split into multiple threads, enhancing performance.

Implementing Single-Threaded Operations

Step 1: Setting Up the Environment

To implement single-threaded operations, you’ll need:

  • External Processing: Create a form module with two client methods and one server method.
  • Extension Module: Develop a module named «Long Operations» to house the operation logic.
  • Common Modules: Utilize modules like «LongOperations» and «LongOperationsClient.»

Step 2: Writing the Code

Here’s a simplified version of the code structure:

&НаСервере
Процедура ПриСозданииНаСервере(Отказ, СтандартнаяОбработка)
    Объект.УникальныйИдентификатор = Новый УникальныйИдентификатор();
КонецПроцедуры

&НаКлиенте
Процедура ВыполнитьОперацию(Команда)
    Элементы.СообщениеФорма.ВыделенныйТекст = "";

    ЭтотОбъект.ОбновитьОтображениеДанных();
    ДлительнаяОперация = НачатьПроведениеДокументовПТ(Объект.датаН, КонецДня(Объект.датаК), Объект.УникальныйИдентификатор);
    ПараметрыОжидания = ДлительныеОперацииКлиент.ПараметрыОжидания(ЭтотОбъект);
    ПараметрыОжидания.Заголовок = "Фоновое выполнение длительных операций";
    ПараметрыОжидания.ВыводитьПрогрессВыполнения = Истина;
    Оповещение = Новый ОписаниеОповещения("ПриЗавершенииДлительнойОперации", ЭтотОбъект);
    ДлительныеОперацииКлиент.ОжидатьЗавершение(ДлительнаяОперация, Оповещение, ПараметрыОжидания);
КонецПроцедуры

Implementing Multi-Threaded Operations

Step 1: Preparing for Multi-Threading

Multi-threaded operations require dividing the task into smaller chunks that can be processed simultaneously.

Step 2: Code Implementation

&НаСервере
Функция НачатьМногопоточноеПроведениеДокументовПТ(датаН, датаК, УникальныйИдентификатор, колвоПотоков)
    НомерПотока = 1;
    ПараметрыВыполнения = ДлительныеОперации.ПараметрыВыполненияВФоне(Новый УникальныйИдентификатор);

    ТЗобработки = ПолучитьТзДляМногопоточнойОперации(датаН, датаК);
    ЧислоСтрокВТаблице = ТЗобработки.Количество();
    ВсегоПотоков = колвоПотоков;
    РазмерПорции = Цел(ЧислоСтрокВТаблице / ВсегоПотоков);
    // Further implementation...
КонецФункции

Conclusion

Implementing long-running operations in 1C, whether single-threaded or multi-threaded, can greatly enhance the performance and user experience of your applications. By following the steps outlined in this guide, you can ensure that your applications remain responsive and efficient.

Helpful Tips:

  • Debugging: Use the 1C Enterprise Development Tools for effective debugging.
  • Resource Management: Always clean up temporary storage to free up resources.

For a complete code example, visit the GitHub repository. Happy coding!