2016-12-28 4 views
1

私はこの例(Communicate between two windows forms in C#)はうまく動作していますが、2つのフォームが別のスレッドにある場合は動作しません。C#スプラッシュスクリーンとメインフォーム間の通信が異なるスレッドに

私は(this.splashy.LabelText = I;)のMainFormにライン20で「System.NullReferenceException」を持っている

このスクリプトのポイントはしたときにスプラッシュスクリーンにプログレスバーの幅を変更されましたMainFormが仕事をしています。

ありがとうございます!ここで

2つのC#クラスおよびプログラムファイルです:

//Program.cs 
using System; 
using System.Threading; 
using System.Windows.Forms; 
namespace GIS 
{ 
    static class Program 
    { 
     public static SplashScreen splashy = null; 
     /// <summary> 
     /// Point d'entrée principal de l'application. 
     /// </summary> 
     [STAThread] 
     static void Main() 
     { 
      Application.EnableVisualStyles(); 
      Application.SetCompatibleTextRenderingDefault(false); 
      Thread splashThread = new Thread(new ThreadStart(
      delegate 
      { 
       //SplashScreen splashy = new SplashScreen(); 
       splashy = new SplashScreen(); 
       Application.Run(splashy); 
      } 
      )); 
      splashThread.SetApartmentState(ApartmentState.STA); 
      splashThread.Start(); 
      //run form - time taking operation 
      MainForm mainForm = new MainForm(splashy); 
      mainForm.Load += new EventHandler(mainForm_Load); 
      Application.Run(mainForm); 
     } 
     static void mainForm_Load(object sender, EventArgs e) 
     { 
      //close splash 
      if (splashy == null) 
      { 
       return; 
      } 
      splashy.Invoke(new Action(splashy.Close)); 
      splashy.Dispose(); 
      splashy = null; 
     } 
    } 
} 

//SplashScreen.cs 
using System; 
using System.Windows.Forms; 
namespace GIS 
{ 
    public partial class SplashScreen : Form 
    { 
     public SplashScreen() 
     { 
      InitializeComponent(); 
     } 
     public int LabelText 
     { 
      get 
      { 
       return rectangleShape1.Width; 
      } 
      set 
      { 
       rectangleShape1.Width = value; 
      } 
     } 
    } 
} 

//MainForm.cs 
using System.Threading; 
using System.Windows.Forms; 
namespace GIS 
{ 
    public partial class MainForm : Form 
    { 
     private SplashScreen splashy = null; 
     public MainForm(Form callingForm) 
     { 
      splashy = callingForm as SplashScreen; 
      InitializeComponent(); 
      doWork(); 
     } 
     private void doWork() 
     { 
      for(int i = 0; i < 100; i++) 
      { 
      this.splashy.LabelText = i; 
      Thread.Sleep(10); 
      } 
     } 
    } 
} 

答えて

0

あなたはそれがコンストラクタ

こと

this.splashy.Invoke((MethodInvoker) delegate { this.splashy.LabelText = "Requested" + repeats + "Times"; }); 

ノートのような他のスレッドからの値です変更するSplashScreenを起動する必要があります

splashy = callingForm as SplashScreen; 

は、splashyをnullにすることができます。現在NullReferenceException。この問題は、この中でlocaedさ

が切り取ら:あなたはスタートが速く、あなたがそれを渡す前SplashScreenのインスタンスを作成するためにenoughtされていない

 Thread splashThread = new Thread(new ThreadStart(
     delegate 
     { 
      splashy = new SplashScreen(); 
      Application.Run(splashy); 
     } 
     )); 
     splashThread.SetApartmentState(ApartmentState.STA); 
     splashThread.Start(); 
     //run form - time taking operation 
     MainForm mainForm = new MainForm(splashy); 

新しいスレッドです。結果はnullです。

 splashy = new SplashScreen(); 
     Thread splashThread = new Thread(new ThreadStart(
     delegate 
     { 
      Application.Run(splashy); 
     } 
+0

こんにちは、お返事のためのTHX:

はそれがによってあなたのスレッドを開始する前に派手なを作成する修正します! –

+0

私はまだInvoke()がどのように動作し、ラムダ式をDelegated型に変換するのが不可能であるかを明らかにしていません... –

+0

申し訳ありません - 私のリートミスマッチ - 編集された答えをチェックしてください! – Fruchtzwerg

関連する問題