Hi, i ran some tests and i learn use taskmanager to run a background task, i wrote a little exemple exporting the page to a jpg file
https://drive.google.com/open?id=1poibUz7DkbLOxwUrgVSN0FwsJnMUoKF1
I try upload here but don't works!
Macro Class
public partial class Main { private Corel.Interop.VGCore.Application app; private static BGTask bgTask; [CgsAddInConstructor] public Main(object _app) { app = _app as Corel.Interop.VGCore.Application; if (bgTask == null) { bgTask = new BGTask(app); bgTask.RunningChange += BgTask_RunningChange; } Startup(); } private void BgTask_RunningChange() { string msg = "Stop Task"; if (bgTask.Running) msg = "Start Task"; app.FrameWork.ShowMessageBox(msg); } private void Startup(){} [CgsAddInMacro] public void StartTask() { if (bgTask == null) return; ICUITaskManager taskManager = app.FrameWork.TaskManager; try { taskManager.RunInBackground(cuiTaskPriority.kASAP, bgTask); } catch(Exception erro) { app.FrameWork.ShowMessageBox(erro.Message); } } [CgsAddInMacro] public void StopTask() { if(bgTask!=null) bgTask.QuitTask(); } }
Task Class
public class BGTask : ICUIRunningTask, ICUIBackgroundTask { public event Action RunningChange; private Application app; private bool running = false; public bool Running { get { return running; } private set { running = value; if(RunningChange!=null) RunningChange(); } } ExportFilter ex; string exportFolder = "C:\\Users\\Reginaldo\\Desktop\\ExportTest"; int limiterCount = 0; int limiterMax = 10; private int index = 0; public int Index { get { return index; } } public string Name { get { return "Export Task"; } } public BGTask(Application app) { this.app = app; Random rd = new Random(); index = rd.Next(10); }
public void FinalizeTask() { Running = false; }
public void FreeTask() { Running = false; }
public void QuitTask() { Running = false; }
public void TryAbort() { Running = false; } public void RunTask() { Running = true; limiterCount = 0; ExportJPG(); } protected void ExportJPG() { ex = app.ActiveDocument.ExportBitmap(string.Format("{0}\\exported{1}-{2}.jpg" , exportFolder,limiterCount, DateTime.Now.ToString("mm-ss")) , cdrFilter.cdrJPEG); ex.Finish(); if (running && limiterCount < limiterMax) { limiterCount++; Thread.Sleep(1900); ExportJPG(); } if (limiterCount == limiterMax) { throw new Exception("Safe limit rearch!"); } return; }
}
For a Demo
https://www.youtube.com/watch?v=Cs8mchmzcm0
Nice one! Do you have any idea if this could work with saving CDR file as well? I found built in Save method is single threaded and always lock out the CorelDRAW UI if it's saving a big file.
The Save itself of course will work...
But I do not think corel is able to deal with that background task since it is single threaded inside its COM. But you could try.
Yes this work to save, your ui dont will block, but only in coreldraw 2017