code_junkie

How to make it so if one copy of a program is running another won't be able to open

Nov 13th, 2011
156
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.04 KB | None | 0 0
  1. using System;
  2. using Microsoft.VisualBasic.ApplicationServices;
  3.  
  4. namespace Owf
  5. {
  6. public class SingleInstanceController
  7. : WindowsFormsApplicationBase
  8. {
  9. public SingleInstanceController()
  10. {
  11. // Set whether the application is single instance
  12. this.IsSingleInstance = true;
  13.  
  14. this.StartupNextInstance += new
  15. StartupNextInstanceEventHandler(this_StartupNextInstance);
  16. }
  17.  
  18. void this_StartupNextInstance(object sender,
  19. StartupNextInstanceEventArgs e)
  20. {
  21. // Here you get the control when any other instance is
  22. // invoked apart from the first one.
  23. // You have args here in e.CommandLine.
  24.  
  25. // You custom code which should be run on other instances
  26. }
  27.  
  28. protected override void OnCreateMainForm()
  29. {
  30. // Instantiate your main application form
  31. this.MainForm = new Form1();
  32. }
  33. }
  34. }
  35.  
  36. [STAThread]
  37. static void Main()
  38. {
  39. string[] args = Environment.GetCommand
  40. SingleInstanceController controller = new SingleInstanceController();
  41. controller.Run(args);
  42. }
Add Comment
Please, Sign In to add comment