2012-11-03 1 views
9

C#WPFアプリケーションSplashScreen.Close(Timespan.FromMilliseconds(int)):Timespan Completeでイベントが送出されていますか?

私はSplashScreenがそのスリープ時間に達した場合

Thread.Sleep(int); //int = milliseconds to display splash screen 

を使用して、時間の最小量のために起動時に表示されている持って、コードが再開し、スプラッシュ画面が近くするフェードアウト

SplashScreen.Close(Timespan.FromMilliseconds(int)); //int = milliseconds fade-out 

を使用して、私は、他のタスクを続行し、スプラッシュ画面が100%透明になり、完全に閉じられているまで待つようにこの時点で一時停止するIEをしたいと思いますコンソールへの書き込みまたはMainWindowの表示。

(TimeSpan.FromMilliseconds(int))が完了すると、イベントが発生しますか? 他の提案はありますか?

namespace StartupSplash 
{ 
    public class SplashScreenStartup 
    { 
     //IMPORTANT:set image property to Resource and NOT Splash Screen 
     private SplashScreen Splash = new SplashScreen("Resources/SplashScreen.png"); 

     public void SplashScreenStartUp() 
     { 
      Splash.Show(false, true); 
      Thread.Sleep(3000); // Pause code, display splash screen 3 seconds 
      Splash.Close(TimeSpan.FromMilliseconds(3000)); // 3 second splash fade-out 
      // I want to wait until splash screen fadeOut has completed before 
      // this next console output is performed. 
      Console.WriteLine("Executes before Splash fadeOut completes."); 
     } 

    } 
+1

これはWPFと仮定していますか? –

+0

はい、そうです。それを指摘していただきありがとうございます。 – reido113

+2

スレッド内でこれを行い、スレッドが終了するまで待ってから、必要な結果を得て、簡単に実装する必要があります。 –

答えて

0

TimeSpanの完了時に聞くイベントはありませんでした。また、スレッドを停止しないように決めた後、代わりにDispatcherTimersを使用することにしました。

(私は間引かと参照目的のために、この一つのクラスにロジックを含んでいた)

using System; 
using System.Windows; 
using System.Windows.Threading; 


namespace StartupSplash2 
{ 

public partial class MainWindow : Window 
{ 
    private DispatcherTimer visibleTimer; 
    private DispatcherTimer fadeoutTimer; 
    private SplashScreen splash; 
    private int visibleTime = (4000); //milliseconds of splash visible time 
    private int fadeoutTime = (1500); //milliseconds of splash fadeout time 

    public MainWindow() 
    { 
     //hide this MainWindow window until splash completes 
     this.Visibility = Visibility.Hidden; 
     InitializeComponent(); 
     splashIn(); //start the splash 
    } 

    private void splashIn() 
    { 
     splash = new SplashScreen("Resources/SplashScreen.png"); //ensure image property is set to Resource and not screen saver 
     visibleTimer = new DispatcherTimer(); //timer controlling how long splash is visible 
     visibleTimer.Interval = TimeSpan.FromMilliseconds(visibleTime); 
     visibleTimer.Tick += showTimer_Tick; //when timer time is reached, call 'showTimer_Tick" to begin fadeout 
     splash.Show(false, true); //display splash 
     visibleTimer.Start(); 
    } 

    private void showTimer_Tick(object sender, EventArgs e) 
    { 
     visibleTimer.Stop(); 
     visibleTimer = null; //clear the unused timer 
     fadeoutTimer = new DispatcherTimer(); 
     fadeoutTimer.Interval = TimeSpan.FromMilliseconds(fadeoutTime); //a timer that runs while splash fades out and controlls when main window is displayed 
     fadeoutTimer.Tick += fadeTimer_Tick; //when fadeout timer is reached, call 'fadeTimer_Tick' to show main window 
     splash.Close(TimeSpan.FromMilliseconds(fadeoutTime)); //begin splash fadeout to close 
     fadeoutTimer.Start(); 
    } 

