I create a Flex Desktop App by Flex builder 4
I wa开发者_开发问答nt to log some debug info into a file , named by process id
//var pid:int = NativeApplication.nativeApplication.getPid() ??
var logFile:String = "/var/log/MyApp_"+pid+".log";
Is these any API to get pid in Flex/ActionScript3 ?
You could get processes from a C# console application and pick out the process that your flex application is using. I made an example of this:
GetProcesses.cs:
using System;
using System.Diagnostics;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace GetProcesses
{
class Program
{
static void Main(string[] args)
{
Process[] processes = System.Diagnostics.Process.GetProcesses();
String processesXml = "";
processesXml += "<processes>\n";
foreach (Process process in processes)
{
processesXml += "\t<process id='" + process.Id + "' name='" + process.ProcessName + "' />\n";
}// end foreach
processesXml += "</processes>";
Console.WriteLine(processesXml);
}// end method
}// end class
}// end namespace
Main.as:
package
{
import flash.desktop.NativeProcess;
import flash.desktop.NativeProcessStartupInfo;
import flash.display.Sprite;
import flash.events.Event;
import flash.events.ProgressEvent;
import flash.filesystem.File;
import flash.utils.ByteArray;
public class Main extends Sprite
{
private var _nativeProcess:NativeProcess;
private var _nativeProcessSUI:NativeProcessStartupInfo;
private var _processes:XML;
public function Main():void
{
_nativeProcess = new NativeProcess();
_nativeProcessSUI = new NativeProcessStartupInfo();
_nativeProcessSUI.executable = File.applicationDirectory.resolvePath("GetProcesses.exe");
_nativeProcess.addEventListener(ProgressEvent.STANDARD_OUTPUT_DATA, onNativeProcessSOD);
_nativeProcess.addEventListener(Event.STANDARD_OUTPUT_CLOSE, onNativeProcessSOC);
_nativeProcess.start(_nativeProcessSUI);
}// end function
private function onNativeProcessSOD(e:ProgressEvent):void
{
_processes = XML(_nativeProcess.standardOutput.readUTFBytes(_nativeProcess.standardOutput.bytesAvailable));
}// end function
private function onNativeProcessSOC(e:Event):void
{
trace(_processes.process.(@name == "GetProcesses").@id); // 7192
}// end function
}// end class
}// end package
The C# console application "GetProcesses.cs" creates an xml of processes using the GetProcesses()
method and the flex application retrieves that xml. In this example I use
trace(_processes.process.(@name == "GetProcesses").@id); // 7192
to get the process ID of the C# console application but obviously you would change the process name in the xml query to the process name your flex application is using.
i don't think that you can find the pid of your currently running application. The only place where you can get some info about your app is NativeApplication.
If you really need the pid you can create a second application that acts like a launcher. This new application starts and create a new process for the real application that you've already created using NativeProcess. In this way you have all the information about the process.
Remember that if you use NativeProcess launching you have some limitation (for example you can't use web install)
Hope this helps
I don't think this is included because each OS has a different way to get process id. You could try to do a NativeProcess call to GetCurrentProcessId but I'm not sure if it'll work.
精彩评论