none
[UWP]serial uart

    Question

  • anyone knows my mistake i am useing samplecode https://developer.microsoft.com/en-us/windows/iot/samples/serialuart

    when send data for many times a WinRT task is made but it's not disappear

    so it's makeing loss of memory   how can i solve this matter

    /// <summary>
            /// sendTextButton_Click: Action to take when 'WRITE' button is clicked
            /// - Create a DataWriter object with the OutputStream of the SerialDevice
            /// - Create an async task that performs the write operation
            /// </summary>
            /// <param name="sender"></param>
            /// <param name="e"></param>
            private async void sendTextButton_Click(object sender, RoutedEventArgs e)
            {
                try
                {               
                    if (serialPort != null)
                    {
                        // Create the DataWriter object and attach to OutputStream
                        dataWriteObject = new DataWriter(serialPort.OutputStream);

                        CancellationTokenSource WriteCancellationTokenSource = new CancellationTokenSource();
                        WriteCancellationTokenSource.CancelAfter(TimeSpan.FromMilliseconds(100));
                        //Launch the WriteAsync task to perform the write
                        await WriteAsync(WriteCancellationTokenSource);
                    }
                    else
                    {
                        status.Text = "Select a device and connect";               
                    }
                }
                catch (Exception ex)
                {
                    status.Text = "sendTextButton_Click: " + ex.Message;
                }
                finally
                {
                    // Cleanup once complete
                    if (dataWriteObject != null)
                    {
                        dataWriteObject.DetachStream();
                        dataWriteObject = null;
                    }
                }
            }

            /// <summary>
            /// WriteAsync: Task that asynchronously writes data from the input text box 'sendText' to the OutputStream
            /// </summary>
            /// <returns></returns>
            private async Task WriteAsync(CancellationTokenSource cancellationToken)
            {
                Task<UInt32> storeAsyncTask;

                if (sendText.Text.Length != 0)
                {
                    // Load the text from the sendText input text box to the dataWriter object
                    dataWriteObject.WriteString(sendText.Text);

                   
                    // Launch an async task to complete the write operation
                    storeAsyncTask = dataWriteObject.StoreAsync().AsTask(cancellationToken.Token);

                    UInt32 bytesWritten = await storeAsyncTask;
                   
                    if (bytesWritten > 0)
                    {                   
                        status.Text = sendText.Text + ", ";
                        status.Text += "bytes written successfully!";
                    }

                    dataWriteObject.DetachBuffer();
                    dataWriteObject = null;
                    //sendText.Text = "";
                }
                else
                {
                    status.Text = "Enter the text you want to write and then click on 'WRITE'";
                }
            }


    Friday, March 17, 2017 1:46 AM

All replies

  • Hello kalpacho,

    I’ve tested that official code sample in my side. There’s no problem. You said that “WinRT task is made but it's not disappear”. How did you see the task is still there? Any screenshots?

    Since your code was a little different from the official sample, please share an entire code sample with us. You could upload to OneDrive and post link here.

    Best Regards,

    Xavier Eoro


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    • Marked as answer by kalpacho Monday, March 20, 2017 7:44 AM
    • Unmarked as answer by kalpacho Monday, March 20, 2017 7:45 AM
    Monday, March 20, 2017 5:34 AM
    Moderator
  • Hello Xavier Eoro

    still i have that matter.  maybe something is diffrent i am not sure.

    i shared some pictures and project files for you. by onedrive.

    thank you.

    https://1drv.ms/f/s!AuBsXRgtN25MmSrM4yBVSYZE_hzS

    Monday, March 20, 2017 7:52 AM
  • @kalpacho

    Wow. Your screenshot is Korean. Since here's English form. Please share a English screenshot. Thank you.


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Monday, March 20, 2017 8:16 AM
    Moderator
  • sorry  i exchanged to english.  recheck please  i am waiting good news.

    thank you.

    Monday, March 20, 2017 2:37 PM
  • @kalpacho

    I've checked your code. Your code itself would throw exception. The exception is "the application called an interface that was marshalled for a different thread c#".

    Have you found it? It meant that your code wasn't correct. If the code thrown exception after the task started, it would occur some unexpected behavior.

    I helped you change the code:


    await Task.Run(async () =>
                            {
                                await Dispatcher.RunAsync(Windows.UI.Core.CoreDispatcherPriority.Normal,async()=> {
                                    dataWriteObject.WriteString(sendText.Text + "\r\n");
                                    UInt32 bytesWritten = await dataWriteObject.StoreAsync().AsTask();
                                });
                            }, WriteCancellationTokenSource.Token);

    Then you could see that task will be reduced in the snapshot: 

    Best Regards,

    Xavier Eoro


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.


    Tuesday, March 21, 2017 7:25 AM
    Moderator
  • thank you for your answer.

    i know what your mean. my source code is diffrent, my mistake so i replaced source code again but it just offical code sample from MS

    recheck please,   my point is one time send data with UART was ok but two times or for more times it's makeing many task.

    i don't know why.  first time i just download your offical code from MS, which was same work.  if i send hundred time of datas, task is made hundred..  

    please recheck source code carefully , i arealdy spend a month for this problems.

    Tuesday, March 21, 2017 2:31 PM
  • @ kalpacho

    >>” my point is one time send data with UART was ok but two times or for more times it's makeing many task.”

    I’ve followed your reply to test the official code sample. Yes. Every time, you send data, it will call asynchronous method, then it will create a task to execute this operation. Theses tasks are controlled by ThreadPool. You could see the following snapshot, the “-2” “-40” tasks were automatically reduced, I have not done anything.

    Best Regards,

    Xavier Eoro


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Thursday, March 23, 2017 9:52 AM
    Moderator