Index: /trunk/src/VBox/Main/include/ConsoleImpl.h
===================================================================
--- /trunk/src/VBox/Main/include/ConsoleImpl.h	(revision 58003)
+++ /trunk/src/VBox/Main/include/ConsoleImpl.h	(revision 58004)
@@ -563,4 +563,7 @@
     typedef std::map<Utf8Str, ComPtr<IMediumAttachment> > MediumAttachmentMap;
     typedef std::list <USBStorageDevice> USBStorageDeviceList;
+
+    static DECLCALLBACK(int)    i_powerUpThread(RTTHREAD Thread, void *pvUser);
+    static DECLCALLBACK(int)    i_powerDownThread(RTTHREAD Thread, void *pvUser);
 
 private:
@@ -739,6 +742,4 @@
     void                        i_detachAllUSBDevices(bool aDone);
 
-    static DECLCALLBACK(int)    i_powerUpThread(RTTHREAD Thread, void *pvUser);
-    static DECLCALLBACK(int)    i_powerDownThread(RTTHREAD Thread, void *pvUser);
 
     static DECLCALLBACK(int)    i_vmm2User_SaveState(PCVMM2USERMETHODS pThis, PUVM pUVM);
Index: /trunk/src/VBox/Main/src-client/ConsoleImpl.cpp
===================================================================
--- /trunk/src/VBox/Main/src-client/ConsoleImpl.cpp	(revision 58003)
+++ /trunk/src/VBox/Main/src-client/ConsoleImpl.cpp	(revision 58004)
@@ -73,4 +73,5 @@
 #include "AutoCaller.h"
 #include "Logging.h"
+#include "ThreadTask.h"
 
 #include <VBox/com/array.h>
@@ -150,11 +151,13 @@
  * Console::addVMCaller() for more info.
  */
