AsyncOperationProcessingHandler.cpp 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. /*
  2. * Copyright (c) Contributors to the Open 3D Engine Project.
  3. * For complete copyright and license terms please see the LICENSE at the root of this distribution.
  4. *
  5. * SPDX-License-Identifier: Apache-2.0 OR MIT
  6. *
  7. */
  8. #include <AzCore/Component/TickBus.h>
  9. #include <AzCore/std/parallel/thread.h>
  10. #include <AzToolsFramework/Debug/TraceContext.h>
  11. #include <SceneAPI/SceneUI/Handlers/ProcessingHandlers/AsyncOperationProcessingHandler.h>
  12. #include <QTimer>
  13. namespace AZ
  14. {
  15. namespace SceneAPI
  16. {
  17. namespace SceneUI
  18. {
  19. AsyncOperationProcessingHandler::AsyncOperationProcessingHandler(Uuid traceTag, AZStd::function<void()> targetFunction, AZStd::function<void()> onComplete, QObject* parent)
  20. : ProcessingHandler(traceTag, parent)
  21. , m_operationToRun(targetFunction)
  22. , m_onComplete(onComplete)
  23. {
  24. }
  25. void AsyncOperationProcessingHandler::BeginProcessing()
  26. {
  27. emit StatusMessageUpdated("Waiting for background processes to complete...");
  28. // Note that the use of a QThread instead of an AZStd::thread is intentional here, as signals, slots, timers, and other parts
  29. // of Qt will cause weird behavior and crashes if invoked from a non-QThread. Qt tries its best to compensate, but without
  30. // a QThread as context, it may not correctly be able to invoke cross-thread event queues, or safely store objects in QThreadStorage.
  31. m_thread = QThread::create(
  32. [this]()
  33. {
  34. AZ_TraceContext("Tag", m_traceTag);
  35. m_operationToRun();
  36. QMetaObject::invokeMethod(this, &AsyncOperationProcessingHandler::OnBackgroundOperationComplete, Qt::QueuedConnection);
  37. }
  38. );
  39. m_thread->start();
  40. }
  41. void AsyncOperationProcessingHandler::OnBackgroundOperationComplete()
  42. {
  43. m_thread->quit(); // signal the thread's event pump to exit (at this point, its almost certainly already completed)
  44. m_thread->wait(); // wait for the thread to clean up any state, as well as actually join (ie, exit) so that it is no longer running.
  45. delete m_thread;
  46. m_thread = nullptr;
  47. emit StatusMessageUpdated("Processing complete");
  48. if (m_onComplete)
  49. {
  50. m_onComplete();
  51. }
  52. emit ProcessingComplete();
  53. }
  54. }
  55. }
  56. }
  57. #include <Handlers/ProcessingHandlers/moc_AsyncOperationProcessingHandler.cpp>