Select to view content in your preferred language

How to open ProWindow within QueuedTask from RowCreatedEvent?

426
2
11-07-2022 10:10 AM
succip
by
New Contributor III

Hi there,

I'm hoping to open a ProWindow on a RowCreatedEvent/RowModifiedEvent, but I'm getting an error saying "the calling thread cannot access this object because a different thread owns it". 

Essentially, I'm trying to take user input to force update several attributes on the row being created. 

I've tried wrapping everything in the Current.Dispatcher.Invoke() but then my OK relay command is disabled (I assume because the thread is locked).

My code is as follows:

 

public static void OnRowCreated(RowChangedEventArgs args)
        {
            CreateLotWindow createLotWindow = new CreateLotWindow();
            createLotWindow.Owner = FrameworkApplication.Current.MainWindow; //errors out here
            var vm = createLotWindow.DataContext as CreateLotWindowViewModel;
            if (createLotWindow.ShowDialog() == true)
            {
                string lotNo = vm.LotCount;
                QueuedTask.Run(()=> {
                     // do stuff with user input
                }
            }
        }

 

 Any help is greatly appreciated!

2 Replies
afishdev
New Contributor

I am running into this exact same issue except I am calling the ProWindow inside of a map tool's OnSketchCompleteAsync method. My command buttons are greyed out (seemingly due to a thread lock)! Hoping someone has figured this out!

0 Kudos
succip
by
New Contributor III

It'd take me some time to reorient myself in the old project but I can share my current code that does work.

Here's where the window's opened, note the use of BeginInvoke:

ProApp.Current.Dispatcher.BeginInvoke(DispatcherPriority.Normal, (Action)(() =>
                {
                    OpenPlanListWindow();
                }));

 

And here's my OpenPlanListWindow() function:

private static async void OpenPlanListWindow()
        {
            var planListWindow = new PlanListWindow();
            planListWindow.Owner = FrameworkApplication.Current.MainWindow;
            var planListWindowVM = new PlanListWindowViewModel { Plans = _planList };
            planListWindow.DataContext = planListWindowVM;
            var dialogResult = planListWindow.ShowDialog();
            if (dialogResult == true)
            {
                await QueuedTask.Run(() =>
                {
                    Table lotsTable = Data.Sde.OpenDataset<Table>("SPATIAL.cadLots");
                    Inspector insp = new Inspector();
                    insp.Load(lotsTable, _editedOid);
                    var op = new EditOperation();
                    op.Modify(insp);
                    op.Execute();
                });
            }
            else
            {
                if (MapView.Active.Map.OperationManager.CanUndo)
                {
                    await MapView.Active.Map.OperationManager.UndoAsync();
                    FrameworkApplication.AddNotification(new Notification()
                    {
                        Message = "Lot edit canceled",
                        Title = "Operation Canceled"
                    });
                }
            }
        }
0 Kudos