/// <summary> /// Runs the bat command. /// </summary> /// <param name="batFilePath">The bat file path.</param> /// <param name="outputDelegate">The output delegate.</param> public static void RunBatCommand(string batFilePath, WriteOutputDelegate outputDelegate) { if (!string.IsNullOrWhiteSpace(batFilePath)) { Process batProcess = new Process(); // Redirect the output stream of the child process. batProcess.StartInfo.UseShellExecute = false; batProcess.StartInfo.RedirectStandardOutput = true; batProcess.StartInfo.RedirectStandardError = true; batProcess.StartInfo.FileName = batFilePath; if (outputDelegate != null) { batProcess.OutputDataReceived += (sender, args) => outputDelegate(args.Data); batProcess.ErrorDataReceived += (sender, args) => outputDelegate(args.Data); } batProcess.Start(); batProcess.BeginOutputReadLine(); //StreamReader streamReader = batProcess.StandardOutput; //// Read the standard output of the spawned process. //string output = streamReader.ReadLine(); //outputDelegate(output); batProcess.WaitForExit(); batProcess.Close(); } }
/// <summary> /// Executes the specified file path. /// </summary> /// <param name="filePath">The file path.</param> /// <param name="solutionName">Name of the solution.</param> /// <param name="package">The package.</param> /// <param name="outputDelegate">The output delegate.</param> static void Execute(string filePath, string solutionName, string package, WriteOutputDelegate outputDelegate) { try { var xDocument = XDocument.Load(filePath, LoadOptions.None); var solutions = ConfigurationHelper.LoadSolutions(xDocument); var selectedSolution = solutions.Find((x) => { return x.Name.Equals(solutionName, StringComparison.OrdinalIgnoreCase); }); XElement xmlToRun = null; if (selectedSolution != null) { package = package.SafeToString(); if (package.Equals("full", StringComparison.OrdinalIgnoreCase)) { xmlToRun = selectedSolution.FullXml; } else if (package.StartsWith("incremental-", StringComparison.OrdinalIgnoreCase)) { var pieces = package.Split('-'); if (pieces.Length > 2) { var fromVersion = pieces[1]; var toVersion = pieces[2]; xmlToRun = selectedSolution.IncrementalXml.ToList().Find((x) => { return x.GetAttributeValue("BaseVersion").Equals(fromVersion.SafeToString(), StringComparison.OrdinalIgnoreCase) && x.GetAttributeValue("TargetVersion").Equals(toVersion.SafeToString(), StringComparison.OrdinalIgnoreCase); }); } } if (xmlToRun != null) { SqlExecutor sqlExecutor = new SqlExecutor(outputDelegate, selectedSolution.ConnectionSetting, xmlToRun); sqlExecutor.Execute(solutionName); } else { outputDelegate("No matched SQL package to run."); } } else { } } catch (Exception ex) { outputDelegate(ex.Message); } finally { outputDelegate("Execution is ended at " + DateTime.Now + Environment.NewLine); } }
/// <summary> /// Executes the specified file path. /// </summary> /// <param name="filePath">The file path.</param> /// <param name="solutionName">Name of the solution.</param> /// <param name="package">The package.</param> /// <param name="outputDelegate">The output delegate.</param> static void Execute(string filePath, string solutionName, string package, WriteOutputDelegate outputDelegate) { try { var xDocument = XDocument.Load(filePath, LoadOptions.None); var solutions = ConfigurationHelper.LoadSolutions(xDocument); var selectedSolution = solutions.Find((x) => { return(x.Name.Equals(solutionName, StringComparison.OrdinalIgnoreCase)); }); XElement xmlToRun = null; if (selectedSolution != null) { package = package.SafeToString(); if (package.Equals("full", StringComparison.OrdinalIgnoreCase)) { xmlToRun = selectedSolution.FullXml; } else if (package.StartsWith("incremental-", StringComparison.OrdinalIgnoreCase)) { var pieces = package.Split('-'); if (pieces.Length > 2) { var fromVersion = pieces[1]; var toVersion = pieces[2]; xmlToRun = selectedSolution.IncrementalXml.ToList().Find((x) => { return(x.GetAttributeValue("BaseVersion").Equals(fromVersion.SafeToString(), StringComparison.OrdinalIgnoreCase) && x.GetAttributeValue("TargetVersion").Equals(toVersion.SafeToString(), StringComparison.OrdinalIgnoreCase)); }); } } if (xmlToRun != null) { SqlExecutor sqlExecutor = new SqlExecutor(outputDelegate, selectedSolution.ConnectionSetting, xmlToRun); sqlExecutor.Execute(solutionName); } else { outputDelegate("No matched SQL package to run."); } } else { } } catch (Exception ex) { outputDelegate(ex.Message); } finally { outputDelegate("Execution is ended at " + DateTime.Now + Environment.NewLine); } }
/// <summary> /// Initializes a new instance of the <see cref="SqlExecutor" /> class. /// </summary> /// <param name="writeOutputDelegate">The write output delegate.</param> /// <param name="connectionSetting">The connection setting.</param> /// <param name="runningXml">The running XML.</param> public SqlExecutor(WriteOutputDelegate writeOutputDelegate, ConnectionStringModel connectionSetting, XElement runningXml) { this.writeOutputDelegate = writeOutputDelegate; connectionModel = connectionSetting; if (runningXml != null) { this.runningXml = runningXml; isFull = runningXml.Name.LocalName.Equals("full", StringComparison.InvariantCultureIgnoreCase); } ScriptHelper.WriteOutputDelegate = writeOutputDelegate; }
/// <summary> /// Initializes a new instance of the <see cref="SqlExecutor" /> class. /// </summary> /// <param name="writeOutputDelegate">The write output delegate.</param> /// <param name="connectionSetting">The connection setting.</param> /// <param name="runningXml">The running XML.</param> public SqlExecutor(WriteOutputDelegate writeOutputDelegate, ConnectionStringModel connectionSetting, XElement runningXml) { this.writeOutputDelegate = writeOutputDelegate; connectionModel = connectionSetting; if (runningXml != null) { this.runningXml = runningXml; isFull = runningXml.Name.LocalName.Equals("full", StringComparison.InvariantCultureIgnoreCase); } ScriptHelper.WriteOutputDelegate = writeOutputDelegate; }
/////////////////////////////////////////////////////////////////////////////// // Public Functions. This is the stuff we want to expose to the // application... /////////////////////////////////////////////////////////////////////////////// #region Public Functions... /// <summary> /// Instantiate TWAIN and open the DSM. This looks like a ridiculously /// complex function, so lets talk about it for a moment. /// /// There are four groupings in the argument list (and wouldn't it be nice /// it they were all together): /// /// The Application Identity (TW_IDENTITY) /// a_szManufacturer, a_szProductFamily, a_szProductName, a_u16ProtocolMajor, /// a_u16ProtocolMinor, a_aszSupportedGroups, a_szTwcy, a_szInfo, a_szTwlg, /// a_u16MajorNum, a_u16MinorNum. /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ /// One of the goals of the TWAINWorkingGroupToolkit namespace is to make it /// unnecessary for the caller to include the TWAINWorkingGroup namespace. /// So there's no appeal to the TW_IDENTITY structure, instead it's broken /// out piecemeal. The structure has been unchanged since 1993, so I think /// we can trust that these arguments won't change. You can read about /// TW_IDENTITY in the TWAIN Specification, but essentially these arguments /// identify the application to the TWAIN DSM and the TWAIN driver. /// /// The Flags /// a_blUseLegacyDSM, a_blUseCallbacks, a_setmessagefilterdelegate /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ /// a_blUseLegacyDSM should be false on Windows and Linux and true on the /// Mac (until we get a new DSM). This causes the toolkit to invoke the /// open source DSM provided by the TWAIN Working Group, which can be found /// here: https://sourceforge.net/projects/twain-dsm/. a_blUseCallbacks /// should be true, since the callback system is easier to manage and less /// likely to cause an application's user interface to lock up while they /// are scanning (the alternative is the Windows POST message system). If /// the value is false, then a_setmessagefilterdelegate must point to a /// function that will filter Window's messages sent from the application /// to the driver. /// /// The Callback Functions /// a_writeoutputdelegate, a_reportimagedelegate /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ /// a_writeoutputdelegate is only used by the TWAINCStst application to show /// information in the status window. A regular application might find that /// useful for diagnostics, but it's not necessary, and the value can be set /// to null. a_reportimagedelegate is the really interesting function, this /// is what's called for every image while scanning. It receives both the /// metadata and the image. You'll want to carefully look at the function /// that's used for the TWAINCSscan application. /// /// Windows Cruft /// a_intptrHwnd, a_runinuithreaddelegate, a_objectRunInUiThreadDelegate /// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ /// TWAIN has been around since 1992, and it's one architectural drawback /// comes from how it tied itself to the Windows message loop (which I'm /// sure seemed like a very good idea at the time). We have three functions, /// and the motivation for this is to avoid accessing System.Windows.Forms /// inside of TWAINCSToolkit, so that we can seamlessly work with other /// graphical windowing systems, such as are provided with Mono). I won't /// go into too much detail here. You must have a Form on Windows. The /// the this.Handle is passed to a_intptrHwnd, which is used by both the /// DAT_PARENT and DAT_USERINTERFACE operations. a_objectRunInUiThreadDelegate /// is the this value, itself, and is used by the a_runinuithreaddelegate to /// invoke DAT_USERINTERFACE and DAT_IMAGE*XFER calls into the form's main /// UI thread, where the Windows message loop resides. This is necessary, /// because some TWAIN driver's hook into that message loop, and will crash /// or hang, if not properly invoked from that thread. If you run into this /// kind of situation, take of note of the operation that caused the problem, /// and if it's clearly an invokation issue it can be fixed by adding new /// TWAIN CS operations to this kind of callback route. As for the function /// itself, just copy the RunInThreadUi function from TWAINCSscan, and use /// it as-is. /// /// </summary> /// <param name="a_intptrHwnd">Parent window (needed for Windows)</param> /// <param name="a_writeoutputdelegate">Optional text output callback</param> /// <param name="a_reportimagedelegate">Optional report image callback</param> /// <param name="m_setmessagefilterdelegate">Optional message filter callback</param> /// <param name="a_szManufacturer">Application manufacturer</param> /// <param name="a_szProductFamily">Application family</param> /// <param name="a_szProductName">Name of the application</param> /// <param name="a_u16ProtocolMajor">TWAIN protocol major (doesn't have to match TWAINH.CS)</param> /// <param name="a_u16ProtocolMinor">TWAIN protocol minor (doesn't have to match TWAINH.CS)</param> /// <param name="a_aszSupportedGroups">Bitmask of DG_ flags</param> /// <param name="a_szTwcy">Application's country code</param> /// <param name="a_szInfo">Info about the application</param> /// <param name="a_szTwlg">Application's language</param> /// <param name="a_u16MajorNum">Application's major version</param> /// <param name="a_u16MinorNum">Application's minor version</param> /// <param name="a_blUseLegacyDSM">The the legacy DSM (like TWAIN_32.DLL)</param> /// <param name="a_blUseCallbacks">Use callbacks (preferred)</param> /// <param name="a_runinuithreaddelegate">delegate for running in the UI thread</param> /// <param name="a_objectRunInUiThreadDelegate">the form from that thread</param> public TWAINCSToolkit ( IntPtr a_intptrHwnd, WriteOutputDelegate a_writeoutputdelegate, ReportImageDelegate a_reportimagedelegate, SetMessageFilterDelegate a_setmessagefilterdelegate, string a_szManufacturer, string a_szProductFamily, string a_szProductName, ushort a_u16ProtocolMajor, ushort a_u16ProtocolMinor, string[] a_aszSupportedGroups, string a_szTwcy, string a_szInfo, string a_szTwlg, ushort a_u16MajorNum, ushort a_u16MinorNum, bool a_blUseLegacyDSM, bool a_blUseCallbacks, TWAINCSToolkit.RunInUiThreadDelegate a_runinuithreaddelegate, Object a_objectRunInUiThreadDelegate ) { TWAIN.STS sts; uint u32SupportedGroups; // Init stuff... m_intptrHwnd = a_intptrHwnd; if (a_writeoutputdelegate == null) { WriteOutput = WriteOutputStub; } else { WriteOutput = a_writeoutputdelegate; } ReportImage = a_reportimagedelegate; SetMessageFilter = a_setmessagefilterdelegate; m_szImagePath = null; m_iImageCount = 0; m_runinuithreaddelegate = a_runinuithreaddelegate; m_objectRunInUiThreadDelegate = a_objectRunInUiThreadDelegate; // Convert the supported groups from strings to flags... u32SupportedGroups = 0; foreach (string szSupportedGroup in a_aszSupportedGroups) { TWAIN.DG dg = (TWAIN.DG)Enum.Parse(typeof(TWAIN.DG), szSupportedGroup.Remove(0, 3)); if (Enum.IsDefined(typeof(TWAIN.DG), dg)) { u32SupportedGroups |= (uint)dg; } } // Instantiate TWAIN, and register ourselves... m_twain = new TWAIN ( a_szManufacturer, a_szProductFamily, a_szProductName, a_u16ProtocolMajor, a_u16ProtocolMinor, u32SupportedGroups, (TWAIN.TWCY)Enum.Parse(typeof(TWAIN.TWCY), a_szTwcy), a_szInfo, (TWAIN.TWLG)Enum.Parse(typeof(TWAIN.TWLG), a_szTwlg), a_u16MajorNum, a_u16MinorNum, a_blUseLegacyDSM, a_blUseCallbacks, DeviceEventCallback, ScanCallback, RunInUiThread, m_intptrHwnd ); // Store some values... m_blUseCallbacks = a_blUseCallbacks; // Our default transfer mechanism... m_twsxXferMech = TWAIN.TWSX.NATIVE; // Our default file transfer info... m_twsetupfilexfer = default(TWAIN.TW_SETUPFILEXFER); m_twsetupfilexfer.Format = TWAIN.TWFF.TIFF; if (TWAIN.GetPlatform() == TWAIN.Platform.WINDOWS) { m_twsetupfilexfer.FileName.Set(Path.GetTempPath() + "img"); } else if (TWAIN.GetPlatform() == TWAIN.Platform.LINUX) { m_twsetupfilexfer.FileName.Set(Path.GetTempPath() + "img"); } else if (TWAIN.GetPlatform() == TWAIN.Platform.MACOSX) { m_twsetupfilexfer.FileName.Set("/var/tmp/img"); } else { Log.Assert("Unsupported platform..." + TWAIN.GetPlatform()); } // We've not been in the scan callback yet... m_blScanStart = true; // Open the DSM... try { sts = m_twain.DatParent(TWAIN.DG.CONTROL, TWAIN.MSG.OPENDSM, ref m_intptrHwnd); } catch (Exception exception) { Log.Error("OpenDSM exception: " + exception.Message); sts = TWAIN.STS.FAILURE; } if (sts != TWAIN.STS.SUCCESS) { Log.Error("OpenDSM failed..."); Cleanup(); throw new Exception("OpenDSM failed..."); } }
/// <summary> /// Runs the bat command. /// </summary> /// <param name="batFilePath">The bat file path.</param> /// <param name="outputDelegate">The output delegate.</param> public static void RunBatCommand(string batFilePath, WriteOutputDelegate outputDelegate) { if (!string.IsNullOrWhiteSpace(batFilePath)) { Process batProcess = new Process(); // Redirect the output stream of the child process. batProcess.StartInfo.UseShellExecute = false; batProcess.StartInfo.RedirectStandardOutput = true; batProcess.StartInfo.RedirectStandardError = true; batProcess.StartInfo.FileName = batFilePath; if (outputDelegate != null) { batProcess.OutputDataReceived += (sender, args) => outputDelegate(args.Data); batProcess.ErrorDataReceived += (sender, args) => outputDelegate(args.Data); } batProcess.Start(); batProcess.BeginOutputReadLine(); //StreamReader streamReader = batProcess.StandardOutput; //// Read the standard output of the spawned process. //string output = streamReader.ReadLine(); //outputDelegate(output); batProcess.WaitForExit(); batProcess.Close(); } }
public void Output(int level, String text) { WriteOutputDelegate outputDelegate = new WriteOutputDelegate(WriteOutput); this.textBoxOutput.Invoke(outputDelegate, new Object[] { level, text }); }