[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 245
  • Last Modified:

C# problem trying to start another program

I am trying to run the follwoing command

       private void stockCheckToolStripMenuItem_Click(object sender, EventArgs e)
        {

            string str = "";
            Mainfrm Stockcheck = new Mainfrm();
            Stockcheck.ConfigurationVar = ConfigurationVar;
            Stockcheck.LoginScreenVar = LoginScreenVar;
            Stockcheck.AvailableDataBasesVar = AvailableDataBasesVar;
            Stockcheck.InitStockCheck();
            try { Application.Run(Stockcheck); }
            catch (Exception err)
            {
                str = "Application Failed please contact MAC IT";
                ee.MsgBoxE(err, str, "1", 8);
            }

        }

but I get a failure wheren trying to run the command Application.Run(Stockcheck);

I have created a central window for my applicaitons, and the program first starts with a log in screen, this is executed by the program.cs ...

need some help here
0
jhacharya
Asked:
jhacharya
2 Solutions
 
Kyle AbrahamsSenior .Net DeveloperCommented:
What's your err mesage?

0
 
Carlos VillegasFull Stack .NET DeveloperCommented:
Maybe you want is this:
private void stockCheckToolStripMenuItem_Click(object sender, EventArgs e)
        {

            string str = "";
            Mainfrm Stockcheck = new Mainfrm();
            Stockcheck.ConfigurationVar = ConfigurationVar;
            Stockcheck.LoginScreenVar = LoginScreenVar;
            Stockcheck.AvailableDataBasesVar = AvailableDataBasesVar;
            Stockcheck.InitStockCheck();
            try {
               Stockcheck.ShowDialog();
             }
            catch (Exception err)
            {
                str = "Application Failed please contact MAC IT";
                ee.MsgBoxE(err, str, "1", 8); 
            }

Open in new window

0
 
brutaldevCommented:
You're doing that code in a click event of your form. Application.Run won't be able to pass control to another form if on is already running.

Based on this I'm guessing you're getting "A main message loop is already running on the current thread."
You should rather just show the form:
...
// Do not do this while a main form is already running.
// Application.Run(Stockcheck);
Stockcheck.Show();
...

Open in new window

0
 
Carlos VillegasFull Stack .NET DeveloperCommented:
I forget a }
private void stockCheckToolStripMenuItem_Click(object sender, EventArgs e)
{
    string str = "";
    Mainfrm Stockcheck = new Mainfrm();
    Stockcheck.ConfigurationVar = ConfigurationVar;
    Stockcheck.LoginScreenVar = LoginScreenVar;
    Stockcheck.AvailableDataBasesVar = AvailableDataBasesVar;
    Stockcheck.InitStockCheck();
    try
    {
        Stockcheck.ShowDialog();
    }
    catch (Exception err)
    {
        str = "Application Failed please contact MAC IT";
        ee.MsgBoxE(err, str, "1", 8);
    }
}

Open in new window

0
 
jhacharyaAuthor Commented:
Thanks this was perfect,
the final code
        private void stockCheckToolStripMenuItem_Click(object sender, EventArgs e)
        {
            string str = "";
            Mainfrm Stockcheck = new Mainfrm();
            Stockcheck.ConfigurationVar = ConfigurationVar;
            Stockcheck.LoginScreenVar = LoginScreenVar;
            Stockcheck.AvailableDataBasesVar = AvailableDataBasesVar;
            Stockcheck.InitStockCheck();
            try
            {
                //inside window remove to be outside window
                Stockcheck.MdiParent = this;
                Stockcheck.Show();
            }
            catch (Exception err)
            {
                str = "Application Failed please contact MAC IT";
                ee.MsgBoxE(err, str, "1", 8);
            }

        } //stockCheckToolStripMenuItem_Click


all I need to do now is figure out how to close the child window
0

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now