Starting multiple simpultaneous WorkAsync leaves infopanel

Oct 30, 2014 at 12:40 PM

When writing a plugin using the (excellent!) PluginBase and starting multiple WorkAsync simultaneously, only the first created infoPanel is disposed, leaving the others on top in the window.
if (host.Controls.Contains(_infoPanel)) 
evaluates to false for the rest of them.

Is there a way around this, or is it a known problem?

Oct 31, 2014 at 1:41 PM
Edited Oct 31, 2014 at 2:26 PM
Thanks for the PluginBase shout out. I created it for my projects and was hoping others would find it useful as well!

Currently creating multiple simultaneous WorkAsync calls is not supported. I'm not even sure it should be. It's primary purpose is to easily give you a thread that isn't on the main UI thread, for performing an action. If you'd like to do additional multi-threading, I'd use the Parallel Task Library from within the workAsync work Action. As long as you pass the Background worker to the new threads, they can update the Message Panel as well.

In order to implement being able to have multiple WorkAsync calls work correctly, you'd have to keep a collection of each control and dispose and use the correct one. I'll look into it if there is a valid use case for it, but currently I don't see it.
Marked as answer by Rappen on 11/3/2014 at 6:37 AM
Nov 3, 2014 at 9:16 AM
Ah, got it.
I fire events to read data from CRM from onChange on some controls on the form, and that combined with reading values from settings file on startup gives me the problem as described.
Guess I will have to redesign some of it then!
Nov 3, 2014 at 2:33 PM
Normally whenever a user triggers a event that requires the WorkAsync, I disable the form until that request is done to prevent race conditions, and continually keep the plugin in a valid state. If there are multiple requests being needed to be made to CRM, they can all be done in a single WorkAsync call.

Reading values on start up should take only a milliseconds or two, and therefore, wouldn't need to be in a WorkAsync call.

Let me know if you need more help!
Nov 3, 2014 at 2:36 PM
Thanks for the tips :)

During startup, it is not actually the reading that is done async, but when values have been read they are written to textboxes etc on the form, which in turn fire the change-events that start WorkAsyncs...

I will redesign some of this though.
Nov 3, 2014 at 2:46 PM
Ah.... that makes more sense.

You could make a Class level IsLoaded Property that you set to false before you read from the config file, then in the event handlers, do nothing if that isn't true. After all the config values have been loaded, set IsLoaded to true so future updates fire correctly, and call a sync WorkAsync method that performs all of your CRM calls within a single WorkAsync.

I'm thinking that would require the least amount of changes for you.