Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

passing open arguments from one form instance to anther 1

Status
Not open for further replies.

regulardude

Programmer
Oct 24, 2007
18
US
Hi,

I am using the

SingleInstanceApplication: WindowsFormsApplicationBase

of the visual basic.dll in my c# application to get a single instance application.

I have seen numerous articles



with the help of these, I have only gotten the single instance part working, but not the passing of parameter from one instance of the form to the new instance of the form.

Can someone look at that part of my code and tell me what to do?

Code:
static void StartupNextInstanceHandler(object sender, StartupNextInstanceEventArgs e)    
{   
  
// do whatever you want here with e.CommandLine...    
  
  
int b = 0;    
  
foreach (string i in e.CommandLine)    
    {   
        if (b == 1)    
        {   
            // I know that when b==1 that is the argument I need.   
            // but what to I do now?   
               
             break;    
        }   
        b++;   
    }   
}

Thanks
 
Hello regular dude. I had a look at the first link that you provided and was able to create a simple test program that worked based on instructions and code provided in Figure 4 and 5 in the link.

Below is the program code i tried.
Code:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows.Forms;
using Microsoft.VisualBasic.ApplicationServices;

namespace SingleInstanceTester

{
    static class Program
    {
        /// <summary>
        /// The main entry point for the application.
        /// </summary>
        [STAThread]
        static void Main()
        {
            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);
            SingleInstanceApplication.Run(new Form1(), StartupNextInstanceHandler);
        }

        static void StartupNextInstanceHandler(object sender, StartupNextInstanceEventArgs e)
        {
            foreach (string param in e.CommandLine)
            {
                MessageBox.Show(param);
            }   
        }
    }

    public class SingleInstanceApplication : WindowsFormsApplicationBase
    {
        private SingleInstanceApplication()
        {
            base.IsSingleInstance = true;
        }

        public static void Run(Form f,
            StartupNextInstanceEventHandler startupHandler)
        {
            SingleInstanceApplication app = new SingleInstanceApplication();
            app.MainForm = f;
            app.StartupNextInstance += startupHandler;
            app.Run(Environment.GetCommandLineArgs());
        }
    }
}

Once the program is compiled i started the first instance of the above SingleInstanceTester (which in my test does nothing but displays a form) in debug mode and i put a stop at the begining of StartupNextInstanceHandler routine. I then used command line windows to start the second instance of the SingleInstanceTester using the command below:

Code:
SingleInstanceTester Hope this helps

This command triggered the call to StartupNextInstanceHandler of the first instance and displayed message boxes 4 times as expected by the code it ensured only the first instance remained running. Below are the messages that appeared in my case

message1: "SingleIntanceTester"
message2: "Hope"
message3: "this"
message4: "helps"

So in other words the command line parameters of the second call to SingleInstanceTester were sent to the first instance.

I found this quite interesting. Thank you for the links and hope this reply helps to clarify things a little.



"It is in our collective behaviour that we are most mysterious" Lewis Thomas
 
Hi bledazemi,

Thank you for your insight.

I will try this out asap.

In the meantime, here is a star.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top