Community
Showing results for 
Search instead for 
Do you mean 
Reply

Error createing a custom sub enity in a background worker

Tuned Listener
Posts: 10
Country: United_Kingdom

Error createing a custom sub enity in a background worker

Hi im using a background worker to create a couple of custom subentiies in an Ac16 Database, the worker just shows a please wait dialog while its processing and closes it when the worker has finished. Its giving a weird threading error while creating one of the entities (others are ok).

 

Has anybody seen something like this before ?

 

===================================

Controls created on one thread cannot be parented to a control on a different thread. (System.Windows.Forms)

------------------------------
Program Location:

at System.Windows.Forms.Control.ControlCollection.Add(Control value)
at System.Windows.Forms.TabControl.ControlCollection.Add(Control value)
at System.Windows.Forms.TabControl.TabPageCollection.Add(TabPage value)
at Act.UI.ContactViews.ContactDetailView.AddBackofficeTab(ILiveDataTab liveDataTab)
at Act.UI.ContactViews.ContactDetailView.OnLoad(ActApplication actApp)
at Act.UI.ContactViews.ContactBaseView.OnSchemaChanged()
at Act.UI.ContactViews.ContactDetailView.OnSchemaChanged()
at Act.Framework.MetaData.SchemaMetaData.SchemaChangedHandler.Invoke()
at Act.Framework.MetaData.SchemaMetaData.Reload(Boolean raiseEvents)
at Act.Framework.Database.DescriptorBaseManager.RefreshSchema(Boolean raiseEvents)
at Act.Framework.Database.DescriptorBaseManager.OnSchemaChanged()
at Act.Framework.Database.DefineEntityManager.CreateSubentity(CreateSubentityInfo& si)
at Act.Framework.Database.DefineEntityManager.CreateSubentity(String entityName, String displayName, Guid[] entityId, Boolean isDefineField, String description)
at Act.Framework.CustomEntities.CustomEntityManager.CreateCustomSubEntity(String entityName, String displayName, ParentEntity parentEntities, Boolean isDefineField, String description)
at AlphaLogix.Aim50.Plugins.Invoices.InvoiceItemEntityDescriptorFactory.CreateEntity() in e:\p4_pixel1666_HADES\AlphaLogix.Aim50.SDK\AlphaLogix.Aim50.Plugins\Invoices\InvoiceItemEntityDescriptorFactory.cs:line 137

 

Nickel Contributor
Posts: 175
Country: USA

Re: Error createing a custom sub enity in a background worker

A background worker, I believe, creates a completely new thread. Any calls to the UI must be marshalled, so the UI thread ultimately makes the call.

 

In the code below, I show an example of how this can be done. This code is located on a form (UI), and the routine "Foo" is actually a callback method (I removed a bunch of the specifics for simplicity), which means it's on a different thread than the UI. Since I want to interact with the form (Me), I must initiate the call from an item on the UI thread, with Me being the easiest object.

Private Sub Foo()
   'some task
     Me.Invoke(New delUpdateLog(AddressOf UpdateLog), strMsg)
   'finalize/clean-up
End Sub


Private Delegate Sub delUpdateLog(ByVal str As String)
Private Sub UpdateLog(ByVal str As String)
        rtbLog.AppendText(vbCrLf & str)
End Sub

 

In your case, you'd have to have a control object as your parameter in the Delegate and respective method to be invoked. Then in that method, you'd need to add the passed in control to the proper parent control. I hope this helps.

 

Tuned Listener
Posts: 10
Country: United_Kingdom

Re: Error createing a custom sub enity in a background worker

Im not actually doing an UI work in the thread ...the line throwing the error is a call to the act sdk method as follows, ie its the act sdk throwing an error ...not any ui code 

 

CustomEntityDescriptor descriptor = framework.CustomEntities.CreateCustomSubEntity(
InvoiceItemEntityDefinition.ENTITY_NAME, // unique name
InvoiceItemEntityDefinition.ENTITY_DISPLAY_NAME, // display name
InvoiceItemEntityDefinition.PARENT_ENTITY,
// primary entities the custom sub-entity will be associated with
true, // do we want this in define fields (future use)
InvoiceItemEntityDefinition.ENTITY_DESCRPTION); // description of the entity

Nickel Contributor
Posts: 175
Country: USA

Re: Error createing a custom sub enity in a background worker

While you may not be directly calling the UI, the ACT SDK is, as a result of you calling a specific method. The stack trace you posted shows as much. You will probably have to marshal the CreateCustomSubEntity call. It looks like you could make that into a function that takes the type of InvoiceItemEntityDefinision and returns a CustomEntityDescriptor.

On a slightly different note, since you are showing a wait dialog, it would seem that you want to block the user from doing anything else while your code is creating the sub entities. If this is the case, why use a background worker at all? Would changing the cursor type to "waiting" or "busy" be sufficient while the code runs, maybe with a message up front stating it may take a while? I'm merely asking because adding multi-threading into a project has the potential add quite a bit of complexity that you may not want.
Tuned Listener
Posts: 10
Country: United_Kingdom

Re: Error createing a custom sub enity in a background worker

Taken away the background worker and it went through ok ......strangely the problem only happened when going live on the customers machine ....didnt happen at all during testing on multiple vms and real machines. Thanks for help

Nickel Contributor
Posts: 175
Country: USA

Re: Error createing a custom sub enity in a background worker

Just a guess, but I think what may have been happening is that they had that specific view pulled up when the custom entities where added, so the UI was trying to update the information as it came in. Then the cross-threading issues would come up. In your testing, you may not have had that view displayed, so you never ran into the issue.