2016-11-02 6 views
-2

C#.net 3.5 Winformsアプリケーション。C#Winforms - 処理されたオブジェクトにアクセスできない

ここに状況があります。私は別のスレッドで別のフォームを開くいくつかのコードを実行するフォームがあります。そのフォームが開くと、特定のタスクが実行され、それらの条件が満たされた場合に閉じる必要があります。問題は、それはこの例外を投げつけ続けて、なぜ私は考えていない。ここ

Exception Text]([![http://i.imgur.com/WFymZdR.jpg] 1

は全てこの中で使用されるコードの私の抜粋である:

ここで別のスレッドでフォームを開く機能である:のProgram.cs

public static void new_downloader_thread() 
    { 
     Thread t = new Thread(new ThreadStart(open_downloader)); 
     t.SetApartmentState(ApartmentState.STA); 

     if (Open_Downloader) 
     { 
      t.Start(); 
     } 
    } 
    public static void open_downloader() 
    { 
     try 
     { 
      Application.Run(new DownloadInstall()); 
     } 
     catch (Exception e) 
     { 
      MessageBox.Show(Convert.ToString(e)); 
     } 
    } 

はここに私の問題を与えるフォームを開くフォームからのコードです:Manager.cs

private void new_System_Click(object sender, EventArgs e) 
    { 
     if (Program.Open_Downloader == false) 
     { 
      Program.Current_Download = "newsys.exe"; 
      Program.Open_Downloader = true; 
      Program.new_downloader_thread(); 
     } 
     else 
     { 
      download_busy(); 
     } 
    } 
ここで

開いている形です。DownloadInstall.cs

public partial class DownloadInstall : Form 
{ 
    /// Opening Declarations 
    private static System.Net.WebClient web_client; 
    private static System.Diagnostics.Stopwatch stop_watch; 

    private static bool downloading { get; set; } 
    private static bool is_name_update { get; set; } 

    private static int download_state { get; set; } 
    private static int elapsed { get; set; } 
    private static int opening { get; set; } 
    //private static int counter; 
    /// -------------------- 

    public DownloadInstall() 
    { 
     InitializeComponent(); 

     is_name_update = false; 
     downloading = false; 
     opening = 0; 

     web_client = new System.Net.WebClient(); 
     web_client.DownloadProgressChanged += new DownloadProgressChangedEventHandler(Download_Progress); 
     web_client.DownloadFileCompleted += new AsyncCompletedEventHandler(Download_Complete); 
     stop_watch = new System.Diagnostics.Stopwatch(); 

     /// Keypress event call and blanking out the box text. 
     this.name_update_field.Text = ""; 
     this.name_update_field.KeyPress += new KeyPressEventHandler(no_period); 
     /// -------------------------------------------------- 

     /// Changes the text to what file we are downloading. 
     this.file_name.Text = string.Format("Downloading {0}", Program.Current_Download); 
     /// ------------------------------------------------- 

     /// Sets a forms closed event handler 
     this.FormClosing += new FormClosingEventHandler(Download_Window_Closing); 
     /// --------------------------------- 

     /// Creates folder where files are to be downloaded and deletes any residual files. 
     try 
     { 
      System.IO.Directory.CreateDirectory(@"C:\Downloads"); 
     } 
     catch 
     { 

     } 
     if (System.IO.File.Exists(string.Format(@"C:\Downloads\{0}", Program.Current_Download))) 
     { 
      try 
      { 
       System.IO.File.Delete(string.Format(@"C:\Downloads\{0}", Program.Current_Download)); 
      } 
      catch 
      { 
       if (Program.Disconnecting == false) 
       { 
        opening = 1; 
       } 
      } 
     } 
     /// ------------------------------------------------------------------------------- 

     switch (opening) 
     { 
      /// Case 0 Starts the download or name update code normally. 
      case 0: 
       if (Program.Current_Download == "Name Update") 
       { 
        file_name.Text = "Name Update Downloader"; 
        is_name_update = true; 
        this.restart_or_start.Text = "Start"; 
        this.cid.Visible = true; 
        this.name_update_field.ReadOnly = false; 
        this.name_update_field.Visible = true; 
       } 
       else 
       { 
        Download_Begin(); 
       } 
       break; 

      /// Case 1 will close the downloader. 
      case 1: 
       MessageBox.Show("It is possible this file was already downloaded and is already open on this computer.", "Hmmmmm...", MessageBoxButtons.OK, MessageBoxIcon.Exclamation); 
       this.Close(); 
       break; 
     } 

    } 


    /// Function that is called when the window is closed. 
    private void Download_Window_Closing(object sender, FormClosingEventArgs e) 
    { 
     if (downloading) 
     { 
      web_client.CancelAsync(); 
     } 
     Program.Current_Download = ""; 
     Program.Open_Downloader = false; 
    } 
    /// -------------------------------------------------- 

ないのはなぜこれが例外をスローして確認してください。フォームを閉じるコードを削除しても機能し、コードの他の部分からフォームを適切に閉じることができます。どんな助けも素晴らしいだろう!

+0

? –

+0

@DangerZone私はそれを含めるために自分の投稿を編集しました。 –

+0

@DangerZone私はそれを解決しました。コンストラクタが完全に実行される前にフォームをクローズしようとしているので、オブジェクトが存在しないため、参照。 –

答えて

0

解決済み!問題は、フォームの作成が完了する前にフォームを閉じるときです。

は変更:

this.Close(); 

に: `Download_Window_Closing`ためのコードがある

Load += (s, e) => Close(); 
関連する問題