簡體   English   中英

Task.Run 在執行完成后不會讓線程死亡

[英]Task.Run doesn't let thread die after execution finishes

我正在研究C#代碼,該代碼調用C++代碼以將數據發送到服務器。

我使用Task.Run()作為線程。

一旦 function 返回線程應該死掉,但問題是它不會,只要有數據就會創建一個新線程。

這是我調用Task.Run的代碼

private void ColorFrameReader_FrameArrivedAsync(MediaFrameReader sender, MediaFrameArrivedEventArgs args)
{
    var frame = sender.TryAcquireLatestFrame();
    if (frame != null)
    {
        SoftwareBitmap originalBitmap = null;
        var inputBitmap = frame.VideoMediaFrame?.SoftwareBitmap;
        if (inputBitmap != null)
        {
            // The XAML Image control can only display images in BRGA8 format with premultiplied or no alpha
            // The frame reader as configured in this sample gives BGRA8 with straight alpha, so need to convert it
            originalBitmap = SoftwareBitmap.Convert(inputBitmap, BitmapPixelFormat.Bgra8, BitmapAlphaMode.Premultiplied);                 
            SoftwareBitmap outputBitmap = new SoftwareBitmap(BitmapPixelFormat.Bgra8, originalBitmap.PixelWidth, originalBitmap.PixelHeight, BitmapAlphaMode.Premultiplied);

            //this thread goes to the c++ code and start the TCP communication                
            //var task = Task.Factory.StartNew(() => { ct.ThrowIfCancellationRequested(); _helper.Connect(originalBitmap); }, cts.Token);
            //_helper is the objet of C++ class where it connect and send the frames to the server and Connect is the method where it send.
            Task.Run(() => { _helper.Connect(originalBitmap); });
        }
    }
    
}

我附上了屏幕截圖,您可以在其中看到創建了太多線程。

截屏

我用這個鏈接解決了這個問題,該鏈接創建了一個緩沖區並用框架填充它。 我必須處理 softwareBitmap 對象的主要問題。 這是鏈接解決方案

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM