A forum for questions or concerns related to the PDF-XChange Core API SDK
Moderators:TrackerSupp-Daniel, Tracker Support, Vasyl-Tracker Dev Team, Chris - Tracker Supp, Sean - Tracker, Tracker Supp-Stefan
Forum rules DO NOT post your license/serial key, or your activation code - these forums, and all posts within, are public and we will be forced to immediately deactivate your license.
When experiencing some errors, use the IAUX_Inst::FormatHRESULT method to see their description and include it in your post along with the error code.
I want to update my code using the Viewer Simple DLL SDK to the new Core API SDK to solve our problem described here. We currently have a Core API SDK license.
I only need the PXCV_GetPagesCount, PXCV_GetPageDimensions and PXCV_DrawPageToDIBSection methods. Which solution is the best:
Use the Core API SDK (IPXC_Document.Pages.Count, IPXC_Page.GetDimension and IPXC_Page.DrawToMemory or IPXC_Page.DrawToDevice?) ;
Or use the Editor Simple SDK with the same method names (as mentioned here), but I don't think that the Core API SDK license we have work with it, right?
var coreAPI = new PDFXCoreAPI.PXC_Inst();
coreAPI.Init(key);
var document1 = coreAPI.NewDocument();
document1.Close();
var thread = new Thread(() =>
{
var document2 = coreAPI.NewDocument();
document2.Close();
});
thread.Start();
I tried both, ApartmentState.STA and ApartmentState.MTA but I always get the same error. With the old Viewer Simple DLL SDK, I called the PXCV_DrawPageToDIBSection in a STA apartment thread.
Do you think that it's bug in the Core API SDK or a mistake I made using the API in the other thread? I tried to look for this exception using COM objects in a multi-thread context but I can't find a solution to this problem (apart from the MTA apartment state which does not solve the problem for me).
namespace FullDemo
{
static class Program
{
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new MainFrm());
}
}
}
OK thank you for the sample I understand now. My mistake was to use API from both a STA thread (the main thread) and a MTA thread. If I understand correctly, if I want to do multi-threading, I have to always use the API from a MTA thread (even the synchronous calls):
// Initialize the API in the main STA thread
var coreAPI = new PDFXCoreAPI.PXC_Inst();
coreAPI.Init(key);
var document1 = coreAPI.NewDocument();
document1.Close();
// Use the API in a MTA thread
var thread = new Thread(() =>
{
var document2 = coreAPI.NewDocument(); // => COMException (Bad variable type)
document2.Close();
});
thread1.SetApartmentState(ApartmentState.MTA);
thread.Start();
// Initialize the API in a MTA thread
PDFXCoreAPI.PXC_Inst coreAPI = null;
var thread = new Thread(() =>
{
coreAPI = new PDFXCoreAPI.PXC_Inst();
coreAPI.Init(key);
});
thread.SetApartmentState(ApartmentState.MTA);
thread.Start();
// Wait for the thread to end
thread.Join();
// Use the API in a MTA thread
var thread1 = new Thread(() =>
{
var document1 = coreAPI.NewDocument();
document1.Close();
});
thread1.SetApartmentState(ApartmentState.MTA);
thread1.Start();
// Use the API in another MTA thread
var thread2 = new Thread((data) =>
{
var document2 = coreAPI.NewDocument();
document2.Close();
});
thread2.SetApartmentState(ApartmentState.MTA);
thread2.Start();
So I guess I'll have use a MTA thread for all my API calls (synchronous and asynchronous), because the main WPF thread is STA (stackoverflow example here).
It's not ideal but it seems to work.