cancel
Showing results for 
Search instead for 
Did you mean: 

OpenUI execute Agentry Action failed

Former Member
0 Kudos

There is an issue: when Agentry action triggered from OpenUI, it is not executed and cause AG client crash under certain scenario.

The OpenUI is defined at MainMenuScreen: spawn a background thread to monitoring a folder for new file creation, if certain file e.g. 12345678.nav is found created in that folder, then OpenUI calls Agentry action to navigate to the Work Order list screen and do list selection of WO 12345678.

1. If the Agentry Screen is at MainMenu, BUT the Windows OS focus (active window) is at other process say notepad.exe, then the Navigation step never occurs and cause AG client crash

2. If the Agentry Screen is at MainMenu, AND the Windows OS focus is at Agentry process, then the Navigation step occurs and proper WO is selected afterwards

3. If the Agentry Screen is already at Work Order list screen, it does not matter who has the Windows OS focus, then proper WO is selected (here, Navigation seems to be ignored)

                // Specify what is done when a file is created.

                string[] files = Directory.GetFiles(navwodir, "*nav");

                wo = Path.GetFileNameWithoutExtension(files[0]);

              

                //if (System.IO.File.Exists(@"" + navwodir + wo + ".nav"))

                //{

                //    System.IO.File.Delete(@"" + navwodir + wo + ".nav");

                //}

                if (Application.Current.Dispatcher.CheckAccess())

                {

                    MessageBox.Show("Safe on Main");

                    CallAgentryAction(wo);

                }

                else

                {

                    // bg thread

                    Application.Current.Dispatcher.BeginInvoke

                    (

                        new Action

                        (

                            () =>

                            {

                                CallAgentryAction(wo);

                            }

                        )

                    );

                }

