Webmaster Forum
Go Back   Webmaster Forums UK SEO SEM Webmaster Community Forum - UKWW > General > General Webmaster Talk
Register FAQ Members List Downloads Calendar Today's Posts Webmaster Resources Webmaster Blogs

Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 07-21-2007, 10:54 PM
Junior Member
 
Join Date: Jul 2007
Posts: 1
iTrader: 0 / 0%
Thanks: 0
Thanked 0 Times in 0 Posts
Nominated 0 Times in 0 Posts
TOTW/F/M Award(s): 0
Kleopatra is on a distinguished road
Default Sample C# code for single instance of applications

Ever wanted to only allow a single instance of your application to be run at a time? If so, read on ....

There are many ways to check for instances, but I have found that the easiest (not necessarily the most robust, however) is using a Mutex.

I am using the MS VCSExpress IDE so a lot of background stuff (which is not included in
this snippet) is done automatically.

The following code assumes you also have a class created for Form1.

Code:
using System;using System.Collections.Generic;using System.Windows.Forms;using System.Threading;namespace WindowsApplication1{ static class Program { // this must be declared static since the class is static private static Mutex m; /// <summary> /// The main entry point for the application. /// </summary> [STAThread] static void Main() { Application.EnableVisualStyles(); Application.SetCompatibleTextRenderingDefault(fals e); bool ok; //The name used when creating the mutex can be any string you want m = new Mutex(true, "MyApp", out ok); if (!ok) { MessageBox.Show("Application Already Running"); return; } // This starts the message loop and opens the intial Form Application.Run(new Form1()); // The next line is important so the garbage collector (GC) // doesn't collect the mutex before your done with it GC.KeepAlive(m); } }}
Many of the coders here probably already knew this, but it might come in handy for some of those new to programming.

If anyone has any questions or would like other code snippets, just reply to this thread.
Digg this Post!Add Post to del.icio.usStumble this Post!Wong this Post!
Reply With Quote
Reply

Bookmarks

Webmaster Resources
UK WW SEO Tools
Find UK Hosts
 
The Forum Rules
Forum Rules - MUST READ
 
Site Of the Month
BizzFace
Nominate site of the month
 
Tag Cloud
.biz .us 100 pound 15000+ mobile wallpapers 19000+ mobile logos article database article site bagalibaba@hotmail.com bidding bidding directory bid directory bid for position buy a website cheap domains cmodz dabangla directory estdomains free space glype glype theme google http://www.bagalibaba.com international seo java games links marketing phones electronics promo promotion proxy script proxy theme purchase a website ranking 3 seo seo expert seo services site dump social web space theme three year old web vebtools.com for sale videos web directory web hosting website development wordpress work at home

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On
Forum Jump


Similar Threads
Thread Thread Starter Forum Replies Last Post
Forum HTML/BB Code or whatever redlinemaxed General Webmaster Talk 3 08-31-2007 01:08 PM
Multiple forum or a single forum? temi Running a forum 8 08-12-2007 01:46 PM
Google Summer of Code ovi General Webmaster Talk 1 06-15-2005 10:21 PM
w00t, learned how to code in php crowebird General Webmaster Talk 9 03-30-2005 11:16 AM
Choose button GIVES php code lim038 iG Shop 0 04-25-2004 11:56 AM


All times are GMT. The time now is 01:07 AM.

UK Webmaster World Forums - Internet marketing, web development, domain names, SEO contest and discussuons.
Subscribe to our feeds   Subscribe to our feeds

Powered by vBulletin® Version 3.7.0
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
LinkBacks Enabled by vBSEO 3.1.0

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151