M
M
Mikhail Usotsky2016-11-18 09:54:35
C++ / C#
Mikhail Usotsky, 2016-11-18 09:54:35

How to pass data to a running thread?

I have a thread that runs endlessly in a loop. But there was one problem. I can't pass periodically loaded data to a stream. Tried delegates. Useless.

public void Thread()
        {
            GetMessage msg = new GetMessage(Fail);
            GetCommands val = new GetCommands(Check);

            Ядро_хххх.BasicStructures.Command cmds = new Ядро_хххх.BasicStructures.Command();

            do
            {
                #region Стадия записи.

                if (cmd.Count > 0) cmds = cmd.ElementAt(0);

                cmd.TryDequeue(out cmds); // в пределах класса копится, но здесь остаётся пустым

                if (Device.CheckTxWaiting(ref numBytesWritten))
                    if (Commands.HasFlag(Ядро_хххх.BasicStructures.Command.NotOperation))
                    {
                        Device.Write(хххх.Upload(Outbox, Ядро_хххх.BasicStructures.Command.NotOperation), ref numBytesWritten);
                        IsButton = false;
                        RevisionTxFail = 0;
                    }
                    else
                    {
                        if (Commands.HasFlag(Ядро_хххх.BasicStructures.Command.LoadFPGAAll))
                        {
                            Device.Write(хххх.Upload(Outbox, Ядро_хххх.BasicStructures.Command.LoadFPGAAll), ref numBytesWritten);
                            //Commands = Ядро_хххх.BasicStructures.Command.NotOperation;
                        }
                        else
                        {
                            RevisionTxFail += 1;
                            if (RevisionTxFail > 1000) msg(5);
                        }
                    }
                #endregion

                #region Стадия чтения.
                if (Device.CheckRxQueue(ref numBytesAvailable))
                {
                    Device.Read(numBytesAvailable, ref ReadData, ref numBytesRead);
                    Inbox = хххх.Download(ReadData);
                    RevisionRxFail = 0;
                }
                else
                {
                    RevisionRxFail += 1;
                    if (RevisionRxFail > 1000) msg(5);
                }
                #endregion
            } while (IsWorking); // стоит всегда true, пока msg не изменит в значение false, в идеальном случае здесь всегда стоит true
            //логика для работы с приёмом и передачей
        }

Answer the question

In order to leave comments, you need to log in

3 answer(s)
A
Alexander Kuznetsov, 2016-11-18
@Aquarius-Michael

And the field through which you shove data, is it volatile? Maybe this will help?

P
Peter, 2016-11-18
@petermzg

And what ConcurrentQueue did not please?
Enqueue(T) - added where data loaded
TryDequeue(T) - received in thread for processing

R
Roman, 2016-11-18
@WNeZRoS

Are you creating cmds in a thread and not passing them anywhere? How then can something appear there?

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question