Android Question AtomicInteger

mr23

Active Member
Licensed User
Longtime User
Sorry for the delay.
I'm using AsyncStreamsText. Is it meant to guarantee no corruption or loss of data?
Theory is that it allows the (astreams) supplier entry even while it is already invoked and overwrites the sb, since the processing portion holds up the return from astreams_NewData, and the astreams caller can call in at any point due to variable lengths of data 'packets' (sometimes single characters).

Would you agree?

I'm working up a CircularQueue single supplier/consumer and wondered if I could do it sans semaphore, if b4a would respect the java AtomicInteger.

Edit: there was no 'pure' corruption or loss, but a reordering of received substrings of a message. AsyncStreams will break up a serial message into pieces, because it is event driven and once started it doesn't wait for the message to completely arrive, before dispatching. When a modal dialog is used, it blocks the event processing in the entire App, including and even if the AsyncStreams is in a Service. AsyncStreams when unblocked may (and often does) deliver the substrings out of order, so the 'message' received by your app even via AsyncStreamsText will be 'garbled'.
 
Last edited:
Upvote 0

mr23

Active Member
Licensed User
Longtime User
I believe I'm seeing 'bursts' of calls coming in from AsyncStreams->AsyncStreamsText, and a subsequent call appears to be overwriting the data buffer used by the _NewText sub. I think it happens when a longer string of input is following by a much smaller set (say 44 characters, then the next one has 1-3 characters).

I may be wrong, I've only been using B4A for a few weeks. But the first app I downloaded source for suffered from this problem; it was the first B4A code I looked at. When I added a multiple text (string) buffer (in a circular queue), and changed the code to just fill the buffer then callsubdelayed to let the _NewText return ASAP, it cleaned up quite a bit. In fact, as long as my app didn't fail to service the stream it was okay. But if I hit a menu or something, or the phone had something 'else' (outside the app) 'run', the data would no longer be intact. That particular app is pretty rough and is monolithic. I've almost completely rewritten it using modern techniques, and added some interesting functionality. But to play with it you need a 'compatible' BT serial source (the IR-BLUE), or modify the code to accept whatever stream you have available.

When I looked into AsyncStreamsText, it looks like the same type of thing could occur. Not asking for help on this just yet, I should be able to work through this and report back if I find my hypothesis to be true and corrected by the code. If so I'll submit a post here with a new AST that uses a CircularQueue, and an AST that uses it to buffer the input.

Edit: what I didn't know at the time is that Modal Dialogs will block all events, and AsyncStreams responds after the unblock by delivering the message substring packets out of order for some reason.

Anyway, I expect to finish it up shortly and test it hard (I have a 'realtime' Parallax Prop driven project that runs a stream out through a BT port, and I can vary the rate etc.

I had read something the other day about CircularQueues for Single Supplier/Consumer w/o a Semaphore using an Atomic, and thought I'd ask whether B4A supported an atomic. I don't need it to complete my task.

This was the article.
 
Last edited:
Upvote 0

KitCarlson

Active Member
Licensed User
Longtime User
I have found BT communications to be reliable. As I read your post , I wonder if the case of having two CRLF, in a NewText, is not being handled in your code correctly.
 
Upvote 0

KitCarlson

Active Member
Licensed User
Longtime User
It looks like you are using text reader via timer. I have no experience, that it will work.

Why not use AsyncStreams? That has proven to work well for me.
 
Upvote 0

KitCarlson

Active Member
Licensed User
Longtime User
I mean the receive buffer may contain strings, perhaps the end of a longer one, and the shorter one. The part of your program that looks for the CRLF, may send both together for process, resulting in errors. So the short one is there, but processed incorrectly.

I find working with a simple communications example only, is a good way to start. I verify the performance, using logic analyzer for hardware, and simple display means on android. When all is well, then integrate sections of program at a time. Development is much easier to debug that way.
 
Upvote 0

mr23

Active Member
Licensed User
Longtime User
AST is managing the CRLF. Correction. I neglected to reread the original source (linked). It used TextReader & TextWriter. After the first tests and dropped/mangled data, I switched it to AST, didn't see a big improvement. Then added a circular queue of string buffers, which 'solved' the issue, except when the application was 'stalled' by hitting menus/etc.

Thanks for taking a look at it.
 
Last edited:
Upvote 0

mr23

Active Member
Licensed User
Longtime User
To close out this thread, the problems I had stemmed from using the modal Dialogs Library dialogs, in combination with AsyncStreams and derivatives. The modal dialog use in an App activity, will block even a related Service's proper function of AsyncStreams, as it disrupts (blocks) the events. In the case of AsyncStreams, it appears the events are not lost, but the events are delivered out of order after the model/block occurs.

The only method of having a non-modal dialog that I have found so far on the forum, is to create panels of control(s) and manage those instead, meaning no reusable components. A non-modal DialogsLibrary equivalent would be helpful to minimize duplication of effort. If anyone knows of a non-modal dialog library, please reply here.
 
Upvote 0
Top