-struct VMTask
-{
+class VMTask: public ThreadTask
+{
+public:
     VMTask(Console *aConsole,
            Progress *aProgress,
            const ComPtr<IProgress> &aServerProgress,
            bool aUsesVMPtr)
-        : mConsole(aConsole),
+        : ThreadTask("GenericVMTask"),
+          mConsole(aConsole),
           mConsoleCaller(aConsole),
           mProgress(aProgress),
@@ -175,5 +178,5 @@
     }
 
-    ~VMTask()
+    virtual ~VMTask()
     {
         releaseVMCaller();
@@ -205,6 +208,7 @@
 
 
-struct VMPowerUpTask : public VMTask
-{
+class VMPowerUpTask : public VMTask
+{
+public:
     VMPowerUpTask(Console *aConsole,
                   Progress *aProgress)
@@ -215,5 +219,7 @@
           mTeleporterEnabled(FALSE),
           mEnmFaultToleranceState(FaultToleranceState_Inactive)
-    {}
+    {
+        m_strTaskName = "VMPwrUp";
+    }
 
     PFNCFGMCONSTRUCTOR mConfigConstructor;
@@ -227,13 +233,27 @@
     typedef std::list<ComPtr<IProgress> > ProgressList;
     ProgressList hardDiskProgresses;
+
+    void handler()
+    {
+        int vrc = Console::i_powerUpThread(NULL, this);
+    }
+
 };
 
-struct VMPowerDownTask : public VMTask
-{
+class VMPowerDownTask : public VMTask
+{
+public:
     VMPowerDownTask(Console *aConsole,
                     const ComPtr<IProgress> &aServerProgress)
         : VMTask(aConsole, NULL /* aProgress */, aServerProgress,
                  true /* aUsesVMPtr */)
-    {}
+    {
+        m_strTaskName = "VMPwrDwn";
+    }
+
+    void handler()
+    {
+        int vrc = Console::i_powerDownThread(NULL, this);
+    }
 };
 
@@ -2093,5 +2113,4 @@
                             Global::stringifyMachineState(mMachineState));
     }
-
     LogFlowThisFunc(("Initiating SHUTDOWN request...\n"));
 
@@ -2136,29 +2155,18 @@
         try
         {
-            /* Setup task object and thread to carry out the operation asynchronously.
-             * We are going to pass ownership of task pointer to another thread.
-             * So we are in charge of deletion this task pointer in case if RTThreadCreate 
-             * returns not successful result or in case of any exception 
-             */
-            
             task = new VMPowerDownTask(this, pProgress);
-            AssertBreakStmt(task->isOk(), rc = E_FAIL);
-            int vrc = RTThreadCreate(NULL, Console::i_powerDownThread,
-                                     (void *) task, 0,
-                                     RTTHREADTYPE_MAIN_WORKER, 0,
-                                     "VMPwrDwn");
-            if (RT_FAILURE(vrc))
+            if (!task->isOk())
             {
-                rc = setError(E_FAIL, "Could not create VMPowerDown thread (%Rrc)", vrc);
-                break;
+                throw E_FAIL;
             }
-            /* task is now owned by powerDownThread(), so release it */
-            //task.release();
-        }
-        catch(const std::exception &e)
-        {
-            rc = setError(E_FAIL, "Cought exception! Could not create VMPowerDown thread (%s)", e.what());
+        }
+        catch(...)
+        {
+            delete task;
+            rc = setError(E_FAIL, "Could not create VMPowerDownTask object \n");
             break;
         }
+
+        rc = task->createThread();
 
         /* pass the progress to the caller */
@@ -2169,11 +2177,4 @@
     if (FAILED(rc))
     {
-        if (task != NULL)
-        {
-            //we can delete this pointer because something
-            //was wrong and we hadn't passed pointer to another thread
-            LogFlowFunc(("Delete task VMPowerDownTask from Console::powerDown()\n"));
-            delete task;
-        }
         /* preserve existing error info */
         ErrorInfoKeeper eik;
@@ -6978,5 +6979,4 @@
 HRESULT Console::i_powerUp(IProgress **aProgress, bool aPaused)
 {
-
     LogFlowThisFuncEnter();
 
@@ -6995,4 +6995,5 @@
     LONG cOperations = 1;
     LONG ulTotalOperationsWeight = 1;
+    VMPowerUpTask* task = NULL;
 
     try
@@ -7113,6 +7114,18 @@
         /* Setup task object and thread to carry out the operation
          * asynchronously */
-        std::auto_ptr<VMPowerUpTask> task(new VMPowerUpTask(this, pPowerupProgress));
-        ComAssertComRCRetRC(task->rc());
+        try
+        {
+            task = new VMPowerUpTask(this, pPowerupProgress);
+            if (!task->isOk())
+            {
+                throw E_FAIL;
+            }
+        }
+        catch(...)
+        {
+            delete task;
+            rc = setError(E_FAIL, "Could not create VMPowerUpTask object \n");
+            throw rc;
+        }
 
         task->mConfigConstructor = i_configConstructor;
@@ -7372,4 +7385,5 @@
 #endif // 0
 
+        
         /* setup task object and thread to carry out the operation
          * asynchronously */
@@ -7379,12 +7393,8 @@
         }
 
-        int vrc = RTThreadCreate(NULL, Console::i_powerUpThread,
-                                 (void *)task.get(), 0,
-                                 RTTHREADTYPE_MAIN_WORKER, 0, "VMPwrUp");
-        if (RT_FAILURE(vrc))
-            throw setError(E_FAIL, "Could not create VMPowerUp thread (%Rrc)", vrc);
-
-        /* task is now owned by powerUpThread(), so release it */
-        task.release();
+        rc = task->createThread();
+
+        if (FAILED(rc))
+            throw rc;
 
         /* finally, set the state: no right to fail in this method afterwards
@@ -8253,35 +8263,21 @@
                     if (!task->isOk())
                     {
-                        LogFlowFunc(("Console is already being uninitialized.\n"));
-                    }
-                    else
-                    {
-                        /*
-                         * We are going to pass ownership of task pointer to another thread.
-                         * So we are in charge of deletion this task pointer in case if RTThreadCreate
-                         * returns not successful result or in case of any exception
-                         */
-                        int vrc = RTThreadCreate(NULL, Console::i_powerDownThread,
-                                                 (void *) task, 0,
-                                                 RTTHREADTYPE_MAIN_WORKER, 0,
-                                                 "VMPwrDwn");
-                        if (RT_FAILURE(vrc))
-                        {
-                            rc = E_FAIL;
-                            LogFlowFunc(("Could not create VMPowerDown thread (%Rrc)\n", vrc));
-                        }
+                        LogFlowFunc(("Console is already being uninitialized. \n"));
+                        throw E_FAIL;
                     }
                 }
-                catch(const std::exception &e)
+                catch(...)
                 {
-                    rc = E_FAIL;
-                    LogFlowFunc(("Cought exception! Could not create VMPowerDown thread (%s)\n", e.what()));
+                    delete task;
+                    LogFlowFunc(("Problem with creating VMPowerDownTask object. \n"));
                 }
 
-                if(FAILED(rc) || !task->isOk())
+                rc = task->createThread();
+
+                if (FAILED(rc))
                 {
-                    LogFlowFunc(("Delete task VMPowerDownTask from Console::i_vmstateChangeCallback()\n"));
-                    delete task;
+                    LogFlowFunc(("Problem with creating thread for VMPowerDownTask. \n"));
                 }
+
             }
             break;
@@ -9441,6 +9437,6 @@
     LogFlowFuncEnter();
 
-    std::auto_ptr<VMPowerUpTask> task(static_cast<VMPowerUpTask *>(pvUser));
-    AssertReturn(task.get(), VERR_INVALID_PARAMETER);
+    VMPowerUpTask* task = static_cast<VMPowerUpTask *>(pvUser);
+    AssertReturn(task, VERR_INVALID_PARAMETER);
 
     AssertReturn(!task->mConsole.isNull(), VERR_INVALID_PARAMETER);
@@ -10007,10 +10003,7 @@
     }
 
-    LogFlowFunc(("Delete task VMPowerDownTask from Console::i_powerDownThread()\n"));
-    delete task;
     LogFlowFuncLeave();
     return rc;
 }
-
 
 /**
