Author Topic: Code locks up TEXTSCR  (Read 1682 times)

0 Members and 1 Guest are viewing this topic.

Keith™

  • Villiage Idiot
  • Seagull
  • Posts: 16899
  • Superior Stupidity at its best
Code locks up TEXTSCR
« on: July 27, 2018, 03:43:50 PM »
I have a short bit of code that gets a selection from the current document and filters for a block reference.

That in itself is unremarkable, however, what is happening is that after selection, if the user opens TEXTSCR, the window is inaccessible as though it is not the topmost window. Trying to click in the window produces the Default Beep.

I've commented out the actual selection portion of the code and have verified that is where the problem starts.

The code is question:
Code - vb.net: [Select]
  1.         Using doc.LockDocument()
  2.             Using acTrans As Transaction = db.TransactionManager.StartTransaction
  3.                 While _currentBlockReference Is Nothing
  4.                     'Removing the next line stops the issue
  5.                     Dim SSet As PromptSelectionResult = ed.GetSelection(pso, IntvolFilter)
  6.                     If (SSet.Status = PromptStatus.OK) Then
  7.                         For Each selObj As SelectedObject In SSet.Value
  8.                             Dim sent As Entity = DirectCast(acTrans.GetObject(selObj.ObjectId, OpenMode.ForRead), Entity)
  9.                             If TypeOf sent Is BlockReference Then
  10.                                 _currentBlockReference = DirectCast(sent, BlockReference)
  11.                             End If
  12.                         Next
  13.                     ElseIf (SSet.Status = PromptStatus.Error) Then
  14.                         ed.WriteMessage(vbCrLf & My.Resources._MustBeIntersection)
  15.                     ElseIf (SSet.Status <> PromptStatus.OK) Then
  16.                         Exit While
  17.                     End If
  18.                 End While
  19.                 If _currentBlockReference Is Nothing = False Then
  20.                     Me.txtCurrent.Text = GetDataFromIntVol(_currentBlockReference, acTrans)
  21.                     Me.txtProposed.Text = GetDataFromIntVol(_currentBlockReference, acTrans, cbxConvertTo.SelectedIndex)
  22.                 End If
  23.                 acTrans.Commit()
  24.             End Using
  25.         End Using

Obviously I can't just remove one line, but I have edited the code slightly to prevent the selection and it doesn't have the issue. Perhaps it is something simple, but I just don't see it.
Proud provider of opinion and arrogance since November 22, 2003 at 09:35:31 am
CadJockey Militia Field Marshal

Find me on https://parler.com @kblackie

MickD

  • King Gator
  • Posts: 3636
  • (x-in)->[process]->(y-out) ... simples!
Re: Code locks up TEXTSCR
« Reply #1 on: July 27, 2018, 07:59:25 PM »
Hi Keith,

I refactored your code a bit to get a clearer understanding, you may want to put an exit in the for each if you only want to get the first block ref data. Hope this helps, cheers.

NOTE: untested!
Code - vb.net: [Select]
  1. Using doc.LockDocument()
  2.     Using acTrans As Transaction = db.TransactionManager.StartTransaction
  3.     ' no need for the while loop and I think it confuses things a bit...
  4.         Dim SSet As PromptSelectionResult = ed.GetSelection(pso, IntvolFilter)
  5.         If (SSet.Status = PromptStatus.OK) Then
  6.             For Each selObj As SelectedObject In SSet.Value
  7.                 Dim sent As Entity = DirectCast(acTrans.GetObject(selObj.ObjectId, OpenMode.ForRead), Entity)
  8.                 If TypeOf sent Is BlockReference Then
  9.                     _currentBlockReference = DirectCast(sent, BlockReference)
  10.                     'we have the block, might as well get it done here,
  11.                     'NOTE: these variables will be reset each for each iteration!!
  12.                     If _currentBlockReference Is Nothing = False Then
  13.                         Me.txtCurrent.Text = GetDataFromIntVol(_currentBlockReference, acTrans)
  14.                         Me.txtProposed.Text = GetDataFromIntVol(_currentBlockReference, acTrans, cbxConvertTo.SelectedIndex)
  15.                     End If
  16.                 End If
  17.             Next
  18.         ElseIf (SSet.Status = PromptStatus.Error) Then
  19.             ed.WriteMessage(vbCrLf & My.Resources._MustBeIntersection)
  20.         ElseIf (SSet.Status <> PromptStatus.OK) Then
  21.             Exit While
  22.         End If
  23.         acTrans.Commit()
  24.     End Using
  25. End Using
  26.  

"Programming is really just the mundane aspect of expressing a solution to a problem."
- John Carmack

"Short cuts make long delays,' argued Pippin.”
- J.R.R. Tolkien

Keith™

  • Villiage Idiot
  • Seagull
  • Posts: 16899
  • Superior Stupidity at its best
Re: Code locks up TEXTSCR
« Reply #2 on: July 27, 2018, 10:34:42 PM »
Thanks for the input.

I'll probably convert the code from GetSelection to GetEntity because I only need one item .. it would make it much simpler ... maybe it will also resolve the underlying issue of TEXTSCR becoming unresponsive after running the code.

That's the real issue at the moment ... when I comment out the GetSelection portion everything works fine.
Proud provider of opinion and arrogance since November 22, 2003 at 09:35:31 am
CadJockey Militia Field Marshal

Find me on https://parler.com @kblackie

MickD

  • King Gator
  • Posts: 3636
  • (x-in)->[process]->(y-out) ... simples!
Re: Code locks up TEXTSCR
« Reply #3 on: July 27, 2018, 11:37:22 PM »
Another possible cause of unintended side effects could be the passing of the transaction to other methods, might not matter in this case but as a rule, I do not pass transactions to methods anymore. I'll spin up another one in the method and commit it there and you can pass the ObjectId instead of a pointer which is safer.
I've also found doing this can be faster even with many entities spinning up a transaction with each method call! (probably because the trans doesn't have to walk back through the stack to finalise everything...just guessing).
"Programming is really just the mundane aspect of expressing a solution to a problem."
- John Carmack

"Short cuts make long delays,' argued Pippin.”
- J.R.R. Tolkien

Keith™

  • Villiage Idiot
  • Seagull
  • Posts: 16899
  • Superior Stupidity at its best
Re: Code locks up TEXTSCR
« Reply #4 on: July 28, 2018, 12:02:40 AM »
I may have figured it out quite by accident … (crossing fingers)

I noticed that when I called the child form from my main form, the problem happened on often but not always and it could only be resolved by closing AutoCAD and re-opening, however, when I opened the child form from the command methods, the problem didn't happen ever … The only difference was this:

Code - vb.net: [Select]
  1. 'Problematic
  2. Dim myform As New ChildForm
  3. myform.ShowDialog()
  4.  
  5. 'Works perfectly
  6. Dim myform As New ChildForm
  7. Application.ShowModelDialog(myform)
  8.  


I actually read somewhere tonight that using ShowDialog was problematic for AutoCAD and that ShowModalDialog was the safe method … like I said, quite by accident I discovered this ...

Now I have to go back and change every instance in this project to keep callbacks at a minimum. A quick check indicates about 70 instances in this project … I've got some work to do!
Proud provider of opinion and arrogance since November 22, 2003 at 09:35:31 am
CadJockey Militia Field Marshal

Find me on https://parler.com @kblackie