简体   繁体   English

Process.Start有时需要很长时间才能回来

[英]Process.Start Sometimes taking a long time to come back

I have got an odd issue in an app I am writing. 我正在编写的应用程序中出现一个奇怪的问题。

It reads a master xml config file, creates a local copy for the user then performs some actions as laid out in the xml file. 它读取主xml配置文件,为用户创建本地副本,然后执行xml文件中列出的某些操作。 It checks to see if the local xml file needs updating so it doesn't need to do it every time. 它检查本地xml文件是否需要更新,因此不需要每次都进行更新。

Each of the actions are run in a new thread, and once all the threads have finished it writes a log and inform the user its finished. 每个操作都在新线程中运行,一旦所有线程完成,它就会写入日志并通知用户完成。

One of the actions is to import a .reg file into the registry. 一项操作是将.reg文件导入注册表。 I'm doing that like so: 我这样做是这样的:

Process regeditProcess = Process.Start("regedit.exe", "/s " + RegFilePath);
regeditProcess.WaitForExit();
regeditProcess.Close();

The issue I have is if the application creates a new config file, then the regedit process can take anything up to 30 seconds. 我的问题是,如果应用程序创建新的配置文件,则regedit进程最多可能需要30秒。 If it doesn't create one then it finishes within a second. 如果没有创建,那么它会在一秒钟内完成。

In the loop that checks the threads have finished, I have put an 在检查线程完成的循环中,我放置了一个

Application.DoEvents();

If I don't do this, the regedit process will run, and if I wait long enough I get a ContextSwitchDeadlock was detected error from the debugger. 如果不执行此操作,则regedit进程将运行,并且如果等待了足够长的时间,则会收到调试器检测到ContextSwitchDeadlock错误。 I have tried using different .reg file and only running this one action but still the same. 我尝试使用不同的.reg文件,并且仅运行此操作,但仍然相同。

The creation of the xml file takes place in the main worker thread before any of the actions are attempted, but I'm sure this is having some effect on it, I'm just not sure what. 尝试执行任何操作之前,都会在主工作线程中创建xml文件,但是我确定这会对它产生一定的影响,我不确定是什么。

I know its a bit of a strange scenario, but has anyone hit something like this before? 我知道这有点奇怪,但是以前有人打过吗?

Are you explicitly closing the file writer object after creating the config file? 您在创建配置文件后是否明确关闭文件编写器对象? If not, you may be locking the file, preventing access from your worker. 否则,您可能正在锁定文件,从而阻止了工作人员的访问。

I've worked out what the issue was, I had the STAThread directive in Program.cs. 我已经解决了问题所在,在Program.cs中有了STAThread指令。 Once i took this out it worked a treat. 一旦我把它拿出来,它就可以工作了。

STAThread and multithreading STAThread和多线程

http://ilvyanyatka.spaces.live.com/blog/cns!EA0C02AB2E2FCFAC!193.entry?wa=wsignin1.0&sa=143328961 http://ilvyanyatka.spaces.live.com/blog/cns!EA0C02AB2E2FCFAC!193.entry?wa=wsignin1.0&sa=143328961

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM