0

I am trying to receive multiple RTSP streams using Libvlcsharp.

I need to display multiple streams at once, so i declared a VideoView List, private List<VideoView> videoList = new List<VideoView>();

and declared function that creates a VideoView and adds it to the list and flowlayoutpanel.

private async Task AddPlayerAsync()
        {
            await Task.Run(async () =>
            {
                var player = new MediaPlayer(libVLC);
                var Media = new Media(libVLC, new Uri(textBox1.Text));
                var video = new VideoView();
                
                video.Size = new Size(200, 200);

                video.MediaPlayer = player;
                ((System.ComponentModel.ISupportInitialize)video).EndInit();

                var uiUpdatedTask = new TaskCompletionSource<bool>(TaskCreationOptions.RunContinuationsAsynchronously);
                panel1.BeginInvoke((MethodInvoker)delegate
                {
                    panel1.Controls.Add(video);
                    uiUpdatedTask.SetResult(true);
                });

                await uiUpdatedTask.Task.ConfigureAwait(false);

                lock (videoList)
                {
                    // Add to a list
                    videoList.Add(video);
                }

                player.Play(Media);
            });
        }

And i saw the crossthread exeption in: panel1.Controls.Add(video);

Is there an error in the videoview creation step?

I tried using begininit() and endinit(), but the result was the same. I also tried set video.MediaPlayer = player; after panel1.Controls.Add(video);.

  • Remove all pseudo-async stuff you have there. Keep in mind that you cannot create a Control in a Thread other than the UI Thread. If you're trying to show multiple videos at the same time and this causes a problem, it's because the library you're using is not meant for concurrent use or the Control itself is not designed for that -- You forgot to explain why you came up with this kind of code – Jimi Jul 31 '23 at 07:56

0 Answers0