Download data and show the screen saver

I need to download some data from the db4o database, which takes 1 or 2 seconds when starting my application, the rest should wait, because the data should be loaded first. to do this in its own thread would mean that the rest must wait for the completion of finishing processing. I would like to make a splash screen or something while loading data, which also needs my own stream, right? how would you do that?

I am using csharp, .net 3.5 and winforms

+3
source share
3 answers

Displaying a splash screen at startup is easy. In the Main () method of your application (in Program.cs), put something like this before the line Application.Run (...):

SplashForm splashy = new SplashForm();
splashy.Show();
Application.Run(new MainForm(splashy));

Change the code and constructor for your main form so that it looks something like this:

private SplashForm _splashy;
public MainForm(SplashForm splashy)
{
    _splashy = splashy;
    InitializeComponent();
}

Then at the end of your MainForm Load event (which supposedly contains the database code) put this code:

_splashy.Close();
_splashy.Dispose();

If you decide to access the database using a separate Thread or BackgroundWorker, you really don't need a splash, as you need some form of progress indicator that appears when BackgroundWorker does its job. This will be done differently than my answer here.

+5
source

, , . , /. - . . , . ShowDialog .

System.ComponentModel.BackgroundWorker, , . , .

, , , . , ( ):

 private void FileThreadStatusDialog_Load(object sender, EventArgs e)
 {
Cursor = Cursors.WaitCursor;

if (m_OpenMode)
{
    this.Text = "Opening...";
    StatusText.Text = m_FileName;
    FileThread = new BackgroundWorker();
    FileThread.RunWorkerCompleted += new RunWorkerCompletedEventHandler(FileThread_RunWorkerCompleted);
    FileThread.DoWork += new DoWorkEventHandler(FileOpenThread_DoWork);
    FileThread.WorkerSupportsCancellation = false;
    FileThread.RunWorkerAsync();
}
else
{
    this.Text = "Saving...";
    StatusText.Text = m_FileName;
    FileThread = new BackgroundWorker();
    FileThread.RunWorkerCompleted += new RunWorkerCompletedEventHandler(FileThread_RunWorkerCompleted);
    FileThread.DoWork += new DoWorkEventHandler(FileSaveThread_DoWork);
    FileThread.WorkerSupportsCancellation = false;
    FileThread.RunWorkerAsync();
}

}

, :

private void FileThread_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
{
    FileThread = null;
        DialogResult = DialogResult.OK;
        Close();
}

:

FileThreadStatusDialog thread = new FileThreadStatusDialog(m_Engine, dlg.FileName, true);
if (thread.ShowDialog(this) == DialogResult.OK)
{
        m_Engine = thread.Engine;
    FillTree();
}
+1

One could make splashy draw in MusiGenesis's answer by adding

Application.DoEvents();

right after

splashy.Show();
+1
source

Source: https://habr.com/ru/post/1718748/


All Articles