Hi guys,

I'm trying to instantiate an object on a new thread, but when I do this I found the form or the splashScreen object not showing.

When I directly call the method which instantiate the object it showing. Why this happening???

here the code for better grasping.

The following code of the parent form which responsible for calling splashScreen object.

public KEditorMain()
        {
            InitializeComponent(); 
   
//not showing if this line used!!!        
            Thread t = new Thread(this.doSplaching);
            t.Start();

//working when this line used
            this.doSplaching();            
        }

The following method used to call splash screen object

public void doSplaching() {
            splashScreen sp = new splashScreen();
            sp.Visible = true;
            sp.Show();    
        }

Because you're not supposed to do anything with the GUI aside from the application's main thread. There is no reason to. Create your forms and let windows pump messages while you use the thread pool to execute your processor intensive methods. Manually creating a thread like that is probably a little more involved than what you were intending.

Here is an example:

Main form:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace daniweb.splash
{
  public partial class frmMain : Form
  {
    private frmProgress prog;

    public frmMain()
    {
      InitializeComponent();
      prog = new frmProgress();
      prog.Show();
    }

    private void frmMain_Load(object sender, EventArgs e)
    {
      for (int i1 = 1; i1 <= 10; i1++)
      {
        System.Threading.Thread.Sleep(500);
        prog.ReportProgress(i1 * 10);
      }

      prog.Dispose();
      prog = null;
    }
  }
}

Splash screen:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace daniweb.splash
{
  public partial class frmProgress : Form
  {
    public frmProgress()
    {
      InitializeComponent();
      this.progressBar1.Minimum = 0;
      this.progressBar1.Value = 0;
      this.progressBar1.Maximum = 100;
      this.TopMost = true;
    }
    public void ReportProgress(int Progress)
    {
      if (this.InvokeRequired)
      {
        this.Invoke(new MethodInvoker(
          delegate()
          {
            ReportProgress(Progress);
          }));
      }
      else
      {
        this.progressBar1.Value = Math.Min(this.progressBar1.Maximum, Progress);
      }
    }
  }
}

Program.cs:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows.Forms;

namespace daniweb.splash
{
  static class Program
  {
    /// <summary>
    /// The main entry point for the application.
    /// </summary>
    [STAThread]
    static void Main()
    {
      Application.EnableVisualStyles();
      Application.SetCompatibleTextRenderingDefault(false);
      Application.Run(new frmMain());
    }
  }
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.