Hi
I want to define videos in below string say and then loop all the videos one by one.
var videos = new List
{
};
await Task.Run(async () =>
{
}
Hi
I want to define videos in below string say and then loop all the videos one by one.
var videos = new List
{
};
await Task.Run(async () =>
{
}
Know the answer? Post it. Somebody with the same question will find it here.
Answer a question in the forums and earn +20 SHARP for every answer.
See unanswered questions
DevSocialUpcoming · 3rd editionAI Agents ConferenceDec 1–3, 2026Live onlineFree to attend5days leftRegister
Jayraj ChhayaRank #304 · 8.2K reputationPosted Apr 26, 2024, 6:25 AM EDT
To define videos in a string list and loop through them in C#,
using System; using System.Collections.Generic; using System.Threading.Tasks; class Program { static async Task Main() { var videos = new List { "video1.mp4", "video2.mp4", "video3.mp4" }; await Task.Run(async () => { foreach (var video in videos) { Console.WriteLine($"Processing video: {video}"); // Add your video processing logic here await Task.Delay(1000); // Simulating processing time } }); } }Naimish MakwanaRank #324 · 7.7K reputationPosted Apr 26, 2024, 7:56 AM EDT
Sure, I can help with that. Here’s an example of how you might define a list of video URLs and then loop through them one by one in C#:
var videos = new List { "video1.mp4", "video2.mp4", "video3.mp4" // Add more video file names or URLs here }; await Task.Run(async () => { foreach (var video in videos) { Console.WriteLine("Now playing: " + video); // Replace this with your code to play the video await Task.Delay(5000); // This is just to simulate the time it takes to play the video } });In this example,
videosis a list of strings where each string is a video file name or URL. TheTask.Runmethod is used to start a new task that runs the provided action. Inside this action, we loop through each video in thevideoslist, print out a message saying which video is now playing, and then wait for a certain amount of time to simulate the video playing. You should replace theConsole.WriteLineandTask.Delaylines with your own code to actually play the videos.Thanks
Jignesh KumarRank #91 · 22.9K reputationPosted Apr 27, 2024, 2:14 AM EDT
Hello Ramco,
You can use below one,
var videos = new List { "https://example.com/video1.mp4", "https://example.com/video2.mp4", "https://example.com/video3.mp4" // Add more video URLs as needed }; await Task.Run(() => { // Use Parallel.ForEach to iterate over videos in parallel Parallel.ForEach(videos, async videoUrl => { // Process each video URL asynchronously await ProcessVideoAsync(videoUrl); }); });static async Task ProcessVideoAsync(string videoUrl) { // Simulate some async operation, like downloading or playing the video Console.WriteLine("Playing video: " + videoUrl); await Task.Delay(1000); // Replace with your actual async operation }here, I have used Parallel to iterate over the videos collection in parallel
.ForEachInside the
Parallel.ForEachdelegate, weawaitan asynchronous methodProcessVideoAsyncfor each video URL.The
ProcessVideoAsyncmethod simulates an asynchronous operation for processing each video. You can replaceTask.Delay(1000)with your actual asynchronous operation, such as downloading or playing the video.Jaydeep PatilRank #205 · 11.7K reputationPosted Apr 28, 2024, 3:41 AM EDT
Hi Ramco,
You can refer to the to the below code for looping.
await Task.Run(async () => { foreach (var videoUrl in videos) { // Simulate downloading the video asynchronously await DownloadVideoAsync(videoUrl); } });Ramco RamcoRank #462 · 5.4K reputationPosted Apr 29, 2024, 5:53 AM EDT
Hi Naimish
I have below code. What is happening . It opens 1,2,3, video but only last one is run. I want first video should run then second,third and so on.
foreach (var video in videos) { _driver.Navigate().GoToUrl(video); await Task.Delay(1000); // This is just to simulate the time it takes to play the video } var videos = new List { "youtu.be/f7V5l5-EU", "youtu.be/WI-KwauaM", "youtu.be/_KOUp4WBo0", "youtu.be/5CPMILQrE" };Thanks