<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>topic Re: ReadDwgFile in BackgroundWorker with INDEXCTL reports Fatal Error in .NET Forum</title>
    <link>https://forums.autodesk.com/t5/net-forum/readdwgfile-in-backgroundworker-with-indexctl-reports-fatal/m-p/7551636#M28939</link>
    <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://forums.autodesk.com/t5/user/viewprofilepage/user-id/460083"&gt;@Ajilal.Vijayan&lt;/a&gt; wrote:&lt;BR /&gt;&lt;P&gt;Hello all,&lt;/P&gt;&lt;P&gt;I am trying to use ReadDwgFile in a BackgroundWorker.&lt;/P&gt;&lt;P&gt;When using ReadDwgFile in a BackgroundWorker on a drawing where the INDEXCTL is not 0 reports fatal error.&lt;/P&gt;&lt;P&gt;The fatal errors occurs while saving the database.&lt;/P&gt;&lt;P&gt;Please find the attached drawing which is a new drawing and the INDEXCTL = 3.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;FONT color="#808080"&gt;Edit:- I am using AutoCAD 2015.&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;I am using &lt;A href="https://forums.autodesk.com/t5/net/readdwgfile-in-backgroundworker-reports-the-calling-thread/m-p/6746236#M51245" target="_blank"&gt;the code from here&lt;/A&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;&lt;P&gt;You can't use the AutoCAD managed API from a BackgroundWorker. The API is not thread-safe.&lt;/P&gt;</description>
    <pubDate>Fri, 17 Nov 2017 01:30:40 GMT</pubDate>
    <dc:creator>ActivistInvestor</dc:creator>
    <dc:date>2017-11-17T01:30:40Z</dc:date>
    <item>
      <title>ReadDwgFile in BackgroundWorker with INDEXCTL reports Fatal Error</title>
      <link>https://forums.autodesk.com/t5/net-forum/readdwgfile-in-backgroundworker-with-indexctl-reports-fatal/m-p/7548708#M28938</link>
      <description>&lt;P&gt;Hello all,&lt;/P&gt;
&lt;P&gt;I am trying to use ReadDwgFile in a BackgroundWorker.&lt;/P&gt;
&lt;P&gt;When using ReadDwgFile in a BackgroundWorker on a drawing where the INDEXCTL is not 0 reports fatal error.&lt;/P&gt;
&lt;P&gt;The fatal errors occurs while saving the database.&lt;/P&gt;
&lt;P&gt;Please find the attached drawing which is a new drawing and the INDEXCTL = 3.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;FONT color="#808080"&gt;Edit:- I am using AutoCAD 2015.&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;I am using &lt;A href="https://forums.autodesk.com/t5/net/readdwgfile-in-backgroundworker-reports-the-calling-thread/m-p/6746236#M51245" target="_blank"&gt;the code from here&lt;/A&gt;&lt;/P&gt;
&lt;PRE&gt;using System;
using System.Collections.Generic;
using System.Linq;
using System.Diagnostics;
using System.ComponentModel;
using System.IO;
using Autodesk.AutoCAD.DatabaseServices;
using CadApp = Autodesk.AutoCAD.ApplicationServices.Application;
using Autodesk.AutoCAD.Runtime;

namespace AsyncSideDatabase
{
    public class SideDBBackWorker
    {
        private BackgroundWorker _worker = null;

        public void ProcessDwgs(string[] dwgFiles)
        {
            var dwg = CadApp.DocumentManager.MdiActiveDocument;
            var ed = dwg.Editor;

            ed.WriteMessage(
                "\nProcessing {0} file{1}...",
                dwgFiles.Length, dwgFiles.Length &amp;gt; 1 ? "s" : "");

            _worker = new BackgroundWorker();
            _worker.WorkerReportsProgress = true;
            _worker.ProgressChanged += Worker_ProgressChanged;
            _worker.RunWorkerCompleted += Worker_RunWorkerCompleted;
            _worker.DoWork += Worker_DoWork;
            _worker.RunWorkerAsync(dwgFiles);

            ed.WriteMessage("\nPlease wait...");
        }

        private void Worker_ProgressChanged(object sender, ProgressChangedEventArgs e)
        {
            var dwg = CadApp.DocumentManager.MdiActiveDocument;
            var ed = dwg.Editor;

            ed.WriteMessage("\nProcessing {0}, please wait...", e.UserState.ToString());
        }

        private void Worker_DoWork(object sender, DoWorkEventArgs e)
        {
            var dwgFiles = e.Argument as string[];

            var dic = new Dictionary&amp;lt;string, Tuple&amp;lt;int, int&amp;gt;&amp;gt;();
            int i = 0;
            foreach (var dwgFile in dwgFiles)
            {
                _worker.ReportProgress(++i, Path.GetFileName(dwgFile));

                var data = ProcessDwg(dwgFile);
                dic.Add(Path.GetFileName(dwgFile), data);
            }

            e.Result = dic;
        }

        private void Worker_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
        {
            var dwg = CadApp.DocumentManager.MdiActiveDocument;
            var ed = dwg.Editor;

            if (e.Cancelled)
            {
                ed.WriteMessage("\nBackround processing was cancelled.");
            }
            else
            {
                var dic = e.Result as Dictionary&amp;lt;string, Tuple&amp;lt;int, int&amp;gt;&amp;gt;;
                foreach (var entry in dic)
                {
                    string msg = string.Format(
                        "\n{0}: Entity Count: {1}, Processing Time: {2}.\n",
                        entry.Key, entry.Value.Item1, entry.Value.Item2);
                    ed.WriteMessage(msg);
                }

                ed.WriteMessage("\n{0} file{1} processed.",
                    dic.Count, dic.Count &amp;gt; 1 ? "s" : "");
            }

            if (_worker != null)
            {
                _worker.Dispose();
                _worker = null;
            }
        }

        private Tuple&amp;lt;int, int&amp;gt; ProcessDwg(string dwgFile)
        {
            Stopwatch watch = new Stopwatch();
            watch.Start();

            int count = 0;
            int span = 0;

            System.Threading.Thread.Sleep(2000);

            using (var db = new Database(false, true))
            {
                db.ReadDwgFile(dwgFile, FileOpenMode.OpenForReadAndAllShare, true, null);
                var ids = new ObjectIdCollection();
                db.Purge(ids);

                using (var tran = db.TransactionManager.StartTransaction())
                {
                    var bt = (BlockTable)tran.GetObject(db.BlockTableId, OpenMode.ForRead);
                    var space = (BlockTableRecord)tran.GetObject(
                        bt[BlockTableRecord.ModelSpace], OpenMode.ForRead);
                    count = space.Cast&amp;lt;ObjectId&amp;gt;().Count();

                    if (ids.Count &amp;gt; 0)
                    {
                        foreach (ObjectId id in ids)
                        {
                            var obj = tran.GetObject(id, OpenMode.ForWrite);
                            obj.Erase(true);
                        }
                    }

                    tran.Commit();
                }
                    &amp;nbsp;&lt;FONT color="#0000FF"&gt;&lt;STRONG&gt;db.SaveAs(dwgFile, true, DwgVersion.Current, db.SecurityParameters);&lt;/STRONG&gt;      &lt;/FONT&gt;   
            }

            watch.Stop();
            span = watch.Elapsed.Milliseconds;

            return new Tuple&amp;lt;int, int&amp;gt;(count, span);
        }

        [CommandMethod("SideDBWorker", CommandFlags.Session)]
        public static void RunBackworker()
        {
            var dwg = CadApp.DocumentManager.MdiActiveDocument;
            var ed = dwg.Editor;

            try
            {
                string[] dwgFiles = GetFileName();
                if (dwgFiles != null)
                {
                    var worker = new SideDBBackWorker();
                    worker.ProcessDwgs(dwgFiles);
                }
            }
            catch (System.Exception ex)
            {
                ed.WriteMessage("Error: {0}", ex.Message);
                ed.WriteMessage("\n*Cancel*");
            }

            ed.WriteMessage("\nCommand ended and waiting for background process completion...");
            Autodesk.AutoCAD.Internal.Utils.PostCommandPrompt();
        }

