question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Qt: Could not initialize OLE (error 80010106)

See original GitHub issue

I’m working on a ASP.NET Core MVC app and I wanted to use DinkToPdf library to convert Html to PDF. I get this error in the console, when I first access a page that uses the IConverter from the services:

Qt: Could not initialize OLE (error 80010106)

I still get the PDF with no issues. I think the issue is somewhere around calling: new SynchronizedConverter(new PdfTools()) As I have it in the services as a singleton and only called the first time it’s accessed:

services.AddSingleton(typeof(IConverter), new SynchronizedConverter(new PdfTools()));

Issue Analytics

  • State:open
  • Created 3 years ago
  • Reactions:3
  • Comments:14

github_iconTop GitHub Comments

9reactions
distantcamcommented, Jun 28, 2021

I’ve just hit this problem too, in an Azure Isolated Functions host project.

I’m pretty sure this issue is due to the worker thread in SynchronizedConverter not being STA mode. So I copied the SynchronizedConverter and made an STA version.

public class STASynchronizedConverter : BasicConverter
{
    Thread conversionThread;

    BlockingCollection<Task> conversions = new BlockingCollection<Task>();

    bool kill = false;

    private readonly object startLock = new object();

    public STASynchronizedConverter(ITools tools) : base(tools)
    {
    }

    public override byte[] Convert(IDocument document)
    {
        return Invoke(() => base.Convert(document));
    }

    public TResult Invoke<TResult>(Func<TResult> @delegate)
    {
        StartThread();

        Task<TResult> task = new Task<TResult>(@delegate);

        lock (task)
        {
            //add task to blocking collection
            conversions.Add(task);

            //wait for task to be processed by conversion thread
            Monitor.Wait(task);
        }

        //throw exception that happened during conversion
        if (task.Exception != null)
        {
            throw task.Exception;
        }

        return task.Result;
    }

    private void StartThread()
    {
        lock (startLock)
        {
            if (conversionThread == null)
            {
                conversionThread = new Thread(Run)
                {
                    IsBackground = true,
                    Name = "wkhtmltopdf worker thread"
                };

                // This is to fix issue https://github.com/rdvojmoc/DinkToPdf/issues/119
                conversionThread.SetApartmentState(ApartmentState.STA);

                kill = false;

                conversionThread.Start();
            }
        }
    }

    private void StopThread()
    {
        lock (startLock)
        {
            if (conversionThread != null)
            {
                kill = true;

                while (conversionThread.ThreadState == ThreadState.Stopped)
                { }

                conversionThread = null;
            }
        }
    }

    private void Run()
    {
        while (!kill)
        {
            //get next conversion taks from blocking collection
            Task task = conversions.Take();

            lock (task)
            {
                //run taks on thread that called RunSynchronously method
                task.RunSynchronously();

                //notify caller thread that task is completed
                Monitor.Pulse(task);
            }
        }
    }
}
0reactions
zanyar3commented, Jun 16, 2023

Thanks, @distantcam it is work.

only notice for all, To access the Converter class, you must inject it, not create a new instance.

public YourService(IIConverter converter) 
{
        _converter = converter;
}
Read more comments on GitHub >

github_iconTop Results From Across the Web

Could not initialize OLE (error 80010106) - (libwkhtmltox.dll ...
The CreatePdf method its executed two times. In the first time, it executes and show to me in the console, this: Qt: Could...
Read more >
C# : Qt: Could not initialize OLE (error 80010106) - YouTube
C# : Qt : Could not initialize OLE ( error 80010106 ) - (libwkhtmltox.dll) on C# To Access My Live Chat Page, On...
Read more >
Thread: Could not initialize OLE weirdness
Re: Could not initialize OLE weirdness​​ "QClipboard::setMimeData: Failed to set data on clipboard". Workaround is to make the managed code ...
Read more >
Windows: Suggest to implement fallback for clipboard (text) ...
It outputs following errors: Qt: Could not initialize OLE (error 80010106) QClipboard::setMimeData: Failed to set data on clipboard () I ...
Read more >
Qt: Could not initialize OLE (error 80010106)
Qt : Could not initialize OLE (error 80010106)
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found