我已经用C#创建了一个Windows服务,该服务作用于特定的文件夹(及其子文件夹)。每当将具有某些特定扩展名的新文件放在文件夹(或其子文件夹)中时,服务便会触发。它处理这些文件,并在文件夹本身内创建其他目录和文件(具有不同的扩展名)。
值得一提的是,该服务引用实际过程发生的一些非托管代码(在C ++中)。
只要文件夹中有尚未处理的现有文件并且服务已启动,就不会出现问题,并且文件会得到处理。但是,当服务处于运行状态时,如果我删除一些具有相关扩展名的新文件,则会出现访问冲突异常:消息:试图读取或写入受保护的内存。这通常表明其他内存已损坏。
我知道这可能是由于对非托管代码段的调用所导致的,从而导致生成异常。当我在将控件传递给extern函数的函数中使用HandleProcessCorruptedStateExceptions属性时,该异常被捕获在托管C#部分中。 另外,在服务运行时,如果我将文件一一删除,也不会出现异常,并且文件转换成功。即使是一小堆文件(2或3)也不会造成任何问题。此外,我将按顺序处理这些文件,这意味着除非之前的未转换文件被完全处理并从“将要处理”的文件列表中取出,否则下一个文件将不会启动。但是,当我选择一堆文件(超过5个或6个)并将它们粘贴到文件夹中时,就会遇到问题,并且会出现访问冲突异常。
//Extern Function declaration
partial class CSPService : ServiceBase
{
CreateLog logService = new CreateLog();
public static int trialCount, bookKeeping = 0;
private BackgroundWorker myWorker = new BackgroundWorker();
static string repository = ConfigurationManager.AppSettings["Repository"];
[DllImport("SConvertionDllProject.dll", CharSet = CharSet.Ansi, CallingConvention = CallingConvention.Cdecl)]
public static extern void arrconvert(string[] FilePath, bool arch);
//.....
//.....
}
//Function where the extern function is called:
[HandleProcessCorruptedStateExceptions]
private static bool CreateSVGLayerTimeStamp(string subFolderPath, string fileName, string filePath, string timeStampPath, bool startparam, bool startFlag)
{
int tot_pages = -1;
try
{
if (startparam == true)
{
bookKeeping++;
logEvents.AddItem(bookKeeping, "startparam Creation attempt for Resultant files" + fileName);
}
string[] dwgFilePaths = { fileName };
if ((Path.GetExtension(fileName) == ".dwg") || (Path.GetExtension(fileName) == ".dgn") || (Path.GetExtension(fileName) == ".dxf"))
{
arrconvert(dwgFilePaths, true); //, buffer, buffer.Capacity);
string folderpath = Path.GetDirectoryName(filePath);
string extension = Path.GetExtension(fileName);
string FName = Path.GetFileName(fileName);
string svgPath = folderpath + "\\" + justfileName + "_" + extension.Replace(".", "") + "\\" + justfileName + "_S";
string totPagePath = svgPath + "\\pages.tmp";
do
{
Thread.Sleep(1000);
if (File.Exists(totPagePath))
{
logEvents.AddItem(bookKeeping, "Creation attempt successful for resultant folder " + svgPath);
break;
}
} while (true);
string stringPages = File.ReadAllText(totPagePath);
tot_pages = Convert.ToInt32(stringPages);
File.Delete(totPagePath);
int[] pages = new int[tot_pages];
Indexdetails[] objIdetailsarr = new Indexdetails[tot_pages];
for (int i = 0; i < tot_pages; i++)
{
Indexdetails objindexdetails = new Indexdetails();
pages[i] = i + 1;
}
string json = JsonConvert.SerializeObject(pages);
string jsonstr = "{\"File\":\"" + FName + "\"" + "," + "\"Pages\":" + json + "}";
File.WriteAllText(subFolder + "\\indexpage.json", jsonstr);
}
else
tot_pages = pdfconverter(fileName);
if (startparam == true)
{
bookKeeping++;
logEvents.AddItem(bookKeeping, "Creation attempt successful for resultant file " + fileName);
}
DateTime lastModified = System.IO.File.GetLastWriteTime(filePath);
string formattedTimeStampStr = makeTimeStampString(lastModified);
formattedTimeStampStr = formattedTimeStampStr + "|" + tot_pages.ToString();
using (FileStream fs = File.Create(timeStampPath))
{
Byte[] info = new UTF8Encoding(true).GetBytes(formattedTimeStampStr); //modified by Sabyasachi on 13-Sep-2018
fs.Write(info, 0, info.Length);
}
trialCount = 0;
return true;
}
catch (AccessViolationException ex)
{
if (startparam == true)
{
bookKeeping++;
logEvents.AddItem(bookKeeping, "AccessViolation Exception: " + fileName + ". Message: " + ex.Message);
//ServiceController sc = new ServiceController("CSPanoramaService");
//EventLog.WriteEntry("CSPService Stopped.", EventLogEntryType.Information, 1402);
//sc.Stop();
}
if (startFlag == true)
if (!File.Exists(Path.GetDirectoryName(fileName) + "\\" + Path.GetFileNameWithoutExtension(fileName) + ".chk2"))
File.Create(Path.GetDirectoryName(fileName) + "\\" + Path.GetFileNameWithoutExtension(fileName) + ".chk2");
trialCount = trialCount + 1;
return false;
}
catch (Exception ex)
{
if (startparam == true)
{
bookKeeping++;
logEvents.AddItem(bookKeeping, "Exception: " + fileName + ". Message: " + ex.Message);
}
if (startFlag == true)
if (!File.Exists(Path.GetDirectoryName(fileName) + "\\" + Path.GetFileNameWithoutExtension(fileName) + ".chk2"))
File.Create(Path.GetDirectoryName(fileName) + "\\" + Path.GetFileNameWithoutExtension(fileName) + ".chk2");
trialCount = trialCount + 1;
return false;
}
}
//Snippet in the unmanaged code-section (in C++):
std::string SvgExport::arrconvert(char* filePath[], bool isArchitecture)
{
string Layerdetails;
using namespace std;
OdString myarr[1];
string fPath;
string s(filePath[0]);
myarr[0]= s.c_str();
m_pDb = nullptr;
if (eOk != setDb(myarr[0]))
return std::string();
m_enableArch = isArchitecture;
fPath=myarr[0];
getSvg(s);
enter code here
Layerdetails = getJsonLayers(s);
getThumbnail(myarr[0],150,150);
return "";
}