/// <devdoc>
/// <para>
/// <see cref='System.Diagnostics.Process'/>如果过程资源被重用而不是启动,重用的进程与此相关联<see cref ='System.Diagnostics.Process'/>零件。
/// </para>
/// </devdoc>
[ResourceExposure(ResourceScope.None)]
[ResourceConsumption(ResourceScope.Machine, ResourceScope.Machine)]
public bool Start() {
Close();
ProcessStartInfo startInfo = StartInfo;
if (startInfo.FileName.Length == 0)
throw new InvalidOperationException(SR.GetString(SR.FileNameMissing));
if (startInfo.UseShellExecute) {
#if !FEATURE_PAL
return StartWithShellExecuteEx(startInfo);
#else
throw new InvalidOperationException(SR.GetString(SR.net_perm_invalid_val, "StartInfo.UseShellExecute", true));
#endif // !FEATURE_PAL
} else {
return StartWithCreateProcess(startInfo);
}
}
[ResourceExposure(ResourceScope.Machine)]
[ResourceConsumption(ResourceScope.Machine)]
public void Kill() {
SafeProcessHandle handle = null;
try {
handle = GetProcessHandle(NativeMethods.PROCESS_TERMINATE);
if (!NativeMethods.TerminateProcess(handle, -1))
throw new Win32Exception();
}
finally {
ReleaseProcessHandle(handle);
}
}
SafeProcessHandle GetProcessHandle(int access) {
return GetProcessHandle(access, true);
}
/// <devdoc>
/// 获取进程的短期句柄,具有给定的访问权限。
///如果句柄存储在当前进程对象中,则使用它。
///注意,我们存储在当前进程对象中的句柄将具有我们需要的所有访问权限。
/// </devdoc>
/// <internalonly/>
[ResourceExposure(ResourceScope.None)]
[ResourceConsumption(ResourceScope.Machine, ResourceScope.Machine)]
SafeProcessHandle GetProcessHandle(int access, bool throwIfExited) {
Debug.WriteLineIf(processTracing.TraceVerbose, "GetProcessHandle(access = 0x" + access.ToString("X8", CultureInfo.InvariantCulture) + ", throwIfExited = " + throwIfExited + ")");
#if DEBUG
if (processTracing.TraceVerbose) {
StackFrame calledFrom = new StackTrace(true).GetFrame(0);
Debug.WriteLine(" called from " + calledFrom.GetFileName() + ", line " + calledFrom.GetFileLineNumber());
}
#endif
if (haveProcessHandle) {
if (throwIfExited) {
//因为hasProcessHandle是true,我们知道我们有进程句柄
//打开时至少要有SYNCHRONIZE访问,所以我们可以等待它
// zero timeout以查看进程是否已退出。
ProcessWaitHandle waitHandle = null;
try {
waitHandle = new ProcessWaitHandle(m_processHandle);
if (waitHandle.WaitOne(0, false)) {
if (haveProcessId)
throw new InvalidOperationException(SR.GetString(SR.ProcessHasExited, processId.ToString(CultureInfo.CurrentCulture)));
else
throw new InvalidOperationException(SR.GetString(SR.ProcessHasExitedNoId));
}
}
finally {
if( waitHandle != null) {
waitHandle.Close();
}
}
}
return m_processHandle;
}
else {
EnsureState(State.HaveId | State.IsLocal);
SafeProcessHandle handle = SafeProcessHandle.InvalidHandle;
#if !FEATURE_PAL
handle = ProcessManager.OpenProcess(processId, access, throwIfExited);
#else
IntPtr pseudohandle = NativeMethods.GetCurrentProcess();
// Get a real handle
if (!NativeMethods.DuplicateHandle (new HandleRef(this, pseudohandle),
new HandleRef(this, pseudohandle),
new HandleRef(this, pseudohandle),
out handle,
0,
false,
NativeMethods.DUPLICATE_SAME_ACCESS |
NativeMethods.DUPLICATE_CLOSE_SOURCE)) {
throw new Win32Exception();
}
#endif // !FEATURE_PAL
if (throwIfExited && (access & NativeMethods.PROCESS_QUERY_INFORMATION) != 0) {
if (NativeMethods.GetExitCodeProcess(handle, out exitCode) && exitCode != NativeMethods.STILL_ACTIVE) {
throw new InvalidOperationException(SR.GetString(SR.ProcessHasExited, processId.ToString(CultureInfo.CurrentCulture)));
}
}
return handle;
}
}
public bool WaitForExit(int milliseconds) {
SafeProcessHandle handle = null;
bool exited;
ProcessWaitHandle processWaitHandle = null;
try {
handle = GetProcessHandle(NativeMethods.SYNCHRONIZE, false);
if (handle.IsInvalid) {
exited = true;
}
else {
processWaitHandle = new ProcessWaitHandle(handle);
if( processWaitHandle.WaitOne(milliseconds, false)) {
exited = true;
signaled = true;
}
else {
exited = false;
signaled = false;
}
}
}
finally {
if( processWaitHandle != null) {
processWaitHandle.Close();
}
// If we have a hard timeout, we cannot wait for the streams
if( output != null && milliseconds == -1) {
output.WaitUtilEOF();
}
if( error != null && milliseconds == -1) {
error.WaitUtilEOF();
}
ReleaseProcessHandle(handle);
}
if (exited && watchForExit) {
RaiseOnExited();
}
return exited;
}
internal ProcessWaitHandle( SafeProcessHandle processHandle): base() {
SafeWaitHandle waitHandle = null;
bool succeeded = NativeMethods.DuplicateHandle(
new HandleRef(this, NativeMethods.GetCurrentProcess()),
processHandle,
new HandleRef(this, NativeMethods.GetCurrentProcess()),
out waitHandle,
0,
false,
NativeMethods.DUPLICATE_SAME_ACCESS);
if (!succeeded) {
Marshal.ThrowExceptionForHR(Marshal.GetHRForLastWin32Error());
}
this.SafeWaitHandle = waitHandle;
}
[Browsable(false), DesignerSerializationVisibility(DesignerSerializationVisibility.Content), MonitoringDescription(SR.ProcessStartInfo)]
public ProcessStartInfo StartInfo {
get {
if (startInfo == null) {
startInfo = new ProcessStartInfo(this);
}
return startInfo;
}
[ResourceExposure(ResourceScope.Machine)]
set {
if (value == null) {
throw new ArgumentNullException("value");
}
startInfo = value;
}
}
[
DefaultValue(false),
MonitoringDescription(SR.ProcessCreateNoWindow),
NotifyParentProperty(true)
]
public bool CreateNoWindow {
get { return createNoWindow; }
set { createNoWindow = value; }
}
/// <summary>
/// PDF格式转为SWF
/// </summary>
/// <param name="pdfPathParameter">原视频文件地址,如/a/b/c.pdf</param>
/// <param name="swfPathParameter">生成后的FLV文件地址,如/a/b/c.swf</param>
/// <param name="beginpage">转换开始页</param>
/// <param name="endpage">转换结束页</param>
/// <param name="photoQuality">照片质量</param>
/// <returns></returns>
public static bool PdfConversionSwf(string pdfPathParameter, string swfPathParameter, int beginpage, int endpage, int photoQuality)
{
if (string.IsNullOrEmpty(pdfPathParameter))
{
throw new ArgumentNullException(pdfPathParameter);
}
if (string.IsNullOrEmpty(swfPathParameter))
{
throw new ArgumentNullException(swfPathParameter);
}
if (endpage < beginpage)
{
throw new ArgumentException("起始页数大于结束页数");
}
if (photoQuality <= 0)
{
throw new ArgumentException("照片质量错误");
}
var exe = HttpContext.Current.Server.MapPath("~/tools/swftools-2013-04-09-1007.exe");
var pdfPath = HttpContext.Current.Server.MapPath(pdfPathParameter);
var swfPath = HttpContext.Current.Server.MapPath(swfPathParameter);
Process p = null;
try
{
if (!File.Exists(exe) || !File.Exists(pdfPath))
{
return false;
}
if (File.Exists(swfPath))
{
File.Delete(swfPath);
}
var sb = new StringBuilder();
sb.Append(" \"" + pdfPath + "\"");
sb.Append(" -o \"" + swfPath + "\"");
sb.Append(" -s flashversion=9");
sb.Append(" -s disablelinks");
if (endpage > GetPageCount(pdfPath))
{
endpage = GetPageCount(pdfPath);
}
sb.Append(" -p " + "\"" + beginpage + "" + "-" + endpage + "\"");
//SWF中的图片质量
sb.Append(" -j " + photoQuality);
var command = sb.ToString();
//Process提供对本地和远程的访问进程,使能够启动和停止系统进程。
p = new Process
{
StartInfo =
{
FileName = exe,
Arguments = command,
WorkingDirectory = HttpContext.Current.Server.MapPath("~/Bin/"),
UseShellExecute = false,
RedirectStandardError = true,
CreateNoWindow = false
}
};
//启动线程
p.Start();
//开始异步读取
p.BeginErrorReadLine();
//等待完成
p.WaitForExit();
//开始同步读取
//p.StandardError.ReadToEnd();
if (!File.Exists(swfPath))
return false;
return true;
}
catch (IOException ioex)
{
throw new IOException(ioex.Message);
}
catch (Exception ex)
{
throw new Exception(ex.Message);
}
finally
{
if (p != null)
{
//关闭进程
p.Close();
//释放资源
p.Dispose();
}
}
}
机械节能产品生产企业官网模板...
大气智能家居家具装修装饰类企业通用网站模板...
礼品公司网站模板
宽屏简约大气婚纱摄影影楼模板...
蓝白WAP手机综合医院类整站源码(独立后台)...苏ICP备2024110244号-2 苏公网安备32050702011978号 增值电信业务经营许可证编号:苏B2-20251499 | Copyright 2018 - 2025 源码网商城 (www.ymwmall.com) 版权所有