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.
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.
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?
 
         
                                         
                                         
                                         
                                        ![Interactive visualization of a graph in python [closed]](https://www.devze.com/res/2023/04-10/09/92d32fe8c0d22fb96bd6f6e8b7d1f457.gif) 
                                         
                                         
                                         
                                         加载中,请稍侯......
 加载中,请稍侯......
      
精彩评论