Ramco Ramco

Ramco Ramco

  • 442
  • 3.4k
  • 528.6k

Error - Process has exited

Feb 26 2023 3:35 AM

hi

  Error - Process has exited so the requested information is not available on below line 

process.CloseMainWindow();

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<string> youtubevideoslinks = GetYouTubeVideoLinksFromDatabase(connectionString);
    foreach (var item in youtubevideoslinks)
    {
        RunVideo(item);
    }
}
public static List<string> GetYouTubeVideoLinksFromDatabase(string connectionString)
{
    List<string> youtubevideoslinks = new List<string>();

    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


Answers (1)