<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>topic Re: Process Id in C# in Developers</title>
    <link>https://communities.sas.com/t5/Developers/Process-Id-in-C/m-p/684010#M941</link>
    <description>&lt;P&gt;There is probably a better way but this worked. I wrote an extension method in case it is needed:&lt;BR /&gt;&lt;BR /&gt;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;        public static Dictionary&amp;lt;string, string&amp;gt; GetAutoVars(this Workspace ws)
        {
            try
            {
                var lang = ws.LanguageService;
                lang.LineSeparator = "\r\n";
                var code = string.Join(Environment.NewLine, "OPTIONS PAGESIZE=MAX; %PUT --BEGIN--; %PUT _ALL_; %PUT --END--;");
                lang.Submit(code);

                #pragma warning disable CS0219
                const LanguageServiceCarriageControl cc = new LanguageServiceCarriageControl();
                const LanguageServiceLineType lt = new LanguageServiceLineType();
                #pragma warning restore CS0219

                lang.FlushLogLines(100, out Array carriage, out Array lineTypes, out Array lines);
                var lineList = lines.OfType&amp;lt;string&amp;gt;().ToList();
                var start = lineList.IndexOf("--BEGIN--");
                var finish = lineList.IndexOf("--END--");
                var recs = lineList.Skip(start + 1).Take(finish - start - 1).Select(p =&amp;gt; p.Replace("AUTOMATIC", string.Empty).Trim().Split(' ', 2)).ToList();
                var autoVars = recs.ToDictionary(p =&amp;gt; p[0].ToString(), p =&amp;gt; p.Length == 2 ? p[1]?.ToString() : null);
                return autoVars;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
&lt;/CODE&gt;&lt;/PRE&gt;</description>
    <pubDate>Tue, 15 Sep 2020 17:30:13 GMT</pubDate>
    <dc:creator>AlanC</dc:creator>
    <dc:date>2020-09-15T17:30:13Z</dc:date>
    <item>
      <title>Process Id in C#</title>
      <link>https://communities.sas.com/t5/Developers/Process-Id-in-C/m-p/683749#M937</link>
      <description>&lt;P&gt;If I start a SAS Workspace object, I can name it:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;BLOCKQUOTE&gt;
&lt;P&gt;var ws = new SAS.Workspace{Name = "SASWS_0"};&lt;/P&gt;
&lt;/BLOCKQUOTE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Then, if I need to search through the processes running on the machine, I can find all instances of SAS using the following:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;BLOCKQUOTE&gt;
&lt;P&gt;var sasProcs = Process.GetProcessesByName("sas");&lt;/P&gt;
&lt;/BLOCKQUOTE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;If sasProcs has 10 entries, how can I get the one associated with the ws object? Currently, I need to check the process MainWindowHandle to check if it is a 0 to find the workspace but I don't think that is the best way to handle it (pun intended).&lt;/P&gt;</description>
      <pubDate>Mon, 14 Sep 2020 18:31:41 GMT</pubDate>
      <guid>https://communities.sas.com/t5/Developers/Process-Id-in-C/m-p/683749#M937</guid>
      <dc:creator>AlanC</dc:creator>
      <dc:date>2020-09-14T18:31:41Z</dc:date>
    </item>
    <item>
      <title>Re: Process Id in C#</title>
      <link>https://communities.sas.com/t5/Developers/Process-Id-in-C/m-p/683867#M938</link>
      <description>&lt;P&gt;Hi Alan,&lt;/P&gt;&lt;P&gt;i would query the workspace for the automatic macro variable "SYSJOBID". This is the process id of your SAS process. With the PID it is easy to find that process in your operating system.&lt;/P&gt;&lt;P&gt;Best Regards&lt;/P&gt;&lt;P&gt;Andreas&lt;/P&gt;</description>
      <pubDate>Tue, 15 Sep 2020 06:07:04 GMT</pubDate>
      <guid>https://communities.sas.com/t5/Developers/Process-Id-in-C/m-p/683867#M938</guid>
      <dc:creator>AndreasMenrath</dc:creator>
      <dc:date>2020-09-15T06:07:04Z</dc:date>
    </item>
    <item>
      <title>Re: Process Id in C#</title>
      <link>https://communities.sas.com/t5/Developers/Process-Id-in-C/m-p/683960#M939</link>
      <description>&lt;P&gt;Thanks Andreas. When I start the Workspace, how do I know what PID it is running under? If I have the process Windows, how do I get to the Workspace object? Either one should allow the meeting of the 2 but I am missing something here. I may have it in some old code but have not searched through everything.&lt;/P&gt;</description>
      <pubDate>Tue, 15 Sep 2020 14:24:10 GMT</pubDate>
      <guid>https://communities.sas.com/t5/Developers/Process-Id-in-C/m-p/683960#M939</guid>
      <dc:creator>AlanC</dc:creator>
      <dc:date>2020-09-15T14:24:10Z</dc:date>
    </item>
    <item>
      <title>Re: Process Id in C#</title>
      <link>https://communities.sas.com/t5/Developers/Process-Id-in-C/m-p/683962#M940</link>
      <description>Ahh, never miind, I see what you are saying. Let me get back.</description>
      <pubDate>Tue, 15 Sep 2020 14:25:18 GMT</pubDate>
      <guid>https://communities.sas.com/t5/Developers/Process-Id-in-C/m-p/683962#M940</guid>
      <dc:creator>AlanC</dc:creator>
      <dc:date>2020-09-15T14:25:18Z</dc:date>
    </item>
    <item>
      <title>Re: Process Id in C#</title>
      <link>https://communities.sas.com/t5/Developers/Process-Id-in-C/m-p/684010#M941</link>
      <description>&lt;P&gt;There is probably a better way but this worked. I wrote an extension method in case it is needed:&lt;BR /&gt;&lt;BR /&gt;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;        public static Dictionary&amp;lt;string, string&amp;gt; GetAutoVars(this Workspace ws)
        {
            try
            {
                var lang = ws.LanguageService;
                lang.LineSeparator = "\r\n";
                var code = string.Join(Environment.NewLine, "OPTIONS PAGESIZE=MAX; %PUT --BEGIN--; %PUT _ALL_; %PUT --END--;");
                lang.Submit(code);

                #pragma warning disable CS0219
                const LanguageServiceCarriageControl cc = new LanguageServiceCarriageControl();
                const LanguageServiceLineType lt = new LanguageServiceLineType();
                #pragma warning restore CS0219

                lang.FlushLogLines(100, out Array carriage, out Array lineTypes, out Array lines);
                var lineList = lines.OfType&amp;lt;string&amp;gt;().ToList();
                var start = lineList.IndexOf("--BEGIN--");
                var finish = lineList.IndexOf("--END--");
                var recs = lineList.Skip(start + 1).Take(finish - start - 1).Select(p =&amp;gt; p.Replace("AUTOMATIC", string.Empty).Trim().Split(' ', 2)).ToList();
                var autoVars = recs.ToDictionary(p =&amp;gt; p[0].ToString(), p =&amp;gt; p.Length == 2 ? p[1]?.ToString() : null);
                return autoVars;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Tue, 15 Sep 2020 17:30:13 GMT</pubDate>
      <guid>https://communities.sas.com/t5/Developers/Process-Id-in-C/m-p/684010#M941</guid>
      <dc:creator>AlanC</dc:creator>
      <dc:date>2020-09-15T17:30:13Z</dc:date>
    </item>
    <item>
      <title>Re: Process Id in C#</title>
      <link>https://communities.sas.com/t5/Developers/Process-Id-in-C/m-p/684134#M942</link>
      <description>&lt;P&gt;Hi Alan, i guess you also query your SAS Workspace for data. With the SAS OleDB Driver it is just a simple SQL-query against the SAS internal dictionary tables (also available in the SASHELP Library):&lt;/P&gt;&lt;PRE&gt;select name, value from dictionary.macros;&lt;/PRE&gt;&lt;P&gt;or&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;select name, value from sashelp.vmacro;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Wed, 16 Sep 2020 06:27:40 GMT</pubDate>
      <guid>https://communities.sas.com/t5/Developers/Process-Id-in-C/m-p/684134#M942</guid>
      <dc:creator>AndreasMenrath</dc:creator>
      <dc:date>2020-09-16T06:27:40Z</dc:date>
    </item>
    <item>
      <title>Re: Process Id in C#</title>
      <link>https://communities.sas.com/t5/Developers/Process-Id-in-C/m-p/686802#M949</link>
      <description>&lt;P&gt;There is another way to get this without submitting code or querying for macro variables.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;The process ID is among the values available from IHostSystem, which you can get to from IUtilities, which you can get from the Workspace.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;// -- given a SASWorkspace ws
//----- get utilities interface
IUtilities iUtil=ws.Utilities;

//----- get HostSystem interface
IHostSystem iHostSystem=iUtil.HostSystem;

//----- get SAS version, process ID, and other info - in softwareInfo array
Array softwareInfo, hardwareInfo;
iHostSystem.GetInfo(out softwareInfo, out hardwareInfo);&lt;/PRE&gt;
&lt;P&gt;Some &lt;A href="https://www.lexjansen.com/nesug/nesug05/io/io12.pdf" target="_self"&gt;secrets for decoding are in Peter Eberhardt's paper&lt;/A&gt;.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;This would probably perform faster...even if it's a little convoluted.&lt;/P&gt;</description>
      <pubDate>Fri, 25 Sep 2020 18:34:58 GMT</pubDate>
      <guid>https://communities.sas.com/t5/Developers/Process-Id-in-C/m-p/686802#M949</guid>
      <dc:creator>ChrisHemedinger</dc:creator>
      <dc:date>2020-09-25T18:34:58Z</dc:date>
    </item>
  </channel>
</rss>

