Click here to Skip to main content
12,501,692 members (64,508 online)
Click here to Skip to main content
Add your own
alternative version


18 bookmarked

Opening multiple document windows with previous instance

, 15 Mar 2004
Rate this:
Please Sign up or sign in to vote.
Communicates with previous instance by passing command line argument file name. Previous instance opens new child window each time file name is passed.


Programs like Microsoft Word used to use MDI for displaying multiple windows, but users would often "lose" windows behind each other. Instead, each document which is opened is now placed in its own window, and displayed in the task bar. However, only one instance handles all the windows. This code sample shows how to setup your own application with this functionality. In particular, when the application is called with a file name as a command line argument, it checks for a previous instance. If found, it uses interprocess communication, to pass the file name to the running instance, then quits. The running application then opens a new window for the new file name.


The interprocess communication uses code from the following article:

Please read for more details on the interprocess communication code.

Using the code

SingleInstance Project complies to SingleInstance.exe, which is the sample application. TestHarness Project compiles to TestHarness.exe, which can be used to call SingleInstance.exe from a windows form with various command line arguments.

There are various ways to make the main form invisible. Setting Visible = false is not enough, as when the form is activated, it will become visible automatically. Another trick is to set the form's Opacity = 0, which prevent the form being "flashed" when it starts up. You can also position the form so it out of the bounds of the screen.

Note that we must use an invisible form so we can receive window messages. I couldn't discover an easy way of starting a message handling loop without including a form.

FrmMain.cs Make Invisible
public FrmMain()

  // Do everything possible to keep this window hidden
  // note: form.Opacity = 0 set in designer will keep
  // the window from flashing during startup
  this.SetBounds( -1000, -1000, 100, 100 );
  this.Visible = false;
If we are the first instance, start FrmMain. If there is a previous instance running, send it the file name which should be opened, and quit this instance.
FrmMain.cs Main method
static void Main(string[] args) 
  // look for previous instances of this application running
  Process[] processes = Process.GetProcessesByName(
  int length = processes.Length;

  // get first parameter as a file name
  string fileName = string.Empty;
  if ( args.Length > 0 )
    fileName = args[0];

  // if no other instance running, start new instance
  if ( length == 1 )
    FrmMain frmMain = new FrmMain();
    Form form = new FrmChild(fileName);
    frmMain.AddForm( form );
  // if another process exists, send file name as message and quit
  if ( length > 1 )
    // Create a new instance of the class:
    CopyData copyData = new CopyData();
    // Create the named channels to send and receive on.  The name
    // is arbitary; it can be anything you want.

    // send fileName
    // quit
During initialization, setup interprocess communication class, and get ready to handle receive message event.
FrmMain.cs Initialization
private void initialize()
  forms = new ArrayList();

  // Create a new instance of the class:
  copyData = new CopyData();
  // Assign the handle:

  // Create the named channels to send and receive on.  The name
  // is arbitary; it can be anything you want.
  // copyData.Channels.Add("HcClientFrmMain");

  // Hook up event notifications whenever a message is received:
  copyData.DataReceived += new DataReceivedEventHandler(copyData_DataReceived);
When we (the previous instance) receive the file name, open a new window using the file name. For demonstration purposes, the file name is just appended to the child window title.
FrmMain.cs Data Received Event Handler
private void copyData_DataReceived(object sender, DataReceivedEventArgs e)
  // Display the data in the logging list box:
  if (e.ChannelName.Equals("HcClientFrmMain"))
    string fileName = (string) e.Data;
    FrmChild form = new FrmChild( fileName );
    AddForm( form );
Note that FrmMain keeps track of the open child forms, and when the last form closes, FrmMain closes.
FrmMain.cs AddForm
public void AddForm( Form form )
  form.Closed += new EventHandler(OnFormClosed);
  forms.Add ( form );

Points of Interest

Note that CopyData.Channels[x].Send( parameter ) can send any object which can be serialized, not just a string. Internally, it sends a


This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


About the Author

Canada Canada
No Biography provided

You may also be interested in...


Comments and Discussions

QuestionLicence Pin
Dommett15-Jul-13 0:11
memberDommett15-Jul-13 0:11 
GeneralInvisible form Pin
Preky14-Nov-04 22:15
memberPreky14-Nov-04 22:15 
GeneralRe: Invisible form Pin
Preky14-Nov-04 22:32
memberPreky14-Nov-04 22:32 
GeneralCollapsed Group Name in XP Task Bar Pin
handa2931-May-04 9:43
memberhanda2931-May-04 9:43 

Excellent article, neilck. Thank you very much! I'd been searching for quite awhile for a simple solution for this business requirement. One would expect M$ to make it easier to do this, especially since all of their documentation says that MDI apps are discouraged...

I have Windows XP on my machine. When the Task Bar gets crowded with running applications, the Task Bar will collapse all of the instances of the visible forms (FrmChild class in the article) into a single group. When it does that for Microsoft Word, you see the application's icon, a number for the number of collapsed windows in the group, and the text "Microsoft Word." However, for my application, XP only displays the app icon and the number of running instances. It doesn't display my product's name.

Does anyone have an idea about how to ensure the product's name displays in the collapsed group itemm on the Task Bar...?

Much obliged!
GeneralRe: Collapsed Group Name in XP Task Bar Pin
neilck31-May-04 11:22
memberneilck31-May-04 11:22 
GeneralRe: Collapsed Group Name in XP Task Bar Pin
handa291-Jun-04 10:30
memberhanda291-Jun-04 10:30 
GeneralRe: Collapsed Group Name in XP Task Bar Pin
handa291-Jun-04 12:06
memberhanda291-Jun-04 12:06 
Generalwizard creation Pin
Anonymous27-Mar-04 20:43
sussAnonymous27-Mar-04 20:43 
GeneralRe: wizard creation Pin
neilck28-Mar-04 8:16
memberneilck28-Mar-04 8:16 
QuestionHave you tried this? Pin
mav.northwind16-Mar-04 2:56
membermav.northwind16-Mar-04 2:56 
AnswerRe: Have you tried this? Pin
neilck16-Mar-04 8:27
memberneilck16-Mar-04 8:27 
GeneralRe: Have you tried this? Pin
mav.northwind16-Mar-04 20:12
membermav.northwind16-Mar-04 20:12 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Terms of Use | Mobile
Web01 | 2.8.160919.1 | Last Updated 16 Mar 2004
Article Copyright 2004 by neilck
Everything else Copyright © CodeProject, 1999-2016
Layout: fixed | fluid