Error: The calling thread can't access this object because the object is owned by another thread

G. Burghardt 56 Reputation points
2023-06-03T20:34:16.1566667+00:00

Hello,

for some time I have been working through the book "The Developer's Reference Guide To Microsoft Small Basic" by Philip Conrod and Lou Tylee. In Chapter 20 "Animation" I have problems with the sound not playing properly and there is also an error message: "The calling thread cannot access this object because the object is owned by another thread."

Can someone tell me why the error message occurs in the "TickSub()" procedure?

Regards ... Gregor

Answer by "Mohammad Hamdy Ghanem": I review the source code, and seems that the code of the GetMediaPlayer method used in the sound class needs to be wrapped in a call to the SmallBasicApplication.Invoke() method. This also needs to be done with all the sound methods like play and stop.

I did that in sVB and it will be available in a next release.

Note that such questions is better posted at https://docs.microsoft.com/.../topics/small-basic.html

and clearly this is a bug, so it should be reported in the SB editor repo, but I don't anything to be fixed in the desktop version of SB.

ErrorMessage

PublicNumber

Small BASIC
Small BASIC
A programming language created by Microsoft that serves a stepping stone for beginners from block-based coding languages to more complex text-based languages.
280 questions
0 comments No comments
{count} votes

Accepted answer
  1. Small Visual Basic 411 Reputation points
    2023-06-04T13:22:48.3333333+00:00

    The fix is easy:

    In the Sound class, the code of the Play, PlayAndWait, Pause, Stop and GetMediaPlayer methods should be invoked using the SmallBasicApplication.Invoke method.

    I fixed this bug in sVB 2.8.4.3

    This should be fixed also in SB IDE, but it has no new updates for a while.

    1 person found this answer helpful.

2 additional answers

Sort by: Most helpful
  1. LitDev 126 Reputation points
    2023-09-11T21:36:37.5533333+00:00

    There is conflict between the UI and Timer thread. The UI thread owns the internal MediaPlayer objects, but they are being used on the timer thread. This is a common problem in Small Basic. I recommend doing all the 'action' on the UI thread in a game loop, and just using event threads to set flags.

    See https://social.technet.microsoft.com/wiki/contents/articles/22264.small-basic-threading.aspx for more details.
    There are some other issues with the code, but, this is how I would do it using a game loop on the UI thread, RCPQ877.000-0

    1 person found this answer helpful.
    0 comments No comments

  2. G. Burghardt 56 Reputation points
    2023-06-08T16:28:11.4666667+00:00

    Hello Mohammad,

    Is it possible for you to explain the following error message on Small Basic? Which conflict exists while the sub TickSub() is running? What about another thread?

    "The calling thread cannot access this object because the object is owned by another thread." (see: above!!!)

    Regards ... Gregor


Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.