Message was edited by: jinlin wang The monitoring process uses STATIC BackgroundWorker.  The worker_DoWork basically use FileSystemWatcher to monitor a folder.             private static BackgroundWorker worker = null;              if (worker == null)             {                                 worker = new BackgroundWorker();                 //worker.WorkerReportsProgress = true;                 worker.DoWork += worker_DoWork;                 worker.WorkerSupportsCancellation = true;                                //worker.ProgressChanged += worker_ProgressChanged;                 worker.RunWorkerCompleted += worker_RunWorkerCompleted;                 if (worker.IsBusy != true)                 {                     worker.RunWorkerAsync();                 }             }

Accepted Solutions (1)

Accepted Solutions (1)

Former Member
0 Kudos

Found a workaround of the issue:

Set the Window Thread in focus first before call the Agentry Action

[DllImport("user32.dll")]

private static extern bool ShowWindow(IntPtr hWnd, int  nCmdShow);

parentWindow.Topmost = true;

ShowWindow(windowHandle, 9);

CallAgentryAction(wo);

mark_pe
Active Contributor
0 Kudos

Jinlin,

If this is a good enough workaround can you mark this SCN article as answered with your solution so that your solution will be put at the very top.

Thanks,

Mark Pe
SAP Platinum Support Engineer

Answers (2)

Answers (2)

Former Member
0 Kudos

This seems like it should be a BCP ticket. When is this code snippet being called? Why would it be executed on a background thread? Does CheckAccess() ever return false? I think queuing up the call to CallAgentryAction() on the main thread could be causing the problem.

Former Member
0 Kudos

This OpenUI code along with BG thread is being called when the users click Menu button from very first screen to the MainMenuScreen.  If the BackgroundWorker is not used, say define the FileSystemWatcher at main thread, AG client crashes right away when new file is created - see the attachment.  Another reason is: if FileSystemWatcher is defined at main and monitoring with "while(true)" looping, the MainMenuScreen will never finish loading.

Regarding to CheckAccess(), it always return false.

Former Member
0 Kudos

When you're using the FileSystemWatcher Class (System.IO) you should subscribe to its events, not poll it in an infinite loop. That's probably why it appears that the client is never finishing loading.

Former Member
0 Kudos

Below is the snippet of FileSystemWatcher defined on main thread, there is no loop defined, which causes AG client crashing.  When you say "subscribe to its events", did I do it wrong or there is a different way to do it?  Please give more insight.

     public NavWO()

        {

            InitializeComponent();

         

            ...

            listenIncome(navwodir);

        }

     

        [PermissionSet(SecurityAction.Demand, Name = "FullTrust")]

        public void listenIncome(string navwodir)

        {

            // Create a new FileSystemWatcher

            FileSystemWatcher watcher = new FileSystemWatcher();

       

            // set properties.

            watcher.Path =  navwodir;

            // Watch for changes in LastAccess and LastWrite times, and the renaming of files or directories.

            watcher.NotifyFilter = NotifyFilters.LastAccess | NotifyFilters.LastWrite | NotifyFilters.FileName | NotifyFilters.DirectoryName;

            // Only watch nav files.

            watcher.Filter = "*.nav";

            // Add event handlers.

            // watcher.Changed += new FileSystemEventHandler(OnChanged);

            watcher.Created += new FileSystemEventHandler(OnCreated);

            // watcher.Deleted += new FileSystemEventHandler(OnDeleted);

            // watcher.Renamed += new RenamedEventHandler(OnRenamed);

            // Begin watching.

            watcher.EnableRaisingEvents = true;

            //while (true) ;

        }

        private void OnCreated(object source, FileSystemEventArgs e)

        {

            FileSystemWatcher watcher = source as FileSystemWatcher;

            try

            {

               ...

                string[] files = Directory.GetFiles(navwodir, "*nav");

                wo = Path.GetFileNameWithoutExtension(files[0]);             

            

                CallAgentryAction(wo);

            }         

        }

     

        public void CallAgentryAction(string szID)

        {

            ...

            if (viewModel != null)

            {

                if (viewModel.DoesAgentryActionExist(agaction_txn))

                {

                    viewModel.ExecuteAgentryAction(agaction_txn);

                }            

            }

            else

            {

                this.viewModel = (this.DataContext as IAgentryControlViewModel);

                if (viewModel.DoesAgentryActionExist(agaction_txn))

                {

                    viewModel.ExecuteAgentryAction(agaction_txn);

                }

            }         

        }

        //override

        public string GetExtensionString(string key)

        {

            if (key.Equals("WONum"))

            {

                return wo;

            }

            else

            {

                return "Error";

            }

        }

Message was edited by: jinlin wang From searches over the web, it seems FileSystemWatcher does not need loop: "FileSystemWatcher is using some of the built-in hooks to subscribe to changes and just gets the OS to call your delegates when a change occurs." This is proven by the scenario when using BackgroundWorker, which I did not use a loop and the event keeps triggering.

bill_froelich
Product and Topic Expert
Product and Topic Expert
0 Kudos

Jinlin,

I am guessing the problem is due to the call being made from the wrong context/thread.   I had a similar issue when trying to invoke the Agentry methods when I received Serial Data in my Scanner class.

I needed to Invoke the action through the dispatcher to have it be able to correctly call the Agentry method.  I would try using something similar to the following.

try {

Application.Current.Dispatcher.Invoke(new Action(() => { CallAgentryAction(wo); }));

}

--Bill

Former Member
0 Kudos

Bill,

Sorry, I accidentally removed the full code in the snippet of my last reply.  It is actually like the following, similar as you suggest:

                    Application.Current.Dispatcher.BeginInvoke

                    (

                        new Action

                        (

                            () =>

                            {                              

                                CallAgentryAction(wo);

                            }

                        )

                    );

Thanks,

Jinlin

mark_pe
Active Contributor
0 Kudos

Jinlin,

Hi. What version is your SMP 3.0 Agentry SDK? Need to check if this was reported already. We have several OpenUI fixes I need to match your version with what we have to see if this particular issue about executing CallAgentryAction(wo); is one of them. Also what is your device (iOS/Android) and model? Where did it crash?

Best Regards,

Mark Pe
SAP Platinum Support Engineer

Former Member
0 Kudos

Mark,

Thanks for the help.  I tested under SMP SDK SP09 PL1, the issue still occurs.  This is a WPF client.  It does not crash immediately at the Navigation, it crashed at AG client logout whereas the client just hangs.

I guess there may be Process/Thread handling issue within Agentry client causing the Navigation not working.

Thanks and Regards,

Jinlin

mark_pe
Active Contributor
0 Kudos

Jinlin,

So this is WPF. Let me check around for the WPF team. It's Friday so I may have to check with them next week and I'll check our internal testing (Jira) if they found something. I sorta remember Navigation issue with openUI but I need to compare it with yours but I need to check again.

I'll update this thread once I find it or I have an update.

Regards,

Mark Pe
SAP Platinum Support Engineer

bill_froelich
Product and Topic Expert
Product and Topic Expert
0 Kudos

Jinlin,

Are you shutting down your background thread during shutdown of your Open UI code?

--Bill

Former Member
0 Kudos

Hi Bill,

The background thread keeps running as long as the MainMenuScrene is not exited/closed.  If the screen does get exited/closed, the background thread gets killed/disposed also.  And, Agentry client will throw Windows Exception if there is new file creation triggers the flow.

I did not explicitly kill the thread (according to MS, BackgroundWorker will automatically handle the garbage collection).  From the process explorer, I can see the background thread does get cleaned up after Agentry client logout.

Thanks,

Jinlin

Message was edited by: jinlin wang One Correction on "If the screen does get exited/closed, the background thread gets killed/disposed also.":  I am not 100% sure whether the BG thread was killed/disposed.  I believe the OpenUI control is probably unloaded which causes BG thread stop working and throw exception if new file created. Within the Unloaded Event method, CancelAsync is called on the BackgroundWorker.