Hi
I want when first video gets completed then in same session next video should be shown & so on. Currently if there r 4 videos , that open in different sessions
static Process browserProcess = null;
static async Task Main(string[] args)
{
var datasource = @"MSSQLSERVER_2019";
var database = "Videos";
var username = "sa";
var password = "sa@2019";
string connectionString = @"Data Source=" + datasource + ";Initial Catalog="
+ database + ";Persist Security Info=True;User ID=" + username + ";Password=" + password;
List youtubevideoslinks = GetYouTubeVideoLinksFromDatabase(connectionString);
foreach (var item in youtubevideoslinks)
{
RunVideo(item);
}
}
public static List GetYouTubeVideoLinksFromDatabase(string connectionString)
{
List youtubevideoslinks = new List();
string query = "SELECT videoid FROM videos";
using (SqlConnection connection = new SqlConnection(connectionString))
{
SqlCommand command = new SqlCommand(query, connection);
connection.Open();
SqlDataReader reader = command.ExecuteReader();
while (reader.Read())
{
string videoLink = reader["videoid"].ToString();
youtubevideoslinks.Add(videoLink);
}
reader.Close();
}
return youtubevideoslinks;
}
public static void RunVideo(string videoId)
{
string url = $"watch?v={videoId}";
if (browserProcess == null || browserProcess.HasExited)
{
var process = new Process();
var _process = new ProcessStartInfo(@"C:\Program Files (x86)\Microsoft\Edge\Application\msedge.exe", url);
process.StartInfo = _process;
process.Start();
browserProcess = process;
}
else
{
browserProcess.StartInfo.Arguments = url;
browserProcess.Start();
}
Thread.Sleep(2000);
browserProcess.CloseMainWindow();
browserProcess.Close();
}
Thanks
Ramco RamcoPosted Feb 28, 2023, 7:16 AM
Hi Tuhin
I have below code but videos open in different MsEdge sessions.
Thanks
Tuhin PaulPosted Feb 28, 2023, 2:18 AM
Hi Ramco,
In order to play the next video in the same session after the first video is finished, you can modify the RunVideo method to wait for the video to finish playing before closing the browser window. One way to achieve this is by using a SemaphoreSlim object to synchronize the threads and wait for the video to finish playing. Here's an example:
In this code, a SemaphoreSlim object is used to synchronize the threads and ensure that only one thread is running the RunVideo method at a time. The await semaphore.WaitAsync() method call acquires the semaphore before the method is executed, and the semaphore.Release() method call releases the semaphore when the method is finished.
Inside the RunVideo method, a loop is used to wait for the video to finish playing. The loop checks whether the browser process has exited using the HasExited property of the Process object, and waits for 1 second using Task.Delay(1000) before checking again. When the video has finished playing, the semaphore is released, allowing the next thread to execute the RunVideo method and play the next video in the same session.