我编写了一个应用程序,该应用程序从用C ++编写的dll中调用方法。这是一个WinForms应用程序,其中包含一个自托管的WCF服务。WCF方法之一被周期性地在2-3秒内调用。仅执行WCF部分时不会发生此问题。仅调用SDK函数时,不会出现此问题。但是,如果我启动定期调用我的方法的WCF客户端,并且同时执行一些SDK函数,则在10秒钟后出现FatalExecutionEngineError(System.ExecutionEngineException)。引发异常,应用程序崩溃。我真的不知道可能是什么问题,或如何解决。我怀疑它与SDK有某种联系,但我不确定,只是一种感觉。在SO中阅读相关答案,也许方法导入是错误的?这是到目前为止我得到的:
WCF类:
[ServiceContract]
public partial interface IWCFSample
{
[OperationContract]
string GetData(string param);
}
[ServiceBehavior(InstanceContextMode = InstanceContextMode.Single)]
public partial class WCFSample : IWCFSample
{
#region Delegates
public delegate string OnGetDataDelegate(object sender, string getDataParam);
#endregion Delegates
#region Events
public event OnGetDataDelegate OnGetData;
#endregion Events
public string GetData(string serializedGetDataParam)
{
string res = string.Empty;
try
{
if (OnGetData != null)
{
res = OnGetData(this, (serializedGetDataParam));
}
}
catch
{
throw;
}
return res;
}
}
像这样启动:
private ServiceHost _serviceHost;
private WCFSample _samplewcf;
private void Form1_Load(object sender, EventArgs e)
{
_samplewcf = new WCFSample();
_samplewcf.OnGetData += samplewcfOnGetData;
_serviceHost = new ServiceHost(_samplewcf);
_serviceHost.Open();
bw = new BackgroundWorker();
bw.WorkerSupportsCancellation = true;
bw.DoWork += (o, args) =>
{
WCFSampleClient client = new WCFSampleClient();
while (true)
{
if (bw.CancellationPending)
{
return;
}
client.GetData(DateTime.Now.ToString());
Thread.Sleep(200);
}
};
bw.RunWorkerAsync();
}
private string samplewcfOnGetData(object sender, string getDataParam)
{
richTextBox1.Text += "GetData Called - " + getDataParam + "\n";
richTextBox1.SelectionStart = richTextBox1.Text.Length;
richTextBox1.ScrollToCaret();
return getDataParam;
}
与C ++相关的部分很长,但是这里是我使用的导入。
int OpenDevice (char* strUSBName, int iPortNum )
Imported as:
[DllImport("TP9000.dll")]
public static extern int OpenDevice(string USBNam, int portnum);
int CheckFeeder (int* nCheck)
Imported as:
[DllImport("TP9000.dll")]
public static extern int CheckFeeder(ref int pStatus);
int DP_GetSensorStatus (BYTE *pRailStatus, BYTE *pFeedRollerStatus, BYTE *pTraySensorState)
Imported as:
[DllImport("TP9000.dll")]
public static extern int DP_GetSensorStatus(ref byte pRailStatus, ref byte pFeedRollerStatus, byte[] pTraySensorState);
int PathSenser(int *pStatus)
Imported as:
[DllImport("TP9000.dll")]
public static extern int PathSenser(ref int p1);
int CheckRibbonEx( int *nRibbon, int *nTagRibbon)
Imported as:
[DllImport("TP9000.dll")]
public static extern int CheckRibbonEx(ref int nRibbon, ref int nTagRibbon);
int N_EndOfPrint(int nPanel, int *pPanelCount, int *pJobNo)
Imported as:
[DllImport("TP9000.dll")]
public static extern int N_EndOfPrint(int p1, ref int p2, ref int p3);
int N_PrintJobStatus (int *pJobStatus )
Imported as:
[DllImport("TP9000.dll")]
public static extern int N_PrintJobStatus(int[] p1);
更新:
看来最后一次导入是错误的:
int N_PrintJobStatus (int *pJobStatus )
Imported as:
[DllImport("TP9000.dll")]
public static extern int N_PrintJobStatus(int[] p1);
如果我未在代码中调用此函数,则该函数不会挂起。但是我应该如何封送它。该文档说:
它检查打印状态。
int N_PrintJobStatus(int * pJobStatus)参数pJobStatus [out]:
- JobStatus [0] =特权状态:空闲(0x00),正在打印(0x01),错误打印停止(0xFF)
- JobStatus [1] =无备用作业(错误,无暂停作业)
- JobStatus [2] =待机作业数(包括当前正在运行的作业)
- JobStatus [3] =当前正在运行的作业的份数(总打印份数)
- JobStatus [4] =剩余作业的副本数(剩余计数)
- JobStatus [5] =是否显示重新打印检查窗口。1:自动选项显示,2:手动选项显示,0:无检查窗口
JobStatus [6] =它检查错误处理和状态消息级别(1〜4),然后在重印检查窗口显示0t(自动选项)
- 打开打印机,然后检查色带。
- 卡正在弹出
- 功能区正在同步
- 如果要重新打印,请按“确定”按钮。(使“确定”按钮被激活)
返回值0:成功。-1:失败。(Get_Status)
好吧,没有其余的代码,就不可能解决这个问题。该错误代码是以下几点:
int[] nJobSt = { 0, 0, 0, 0, 0, 0 }; //notice that the array length is 6
LastErrorCode = TP9000Dll.N_PrintJobStatus(nJobSt);
从SDK文档中,我们应该假设该函数需要一个7的数组长度,因此正确的代码是:
int[] nJobSt = { 0, 0, 0, 0, 0, 0, 0 }; //notice that the array length is 7
LastErrorCode = TP9000Dll.N_PrintJobStatus(nJobSt);
结论:每次阅读3次文档,切勿信任SDK随附的示例应用程序。
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句