    private void fadeTimer_Tick(object sender, EventArgs e) 
    { 
     fadeoutTimer.Stop(); 
     fadeoutTimer = null; //clear the unused timer 
     splash = null; //clear the splash var 
     MainWindowReady(); //call method to display main window 
    } 

    public void MainWindowReady() 
    { 
     this.Visibility = Visibility.Visible; 
     //Here is the start of the Main Window Code 
     this.Content = "Ok, the app is ready to roll"; 
    } 

    } 
} 
3

多分このコードはあなたを助けることができます。 BackgroundWorkerのクラスを使用する:

BackgroundWorker worker = new BackgroundWorker(); 
worker.DoWork += (o, ea) => 
{ 
    // Dispatcher.Invoke commands the dispatcher to do something 
    Dispatcher.Invoke((Action)(() => Splash.Close(TimeSpan.FromMilliseconds(3000))); 
    // Sleeps this worker but NOT the UI 
    Thread.Sleep(3000); 
}; 
worker.RunWorkerCompleted += (o, ea) => 
{ 
    // Open your mainwindow sample 
    MainWindow w = new MainWindow(); 
    w.Show(); 
}; 

//Runs the worker on its own thread 
worker.RunWorkerAsync(); 

これは、あなたのスプラッシュスクリーンの閉鎖を開始し、その後それを介して寝て、それが行わだとき、それはあなたのメインウィンドウを開きますする必要があります。私は実際にこれに非常に類似したものを使用して、ログインバーを実装し、WPFアプリケーションの情報を取得します。進行状況バーを表示し、テキストを「サーバーに接続」、「ログイン」、「データを取得しています。

+0

この度はありがとうございます。最初に.DoWorkと.RunWorkerCompletedを終える括弧で囲まれた括弧の後にセミコロンが必要だったので、いくつかのエラーがありました。でも、Dispatcher.Invokeに関するエラーが出ていました。それを解決しようとしているうちに、私は次のコードを見つけました。 – reido113

+0

ああ、もちろん、申し訳ありません!あなたのエラー:ディスパッチャーは最後にブラケットを逃しています。背景労働者について:私は、あなたのコードをその中に入れ、それがどのように進むのか見ることができます。何かを実行してthreadlockedexceptionなどを与えた場合は、dispatcher.invokeアクションを使用します。何か(int fadeOutTimeなど)が取得され、エラーが発生した場合は、代わりに静的メンバーを使用できるかどうかを確認してください。それはとてもシンプルで、クラスは本当に好きです。 Battlefield 3などのような最適なパフォーマンスが必要な場合にのみ、これを行う必要があります。 SSを表示するためのものではありません。 – AmazingDreams

1

次のコードが動作することがわかりました。私はなぜそれほど明確ではないと私はこれをよりよく理解するために近づくにつれて掘り下げます。

必要に応じて批評してください。私はここで学び、分かち合うためにここにいます。乾杯。

class Tester 
    { 
    // Create splash screen instance and reference the image location. 
    // IMPORTANT Ensure that the image properties are set to Resource and NOT Splash Screen 
    private SplashScreen Splash = new SplashScreen("Resources/SplashScreen.png"); 

    public void Display() 
    { 
     Splash.Show(false, true); 
     // pause the code, thus, displaying the splash for 3 seconds 
     Thread.Sleep(3000); 
     // close the splash 
     Close(); 
    } 

    private void Close() 
    { 
     // sets the fadeout time in milliseconds 
     int fadeOutTime = 1500; 

     // wait until the splash screen fadeOut has completed before writing to the console 
     BackgroundWorker worker = new BackgroundWorker(); 
     worker.DoWork += (o, ea) => 
     { 
      // Run background task (fade out and close the splash) 
      Splash.Close(TimeSpan.FromMilliseconds(fadeOutTime)); 
      // Sleep this worker but NOT the UI (for the same time as the fade out time) 
      Thread.Sleep(fadeOutTime); 
     }; 
     worker.RunWorkerCompleted += (o, ea) => 
     { 
      // Execute task after splash has closed completely 
      Console.WriteLine("This is after the splash screen fadeOut completes."); 
     }; 
     // start the background task, on it's own thread 
     worker.RunWorkerAsync(); 
    } 

} 
+2

ええええええええええええええええええええええええええええええええええええええええええええええええええええええええええええええええええええええええええええええんがショートカットをきちんとクリックしていないと仮定して再度クリックすると、スプラッシュ画面が表示されます。どちらも起動に2倍以上かかる)...だから...あなたのスプラッシュ画面が表示されている間にメインスレッドをスリープ状態にしないようにすることを強くお勧めします。メインフォームが読み込まれ、スプラッシュ画面(ヒント:常に上に並んでいる)が表示されたら、何がダメージですか?乾杯。キース。 – corlettk

+0

非常に良い点キース。これらの提案をありがとうございます。私はBackgroundWorkerとマルチスレッドをよりよく理解するようにこれを改善します。歓声メイト。 Reid – reido113

+0

私の基本的なアイデアは、SplashScreenをDoWorkメソッドに移行することです。バックグラウンドスレッドですべて表示、一時停止、およびフェードアウトされるようになります。メインのFormLoadedイベントハンドラをShowSplashScreenInTheBackgroundに...しかし、私はそれをすべてのそのように表示するつもりだ...一숨! – corlettk

0

最終的に私は前のコメントで間違った木を吠えていたと結論づけました。 SplashScreenをバックグラウンドで表示することは問題があります(私が何を試しても自動的に閉じることを拒否しました)。ここで私は結局何をしましたか...本当に簡単です!

using System; 
using System.Net; 
using System.Windows; 

namespace WpfApplication1 
{ 
    /// <summary> 
    /// Interaction logic for Window1.xaml 
    /// </summary> 
    public partial class Window1 : Window 
    { 
    public Window1() { 
     InitializeComponent(); 
    } 

