tags:

views:

52

answers:

2

Happy Friday SO! I'm building a multi-WinForm application and am having some troubles.

I have a main WinForm that stays open at all times. It hits a database every minute looking for changes, and if there is a change, it will open the second WinForm (this may seem like a stupid way to do things, but for my purpose, this is the best method).

Just sticking the following code into my Form1.cs doesn't do the trick:

Application.Run(new Form2());

Can you guys point me in the right direction? I have no idea where to turn.

+2  A: 
Form2 form2 = new Form2();
form2.Show();

and to prevent a ton of forms being opened, maybe:

Form2 form2 = new Form2();
form2.ShowDialog();

@Comment:

A BackgroundWorker is used to keep your current UI Thread responsive. It was not designed to keep multiple forms pumping happily along. Look into running your intensive code as a Background thread within a ThreadPool.

Kyle Rozendo
Nice, piece of cake :)
Soo
@Soo - Why is this the best way of doing things? Curiosity question.
Kyle Rozendo
Ok, when I try this in a BackgroundWorker, the newly opened form becomes unresponsive. Is there any way around this?The reason why there are two forms is because the second winform has different properties (it can't be closed and stays on top of other windows).
Soo
@Soo - Check my updated answer.
Kyle Rozendo
Kyle meant that your database-checking code from the first window could be ran by a BackgroundWorker to keep the main UI responsive. My bad, did not notice the timestamps.
Tim Ridgely
@Soo: Are you trying to say that this second form that can't be closed and stays on top of the other is imitating a progress window or something like that?
Will Marcouiller
@Soo: Background worker threads cannot display forms. It has the wrong apartment state and doesn't pump a message loop. Use the worker to collect the form data, create and populate the form in the RunWorkerCompleted event.
Hans Passant
A: 

If what you wish is to launch a long process and to show the progress to the user, for example just like when you have a progress bar or something alike, you should use a BackgroundWorker to do the job. Here's a simple example:

public partial class ProgressForm : Form {
    // Assuming you have put all required controls on design...

    // Allowing some properties to be exposed for progress update...
    public properties MaximumProgress { 
        set {
            progressBar1.Maximum = value;
        }
    public properties OverallProgress { 
        set {
            progressBar1.Value = value;
        }
}

public partial class MainForm : Form {
    private BackgroundWorker backgroundWorker1;
    private ProgressForm _pf;

    public MainForm() {
        InitializeComponent();

        backgroundWorker1 = new BackgroundWorker();
        backgroundWorker1.WorkerReportsProgress = true;
        backgroundWorker1.DoWork += backgroundWorker1_DoWork;
        backgroundWorker1.ProgressChanged += backgroundWorker1_ProgressChanged;
        backgroundWorker1.RunWorkerCompleted += backgroundWorker1_RunWorkerCompleted;
    }

    // Assuming process starts on Button click.
    private void button1_Click(object sender, EventArgs e) {
        _pf = new ProgressForm();
        _pf.MaximumProgress = number-of-elements-to-treat-returned-by-prevision-or-whatever-else;

        // Launching the background workder thread.
        backgroundWorker1.RunWorkerAsync(); // Triggering the DoWork event.

        // Then showing the progress form.
        _pf.ShowDialog();
    }

    private void backgroundWorker1_DoWork(object sender, EventArgs e) {
        LaunchProcess();
    }

    private void backgroundWorker1_ProgressChanged(object sender, ProgressChangedEventArgs e) {
        _pf.OverallProgress = e.ProgressPercentage;
    }

    private void backgroundWorker1_RunWorkerCompleted(object sender, EventArgs e) {
        _pf.Close();
        _pf.Dispose();
    }

    private void LaunchProcess() {
        // Do some work here...
        // Reporting progress somewhere within the processed task
        backgroundWorker1.ReportProgress();
    }
}

This is not a compileable code as its purpose is to illustrate the main idea.

Now, is this something alike you want to do?

Will Marcouiller