        private static string[] GetFileName(bool multiFiles = true)
        {
            string[] fileNames = null;
            using (var dlg = new System.Windows.Forms.OpenFileDialog())
            {
                dlg.Title = "Select Drawing File To Read";
                dlg.Multiselect = multiFiles;
                dlg.Filter = "AutoCAD Drawing File *.dwg|*.dwg";
                if (dlg.ShowDialog() == System.Windows.Forms.DialogResult.OK)
                {
                    fileNames = dlg.FileNames;
                }
            }

            return fileNames;
        }
    }
}

&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 16 Nov 2017 08:21:41 GMT</pubDate>
      <guid>https://forums.autodesk.com/t5/net-forum/readdwgfile-in-backgroundworker-with-indexctl-reports-fatal/m-p/7548708#M28938</guid>
      <dc:creator>Ajilal.Vijayan</dc:creator>
      <dc:date>2017-11-16T08:21:41Z</dc:date>
    </item>
    <item>
      <title>Re: ReadDwgFile in BackgroundWorker with INDEXCTL reports Fatal Error</title>
      <link>https://forums.autodesk.com/t5/net-forum/readdwgfile-in-backgroundworker-with-indexctl-reports-fatal/m-p/7551636#M28939</link>
      <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://forums.autodesk.com/t5/user/viewprofilepage/user-id/460083"&gt;@Ajilal.Vijayan&lt;/a&gt; wrote:&lt;BR /&gt;&lt;P&gt;Hello all,&lt;/P&gt;&lt;P&gt;I am trying to use ReadDwgFile in a BackgroundWorker.&lt;/P&gt;&lt;P&gt;When using ReadDwgFile in a BackgroundWorker on a drawing where the INDEXCTL is not 0 reports fatal error.&lt;/P&gt;&lt;P&gt;The fatal errors occurs while saving the database.&lt;/P&gt;&lt;P&gt;Please find the attached drawing which is a new drawing and the INDEXCTL = 3.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;FONT color="#808080"&gt;Edit:- I am using AutoCAD 2015.&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;I am using &lt;A href="https://forums.autodesk.com/t5/net/readdwgfile-in-backgroundworker-reports-the-calling-thread/m-p/6746236#M51245" target="_blank"&gt;the code from here&lt;/A&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;&lt;P&gt;You can't use the AutoCAD managed API from a BackgroundWorker. The API is not thread-safe.&lt;/P&gt;</description>
      <pubDate>Fri, 17 Nov 2017 01:30:40 GMT</pubDate>
      <guid>https://forums.autodesk.com/t5/net-forum/readdwgfile-in-backgroundworker-with-indexctl-reports-fatal/m-p/7551636#M28939</guid>
      <dc:creator>ActivistInvestor</dc:creator>
      <dc:date>2017-11-17T01:30:40Z</dc:date>
    </item>
    <item>
      <title>Re: ReadDwgFile in BackgroundWorker with INDEXCTL reports Fatal Error</title>
      <link>https://forums.autodesk.com/t5/net-forum/readdwgfile-in-backgroundworker-with-indexctl-reports-fatal/m-p/7551637#M28940</link>
      <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://forums.autodesk.com/t5/user/viewprofilepage/user-id/460083"&gt;@Ajilal.Vijayan&lt;/a&gt; wrote:&lt;BR /&gt;&lt;P&gt;Hello all,&lt;/P&gt;&lt;P&gt;I am trying to use ReadDwgFile in a BackgroundWorker.&lt;/P&gt;&lt;P&gt;When using ReadDwgFile in a BackgroundWorker on a drawing where the INDEXCTL is not 0 reports fatal error.&lt;/P&gt;&lt;P&gt;The fatal errors occurs while saving the database.&lt;/P&gt;&lt;P&gt;Please find the attached drawing which is a new drawing and the INDEXCTL = 3.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;FONT color="#808080"&gt;Edit:- I am using AutoCAD 2015.&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;&lt;P&gt;You can't use the AutoCAD managed API from a BackgroundWorker. The API is not thread-safe.&lt;/P&gt;</description>
      <pubDate>Fri, 17 Nov 2017 01:30:58 GMT</pubDate>
      <guid>https://forums.autodesk.com/t5/net-forum/readdwgfile-in-backgroundworker-with-indexctl-reports-fatal/m-p/7551637#M28940</guid>
      <dc:creator>ActivistInvestor</dc:creator>
      <dc:date>2017-11-17T01:30:58Z</dc:date>
    </item>
  </channel>
</rss>

