This topic is about me trying to make a simple IOperation implementation in C# to provide you with some code related to a few topics I raised:
https://forum.pdf-xchange.com/viewtopic.php?f=66&t=35299
https://forum.pdf-xchange.com/viewtopic.php?f=66&t=35252
Now, I have it done in Delphi (all working as expected how I have it there), but I guess you would prefer C# - so this is what I'm trying to do - make a simple IOperation implementation in C#.
Now, it's been a while since I did some real coding in C#, and it seems I've gotten rusty
I know you do not help with writing code, but please just push me in the right direction - so I can finish the sample code and ask questions related to it.
Here's what I have:
A TaskWait is a simple IOperation implementation just waiting in its Do() method:
Code: Select all
class TaskWait : PDFXEdit.IOperation
{
TaskRunner taskRunner;
public TaskWait(TaskRunner tr)
{
taskRunner = tr;
}
public void Do(int nFlags = 0)
{
int myStrID = taskRunner.PXVInst.Str2ID("mystr"); //[b]EXCEPTION HERE[/b]
System.Threading.Thread.Sleep(100 * (new Random().Next(1, 3)));
}
public void ShowSetupUI(uint hWndParent, int nFlags = 0)
{
throw new NotImplementedException();
}
public IOpInputItem CreateInputItem(object pSrc)
{
throw new NotImplementedException();
}
public int ID => throw new NotImplementedException();
public ICab Params => throw new NotImplementedException();
public COMThreadingModel COMThreadingModel => COMThreadingModel.COMThreadingModel_Any;
}
Code: Select all
class TaskRunner
{
public int TotalRunCount = 0;
public PDFXEdit.IPXV_Inst PXVInst;
public TaskRunner(int runCount, IPXV_Inst inst)
{
TotalRunCount = runCount;
PXVInst = inst;
for (int i = 0; i < runCount; i++)
{
TaskWait tw = new TaskWait(this);
inst.AsyncDoAndWaitForFinish(tw);
}
MessageBox.Show("done");
}
}
Code: Select all
private void btnTaskRun_Click(object sender, EventArgs e)
{
new TaskRunner(5, pdfCtl.Inst);
}
In the implementation of the Do() method in TaskWait I'm trying to access PXVInst from TaskRunner as simple as :
Code: Select all
int myStrID = taskRunner.PXVInst.Str2ID("mystr");
This happens when an instance of TaskWait is executed via AsyncDoAndWaitForFinish. If executed in the same (/ main thread) by simply calling tw.Do() - all works.System.InvalidCastException
HResult=0x80004002
Message=Unable to cast COM object of type 'PDFXEdit.PXV_InstClass' to interface type 'PDFXEdit.IPXV_Inst'. This operation failed because the QueryInterface call on the COM component for the interface with IID '{D726366D-34D6-49FC-A341-7B84C54CCA3E}' failed due to the following error: Bad variable type. (Exception from HRESULT: 0x80020008 (DISP_E_BADVARTYPE)).
Source=mscorlib
StackTrace:
at System.StubHelpers.StubHelpers.GetCOMIPFromRCW(Object objSrc, IntPtr pCPCMD, IntPtr& ppTarget, Boolean& pfNeedsRelease)
at PDFXEdit.PXV_InstClass.Str2ID(String sStr, Boolean bAddIfNotExists)
at FullDemo.TaskWait.Do(Int32 nFlags) in D:\pdfxchangetest\PDFEditorSDKExamples-master\CSharp\FullDemo\Task.cs:line 58
So , any help would be much appreciated
p.s.
Seems I must do some kind of casting - but am not sure what kind.
-žarko