Hi
In below code Videos open in different 4 sessions. Is it possible that they should run in same 1 session,
On completion of first video next should run & so on
C#
static async Task Main(string[] args)
{
var datasource = @"MSSQLSERVER_2019";
var database = "Videos";
var username = "sa";
var password = "sa";
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"; //Replace table name and column name here
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 = $"youtube.com/watch?v={videoId}";
var process = new Process();
var _process = new ProcessStartInfo(@"C:\Program Files (x86)\Microsoft\Edge\Application\msedge.exe", url);
process.StartInfo = _process;
process.Start();
Thread.Sleep(2000);
process.CloseMainWindow();
process.Close();
}Thanks

Ramco RamcoRank #462 · 5.4K reputationPosted Feb 26, 2023, 1:13 AM EST
Hi Naimish
On this line - browserProcess.CloseMainWindow(); it gives error "Process has exited so the requested information is not available.
Secondly only first video is played then add comes & that video gets displayed , not mine.
Thanks
Naimish MakwanaRank #324 · 7.7K reputationPosted Feb 26, 2023, 12:23 AM EST
Hello Ramco,
Try below code:
static Process browserProcess = null; public static void RunVideo(string videoId) { string url = $"youtube.com/watch?v={videoId}"; if (browserProcess == null || browserProcess.HasExited) { // Start a new browser process if none exists or the existing one has exited 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 { // Use the existing browser process to open the new video browserProcess.StartInfo.Arguments = url; browserProcess.Start(); } Thread.Sleep(2000); browserProcess.CloseMainWindow(); browserProcess.Close(); }Thanks
Naimish Makwana
Naimish MakwanaRank #324 · 7.7K reputationPosted Feb 26, 2023, 1:22 AM EST
Hello Ramco,
You need to use
HasExited:https://www.c-sharpcorner.com/Forums/error-process-has-exited
Thanks
Naimish
Ramco RamcoRank #462 · 5.4K reputationPosted Feb 26, 2023, 1:46 AM EST
Hi Naimish
I have used
public static void RunVideo(string videoId) { string url = $"youtube.com/watch?v={videoId}"; if (browserProcess == null || browserProcess.HasExited) { // Start a new browser process if none exists or the existing one has exited 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 { // Use the existing browser process to open the new video browserProcess.StartInfo.Arguments = url; browserProcess.Start(); } Thread.Sleep(2000); browserProcess.CloseMainWindow(); browserProcess.Close(); }Thanks