Ejemplo n.º 1
0
        private void accountToMigrate_DoWork(object sender, System.ComponentModel.DoWorkEventArgs e)
        {
            Account accnt = e.Argument as Account;

            // ------------------------------------------------
            // Create a thread-specific log for this thread
            // ------------------------------------------------
            string AccountName = accnt.AccountName;
            int    i           = AccountName.IndexOf("@");

            if (i != -1)
            {
                AccountName = AccountName.Substring(0, i);
            }

            // Similar code in ScheduleViewModel.cs::worker_DoWork
            // Log suffix  [src accnt] to [targ accnt]
            string sSrc = Path.GetFileNameWithoutExtension(accnt.AccountID);

            Log.StartNewLogfileForThisThread("[" + sSrc + "] to [" + AccountName + "]"); // If you change this, also change OpenLogFile()


            using (LogBlock logblock = Log.NotTracing() ? null : new LogBlock(GetType() + "." + System.Reflection.MethodBase.GetCurrentMethod().Name)) // LogBlock not available here...it is now logging moved to ZimbraAPI
            {
                // Get argument from DoWorkEventArgs argument.  Can use any type here with cast
                // int myProcessArguments = (int)e.Argument;
                // "ProcessFile" is the name of my method that does the main work.  Replace with your own method!
                // Can return reulsts from this method, i.e. a status (OK, FAIL etc)
                //e.Result = ProcessFile(myProcessArgument);
                BackgroundWorker worker = sender as BackgroundWorker;

                // Assign the result of the computation
                // to the Result property of the DoWorkEventArgs
                // object. This is will be available to the
                // RunWorkerCompleted eventhandler.
                // e.Result = Accounworker, e);
                //int num = 0;

                //while (!_shouldStop)
                {
                    accnt.WorkerSupportsCancellation = true;

                    CssLib.MigrationAccount MyAcct = new CssLib.MigrationAccount();
                    MyAcct.AccountName   = accnt.AccountName;
                    MyAcct.AccountID     = accnt.AccountID;
                    MyAcct.MaxErrorCount = accnt.Mailoptions.MaxErrorCnt;
                    MyAcct.AccountNum    = accnt.num;
                    MyAcct.OnChanged    += new CssLib.MigrationObjectEventHandler(Acct_OnChanged);

                    CssLib.MigrationFolder MyFolder = new CssLib.MigrationFolder();


                    //MyFolder.AccountNum = argumentTest.num;
                    int idx = MyAcct.AccountName.IndexOf("@");
                    if (idx != -1)
                    {
                        MyFolder.AccountID = MyAcct.AccountName.Substring(0, idx);
                    }


                    // MyFolder.AccountID = argumentTest.AccountID;
                    MyFolder.OnChanged += new CssLib.MigrationObjectEventHandler(Folder_OnChanged);

                    MyAcct.CurrFolder = MyFolder;


                    CssLib.CSMigrationWrapper mw = accnt.TestObj;

                    if (accnt.CancellationPending)
                    {
                        e.Cancel = true;
                    }
                    else
                    {
                        // TestObj.Migrate(MigrateOptions);

                        mw.StartMigration(MyAcct, accnt.Mailoptions, accnt.serverMigration);
                        if (accnt.Mailoptions.IsMaintainenceMode)
                        {
                            System.Console.WriteLine("Mailbox is in Maintainence mode. Try again Later");
                        }

                        accnt.NumofErrors = MyAcct.NumAccountErrs;

                        //Last item migration
                        string sAcccountID = "[" + MyFolder.AccountID.ToString() + "]";
                        string msg2        = sAcccountID.PadRight(25, ' ') + MyFolder.FolderName.PadRight(25, ' ') + "{0,5:#} of {1,5:#} migrated successfully";
                        string msgF        = String.Format(msg2, MyFolder.NumFolderItemsMigrated, MyFolder.NumFolderItemsToMigrate);
                        System.Console.WriteLine(msgF);
                    }
                }

                if (_shouldStop)
                {
                    worker.CancelAsync();
                    accnt.CancelAsync();
                    accnt.countdown.Signal();
                    e.Cancel = true;
                }

                if (_shouldCancel)
                {
                    //worker.CancelAsync();
                    // argumentTest.CancelAsync();
                    // argumentTest.NumofErrors = NumofErrors;
                    accnt.CancelAsync();
                }
            }
        }