Thanks Alex, It's coherent answers like this that make this group such a pleasure.
After cutting out bits of your code I was able to get the JEXEServer showing and shutting down properly. The trick with shutdown is to let C#'s garbage collector take out the trash. You might consider amending your example on the wiki page and insert some conditional compilation steps for j602 and beyond. The only issue with the fake invisible J window is a very brief visible flicker during server startup. This may be something J implementers might want to take a look at. Thanks again. On Tue, Aug 11, 2009 at 10:18 PM, Alex Rufon <[email protected]>wrote: > Hi John, > > I know what your problem is. I also hit this when I was migrating from > previous versions of J to the latest version. > > You see, there was a significant change in how J behaves during JEXEServer > startup. (me checking my production code right now). > > There should be a voice over that said, "a few minutes later" ... heheheeh. > Well, after reviewing my code, the significant change is that the new J602 > requires that you initialize a dummy window in J. So my production code does > the following: > //-->> START OF C# Code << > /// <summary> > /// Actual method that initializes the session > /// </summary> > private void initialize() > { > string jScript = @" > BINPATH_z_=: 1!:46'' > ARGV_z_=: ,<'Session Server' > "; > > // Create a new copy of the J Object and make sure were in the Z > locale > jObject = new JEXEServerClass(); > jObject.Quit(); > > // If were debugging, we need to add the debugging script > if (this.debug) > { > // Add the profile load command > jScript += @" > 0!:0 <BINPATH,'\\profile.ijs' > wd 'pn *Session Server' > "; > // Setup the J Session to log and show the J Window > jObject.Log(1); > jObject.Show(1); > } > else > { > // Since were not loading a profile, we need to create a > hidden form > jObject.Do("11!:0'pc Session;cc e editijx;'"); > jObject.Log(0); > jObject.Show(0); > } > > // Now get the base EOE Script > jScript += > UnicodeEncoding.ASCII.GetString(Resources.eoeBaseScript); > > // Load the new script to the current session > this.Variable("loadScript", jScript); > // string temp = (string)this.Variable("loadScript"); > > // Now we execute the values in the loadScript Variable > this.Eval("0!:0 loadScript"); > } > //--->> END OF CODE << > > If you would review, the code does the following: > 1. The code make sure that the following global variables are properly > initialized: > BINPATH_z_=: 1!:46'' > ARGV_z_=: ,<'Session Server' > 2. Initializes the JEXEServerClass and make sure that the instance will > quit when the calling application does: > jObject = new JEXEServerClass(); > jObject.Quit(); > 3. Depending if you want to display a J form, you'll have to do either one > of the following options: > 3a. If you want to display a J form, it is recommended that you > initialize it with your profile: > // Add the profile load command > jScript += @" > 0!:0 <BINPATH,'\\profile.ijs' > wd 'pn *Session Server' > "; > // Setup the J Session to log and show the J Window > jObject.Log(1); > jObject.Show(1); > 3b. You don't need a J form so you just initialize a HIDDEN form so > J won't crash > // Since were not loading a profile, we need to create a > hidden form > jObject.Do("11!:0'pc Session;cc e editijx;'"); > jObject.Log(0); > jObject.Show(0); > > > So to go back and answer your questions one by one: > > 1) The JEXEServer never appears despite being requested with a Show(1) > - Show(1) does not display the form. You need to create a form and display > it through script using the wd command. > > 2) The Quit() does not shutdown the J server > - Qui() only signifies that the session closes when the calling application > quits. If you don't do this, you'll technically have a memory leak. :P > > 3) Exiting the C# app leads to an invalid memory reference in J. > Probably > > related to not shutting down properly. > - This is "feature" with the new J602. Its seems that since you did not > create a window during your session startup, J is trying to close an invalid > window handle hence the crash. :) > > Hope this helps. > > r/Alex > > P.S. > I'm using MS-Outlook 2007 ... beware of auto line break formatting. :P > > -----Original Message----- > From: [email protected] [mailto: > [email protected]] On Behalf Of John Baker > Sent: Wednesday, August 12, 2009 7:08 AM > To: Programming forum > Subject: [Jprogramming] C# and JEXEServer > > During the last few days I have been trying to control a JEXEServer from > C#. I have downloaded, studied > and compiled Alex Rufon's example on the J wiki but I cannot get it to > behave. > > I am using the free Visual C# Express edition. I don't believe there are > any C# version specific issues but I could > be wrong. The following snippet is just about the simplest possible use of > a JEXEServer > > private void button1_Click(object sender, EventArgs e) > { > object jout; > string jstr; > > JEXEServerLib.JEXEServer jObject = new JEXEServerLib.JEXEServer(); > > jObject.Log(1); > jObject.Show(1); > > jObject.Do("jRes=. 'string from J'"); > jObject.GetB("jRes", out jout); > jstr = (string) jout; > > textBox.Text = jstr; > jObject.Quit(); > jObject = null; > } > This almost works with the following exceptions: > > 1) The JEXEServer never appears despite being requested with a Show(1) > 2) The Quit() does not shutdown the J server > 3) Exiting the C# app leads to an invalid memory reference in J. Probably > related to not shutting down properly. > > I can see J executing as j.exe shows in the process list and I can send and > receive data from the server. I have used similiar code in VB for years > and > I judge people (Alex) have managed this from C# as well. Has anyone tried > this recently in any of the Express compilers. > -- > John D. Baker > [email protected] > ---------------------------------------------------------------------- > For information about J forums see http://www.jsoftware.com/forums.htm > ---------------------------------------------------------------------- > For information about J forums see http://www.jsoftware.com/forums.htm > -- John D. Baker [email protected] ---------------------------------------------------------------------- For information about J forums see http://www.jsoftware.com/forums.htm