    private void Window_Loaded(object sender, RoutedEventArgs e) { 
     // show the splash screen 
     // nb: Resources/SplashScreenImage.png file Properties ~ Build Action='Resource' 
     var splashScreen = new SplashScreen("Resources/SplashScreenImage.png"); 
     splashScreen.Show(false); // don't close automatically 
     // ... initialise my application ... 
     Initialise(); 
     // close the splash screen. 
     splashScreen.Close(TimeSpan.FromMilliseconds(250D)); 
    } 

    private void Initialise() { 
     // do my long-running application initialisation on the main thread. 
     // In reality you'd do this download asyncronously, but in this case 
     // it serves as a simple proxy for some "heavy" inititalisation work. 
     textBox1.Text = new WebClient().DownloadString("http://stackoverflow.com/questions/13213625/splashscreen-closetimespan-frommilliseconds-listen-for-closed-event"); 
    } 
    } 

} 

私はそれができます...私は全くそれが;-)

乾杯と確信していないんだけれども願っています。キース。

PS:なぜスプラッシュが閉じるのを拒否したのでしょうか?私の推測では、WPFのイベントディスパッチスレッド(それが呼び出されているもの)のものにしか利用できない(つまり加入可能な)イベントに内部的に依存していると思います。

+0

最初は、TimeSpanが完了したときにイベントが発生すると思いました。スレッドを止めないようにするために、代わりにタイマの使用を追求し始めました。私はトラックに戻ってすぐにソリューションコードを手に入れます。何がこれを学んで私を導いているのですか? :) – reido113

0

私は、スプラッシュ画面の有効期限が切れた後にアプリを起動することができますSplashScreen.Dismissedというイベントを見つけました。しかし、最低限必要なOSはWindows 8であり、使用できませんでした。 詳細はこちらをご覧くださいMSDN

関連する問題