Navigacija
Lista poslednjih: 16, 32, 64, 128 poruka.

C# i multiple instances ?

[es] :: .NET :: .NET Desktop razvoj :: C# i multiple instances ?

[ Pregleda: 3570 | Odgovora: 5 ] > FB > Twit

Postavi temu Odgovori

Autor

Pretraga teme: Traži
Markiranje Štampanje RSS

djmrky
Novi Sad

Član broj: 25160
Poruke: 179
212.62.36.*



Profil

icon C# i multiple instances ?12.09.2005. u 09:47 - pre 226 meseci
kako onemoguciti da se startuje aplikacija vise puta

Pozdrav
She's nice from a far, but far from nice.
 
Odgovor na temu

Java Beograd
Novi Beograd

Član broj: 11890
Poruke: 9500
82.117.206.*



+10254 Profil

icon Re: C# i multiple instances ?12.09.2005. u 09:59 - pre 226 meseci
Code:

using System;
using SpecialServices;

namespace MyNameSpace
{
    public class MyForm : System.Windows.Forms.Form
    {

        private System.ComponentModel.Container components = null;

        public MyForm()
        {
            InitializeComponent();
            ...
        }

        [STAThread]
        static void Main() 
        {
            using(SingleProgramInstance spi = new SingleProgramInstance("MyForm"))
            {
                if (spi.IsSingleInstance)
                {

                    try 
                    {
                        Application.Run(new MyForm());
                    } 
                    catch  (Exception e) 
                    {
                        ...
                    }
                    Application.ExitThread();
                }
                else
                {
                    MessageBox.Show("Bla, bla",    "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
                }
            }
        }
    }
    
    ...
}
    


Poenta je, očigledno u main funkciji.
Sad ću da okačim i dll
OTPOR blokadi ulica, OTPOR blokiranom Beogradu, OTPOR blokiranoj Srbiji
Prikačeni fajlovi
 
Odgovor na temu

djmrky
Novi Sad

Član broj: 25160
Poruke: 179
212.62.36.*



Profil

icon Re: C# i multiple instances ?12.09.2005. u 11:16 - pre 226 meseci
e pa hvala, to je to
btw, a jel ovaj dll freeware posto mi je neophodan takav

Pozdrav
She's nice from a far, but far from nice.
 
Odgovor na temu

Java Beograd
Novi Beograd

Član broj: 11890
Poruke: 9500
82.117.206.*



+10254 Profil

icon Re: C# i multiple instances ?12.09.2005. u 11:32 - pre 226 meseci
Source code je preuzet sa www.codeproject.com a dll sam izbildovao lično i personalno. E sad, da li se sme koristiti, proceni sam.
OTPOR blokadi ulica, OTPOR blokiranom Beogradu, OTPOR blokiranoj Srbiji
 
Odgovor na temu

nervozica
Novi Beograd

Član broj: 18671
Poruke: 77
195.252.119.*



Profil

icon Re: C# i multiple instances ?12.09.2005. u 12:58 - pre 226 meseci
Code:

[DllImport("kernel32.dll", EntryPoint="CreateMutexA")]
private static extern int CreateMutex (
    IntPtr lpMutexAttributes, 
    int bInitialOwner, 
    string lpName);

[DllImport("kernel32.dll")]
private static extern int GetLastError ();

private const int ERROR_ALREADY_EXISTS = 183;


public static bool IsInstanceRunning() {
      bool bIsRunning = false;
      try
      {
            string appname = Assembly.GetExecutingAssembly().GetName().Name;
            if ((CreateMutex(IntPtr.Zero, 1, appname ) != 0) && (GetLastError() == ERROR_ALREADY_EXISTS)) bIsRunning = true;
      }
      catch (Exception ex) {
            MessageBox.Show("Instance Error: " + ex.ToString());
      }
      return bIsRunning;
}


static void Main() {
    if(IsInstanceRunning()) Application.Exit();
    else Application.Run(new Form1());
}



Djankam se, kuzish.
 
Odgovor na temu

ZokiR
Zoran Radojković
Melburn, Australija

Član broj: 15986
Poruke: 109
*.gpm.net.au.



Profil

icon Re: C# i multiple instances ?13.09.2005. u 01:36 - pre 226 meseci
A može i bez interopa:

Code:
using System.Threading;
using System.Diagnostics;

...

    static void Main() 
    {
        bool createdNew;
        Mutex singleInstanceMutex = new Mutex(true, Process.GetCurrentProcess().ProcessName, out createdNew);

        try
        {
            if (createdNew)
            {
                Application.Run(new Form1());
            }
        }
        finally
        {
            singleInstanceMutex.Close();
        }
    }
 
Odgovor na temu

[es] :: .NET :: .NET Desktop razvoj :: C# i multiple instances ?

[ Pregleda: 3570 | Odgovora: 5 ] > FB > Twit

Postavi temu Odgovori

Navigacija
Lista poslednjih: 16, 32, 64, 128 poruka.