0

I have a custom PropertyGrid editor that launches a form and allows the user to make some selections. These selections are used to provide the property value. The form makes a call to a web service to retrieve data for the user to choose from. Currently, this web service call is done synchronously. From what I understand, such calls should ideally be made asynchronously, so as not to freeze the application.

Is there a way that I could do this asynchronously?

It seems like I would need to make my EditValue method async, but the class that I am over-riding (UITypeEditor) does not appear to have this option.

This answer provides a nice background for how I have my custom PropertyGrid editor implemented. However, I will also provide a very basic code snippet to provide a rough idea of what I am trying to do. I'm not using a form in this example, but the important thing to understand is that I am trying to await an async call within my EditValue method.

class FooEditor : UITypeEditor
{
    public override UITypeEditorEditStyle GetEditStyle(ITypeDescriptorContext context)
    {
        return UITypeEditorEditStyle.Modal;
    }
    public override object EditValue(ITypeDescriptorContext context, System.IServiceProvider provider, object value)
    {
        using (var client = MasterContainer.Resolve<ValueWebClient>())
        {
            var value = await client.GetCurrentValue(); // visual studio shows await can only be used in async method error
            return value;
        }
    }
}

I suppose one approach might be to load all of your data up-front so that you don't have to make the call. However, the data could be changed while the user is working, so I would prefer not to take that approach.

Scott
  • 1,223
  • 2
  • 16
  • 35
  • 1
    Are you sure the real problem isn't the inappropriate use of PropertyGrid? A ProrpertyGrid as the name says is a Grid that edits Properties on a single object. Not a generic table UI that makes remote changes. It's not its job to handle persisting that object to a file, database or a remote server. – Panagiotis Kanavos Apr 12 '21 at 14:18
  • The easy solution would be to use PropertyGrid as intended - once all changes are made, persist the entire object asynchronously, eg in response to a button click or menu command. – Panagiotis Kanavos Apr 12 '21 at 14:20
  • 1
    UITypeEditor is a 20-years old .NET Framework-provided class, just check your options here : https://stackoverflow.com/questions/9343594/how-to-call-asynchronous-method-from-synchronous-method-in-c – Simon Mourier Apr 12 '21 at 14:41
  • Thanks for your responses. I think there may be some misunderstanding. I am not making remote changes using the property grid. I am retrieving data from the server that could be constantly changing as I am using my application. Given a fresh set of this data, the user will make a selection, which will set the property value on a single object. – Scott Apr 12 '21 at 15:09

1 Answers1

2

You can't use the async keyword in the EditValue method without changing the return type of the method to Task<object>.

Assuming you own the UITypeEditor base class, this would be a good option.

The current API (object EditValue(ITypeDescriptorContext context, System.IServiceProvider provider, object value) is indeed synchronous though and it's not much you can do about it I am afraid. Loading all data up-front seems to be a good compromise.

mm8
  • 163,881
  • 10
  • 57
  • 88
  • A PropertyGrid is only meant to edit an in-memory object, not handle loading or persisting changes. While one could ask why it didn't get asynchronous data binding like other controls in ..... WPF, the control was never meant to be the ultimate remote and asynchronous grid editor. – Panagiotis Kanavos Apr 12 '21 at 14:24