0

processId /実行中のタスクの名前を検索して、必要なプロセスを強制終了できることを知っています。

今まで私はスケジュールされたタスク/自己実行可能なアプリケーションを開発していませんでしたが、

そのため、実行後にアプリケーションを閉じる方法を知る必要はありませんでした

探していたものを取得した直後に、+ OR を介してすべて(WebDriver を含む) を閉じようとしています。作戦完了 。閉じてください...もうあなたの仕事はありません。しかし、ミスター。Program.cs には、Form1 の何かがまだ必要です。破棄されたオブジェクトにアクセスできません。オブジェクト名: 'Form1'。Application.Exitthis.Close()

両方の組み合わせは、ミッションが完了したにもかかわらず、ある時点で ( program.cs から) 例外エラーを返していました。これ以上コードは要求されませんでした.(?) 私から..少なくとも.

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using OpenQA.Selenium;
using OpenQA.Selenium.IE;
using System.IO;

namespace HT_R_WbBrows2
{
    public partial class Form1 : Form
    {


    public IeEnginGenerator Iengn = new IeEnginGenerator();
    public Form1()
    {
        InitializeComponent();
        //setLogView(View.Details);

        string extractededVal = Iengn.ExtractPageValue(Iengn.itrfWebEng);
        string flnm = @" the directory path to file --> \dolarRate.asp";



        File.WriteAllText(fn, extractededVal);

        this.Close();
        Application.Exit();
    }





public  class IeEnginGenerator
{

    private string directory = Environment.CurrentDirectory;///Path.GetDirectoryName(Assembly.GetExecutingAssembly().CodeBase);


    public   IWebDriver IwebEngine;
    public List<string> ListElementsInnerHtml = new List<string>();
    public HtmlAgilityPack.HtmlDocument Dnetdoc = new HtmlAgilityPack.HtmlDocument();

    #region <<=========== setupDriver ============>>
    public  string  ExtractPageValue(IWebDriver DDriver, string url="") 
    {
        if(string.IsNullOrEmpty(url))
        url = @"http://www.boi.org.il/he/Markets/ExchangeRates/Pages/Default.aspx";
        var service = InternetExplorerDriverService.CreateDefaultService(directory);
        service.LogFile = directory + @"\seleniumlog.txt";
        service.LoggingLevel = InternetExplorerDriverLogLevel.Trace;

        var options = new InternetExplorerOptions();
        options.IntroduceInstabilityByIgnoringProtectedModeSettings = true;

        DDriver = new InternetExplorerDriver(service, options, TimeSpan.FromSeconds(60));
        DDriver.Navigate().GoToUrl(url);

        Dnetdoc.LoadHtml(DDriver.PageSource);
        string Target = Dnetdoc.DocumentNode.SelectNodes("//table//tr")[1].ChildNodes[7].InnerText;
           //.Select(tr => tr.Elements("td").Select(td => td.InnerText).ToList())
           //.ToList();


        return  Math.Round(Convert.ToDouble(Target), 2).ToString();

        //return "";//Math.Round(Convert.ToDouble( TempTxt.Split(' ')[10]),2).ToString();

    }
    #endregion



}






}
}
4

1 に答える 1

1

なぜwinformアプリケーションを使用するのですか?あなたがしていることには、おそらくコンソールアプリケーションで十分でしょう。Main()が終了すると、アプリも閉じます。Main()は、アプリケーションのrunloopが原因で、winformアプリで終了することはありません。

編集:

これを行う正しい方法は次のとおりです。フォームのLoadイベントに登録し、コンストラクターではなく、そこでコードを実行する必要があります。コンストラクター内からWinFormを閉じることはできません。

編集2:このコードをForm1()コンストラクターに配置します。InitializeComponent();の後のどこか

    this.Load += (sender,args)=>{ /*do all your work here*/  
     string extractededVal = Iengn.ExtractPageValue(Iengn.itrfWebEng);
     string flnm = @" the directory path to file --> \dolarRate.asp";
     File.WriteAllText(fn, extractededVal);
     Application.Exit(); 
  };
于 2012-11-18T19:19:51.793 に答える