<?xml version='1.0' encoding="utf-8"?>
      <rss version='2.0'>
      <channel>
      <title>Форум на Исходниках.RU</title>
      <link>https://forum.sources.ru</link>
      <description>Форум на Исходниках.RU</description>
      <generator>Форум на Исходниках.RU</generator>
  	
      <item>
        <guid isPermaLink='true'>https://forum.sources.ru/index.php?showtopic=278336&amp;view=findpost&amp;p=2636332</guid>
        <pubDate>Sat, 03 Jul 2010 16:18:23 +0000</pubDate>
        <title>Запуск приложения в единственном экземпляре</title>
        <link>https://forum.sources.ru/index.php?showtopic=278336&amp;view=findpost&amp;p=2636332</link>
        <description><![CDATA[Black_Dragon: Рус версия: http://msdn.microsoft.com/ru-ru/library/system.threading.mutex.aspx]]></description>
        <author>Black_Dragon</author>
        <category>.NET FAQ</category>
      </item>
	
      <item>
        <guid isPermaLink='true'>https://forum.sources.ru/index.php?showtopic=278336&amp;view=findpost&amp;p=2624155</guid>
        <pubDate>Fri, 18 Jun 2010 14:05:50 +0000</pubDate>
        <title>Запуск приложения в единственном экземпляре</title>
        <link>https://forum.sources.ru/index.php?showtopic=278336&amp;view=findpost&amp;p=2624155</link>
        <description><![CDATA[PIL: хочу немного добавить, что имя мьютекса надо выбирать правильно:<br>
<div class='tag-quote'><span class='tag-quote-prefix'>Цитата</span> <div class='quote '>Note<br>
<br>
On a server that is running Terminal Services, a named system mutex can have two levels of visibility. If its name begins with the prefix &quot;Global&#092;&quot;, the mutex is visible in all terminal server sessions. If its name begins with the prefix &quot;Local&#092;&quot;, the mutex is visible only in the terminal server session where it was created. In that case, a separate mutex with the same name can exist in each of the other terminal server sessions on the server. If you do not specify a prefix when you create a named mutex, it takes the prefix &quot;Local&#092;&quot;. Within a terminal server session, two mutexes whose names differ only by their prefixes are separate mutexes, and both are visible to all processes in the terminal server session. That is, the prefix names &quot;Global&#092;&quot; and &quot;Local&#092;&quot; describe the scope of the mutex name relative to terminal server sessions, not relative to processes.</div></div><br>
Взято <a class='tag-url' href='http://msdn.microsoft.com/en-us/library/system.threading.mutex.aspx' target='_blank'>здесь</a>]]></description>
        <author>PIL</author>
        <category>.NET FAQ</category>
      </item>
	
      <item>
        <guid isPermaLink='true'>https://forum.sources.ru/index.php?showtopic=278336&amp;view=findpost&amp;p=2596875</guid>
        <pubDate>Mon, 17 May 2010 17:34:27 +0000</pubDate>
        <title>Запуск приложения в единственном экземпляре</title>
        <link>https://forum.sources.ru/index.php?showtopic=278336&amp;view=findpost&amp;p=2596875</link>
        <description><![CDATA[SSV_: <div class='tag-code'><span class='pre_code'></span><div class='code  code_collapsed ' title='Подсветка синтаксиса доступна зарегистрированным участникам Форума.' style=''><div><div><ol type="1"><div class="code_line">using System.Threading;</div><div class="code_line">&nbsp;</div><div class="code_line">&nbsp;static class Program</div><div class="code_line">&nbsp;&nbsp; &nbsp;{</div><div class="code_line">&nbsp;&nbsp; &nbsp; &nbsp; &nbsp;</div><div class="code_line">&nbsp;&nbsp; &nbsp; &nbsp; &nbsp;private static Mutex _syncObject;</div><div class="code_line">&nbsp;&nbsp; &nbsp; &nbsp; &nbsp;private const string _syncObjectName = &quot;{E663FA11-AE0D-480e-9FCA-4BE9B8CDB4E9}&quot;;</div><div class="code_line">&nbsp;&nbsp; &nbsp; &nbsp; &nbsp;/// </div><div class="code_line">&nbsp;&nbsp; &nbsp; &nbsp; &nbsp;/// Главная точка входа для приложения.</div><div class="code_line">&nbsp;&nbsp; &nbsp; &nbsp; &nbsp;/// </div><div class="code_line">&nbsp;&nbsp; &nbsp; &nbsp; &nbsp;[STAThread]</div><div class="code_line">&nbsp;&nbsp; &nbsp; &nbsp; &nbsp;static void Main()</div><div class="code_line">&nbsp;&nbsp; &nbsp; &nbsp; &nbsp;{</div><div class="code_line">&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;bool createdNew;</div><div class="code_line">&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;_syncObject = new Mutex(true, _syncObjectName, out createdNew);</div><div class="code_line">&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;if (!createdNew)</div><div class="code_line">&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;{</div><div class="code_line">&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;MessageBox.Show(&quot;Программа уже запущена.&quot;);</div><div class="code_line">&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;return;</div><div class="code_line">&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;}</div><div class="code_line">&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;// Продолжаем выполнение</div><div class="code_line">&nbsp;</div><div class="code_line">&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;Application.EnableVisualStyles();</div><div class="code_line">&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;Application.SetCompatibleTextRenderingDefault(false);</div><div class="code_line">&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;Application.Run(new Form1());</div><div class="code_line">&nbsp;&nbsp; &nbsp; &nbsp; &nbsp;}</div><div class="code_line">&nbsp;&nbsp; &nbsp;}</div></ol></div></div></div></div><script>preloadCodeButtons('1');</script>]]></description>
        <author>SSV_</author>
        <category>.NET FAQ</category>
      </item>
	
      <item>
        <guid isPermaLink='true'>https://forum.sources.ru/index.php?showtopic=278336&amp;view=findpost&amp;p=2326976</guid>
        <pubDate>Sat, 01 Aug 2009 06:24:46 +0000</pubDate>
        <title>Запуск приложения в единственном экземпляре</title>
        <link>https://forum.sources.ru/index.php?showtopic=278336&amp;view=findpost&amp;p=2326976</link>
        <description><![CDATA[TerraGhost: Очень часто нужно быть уверенным, что программа запущена в единственном экземпляре, при этом копия приложения должна уведомлять главное приложение (первый экземпляр) о том, что была попытка повторного запуска.<br>
Есть два варианта: когда нужно просто послать сигнал о запуске копии и когда помимо сигнала, нужно передать аргументы командной строки копии-приложения.<br>
<br>
Средств для связи процессов огромное. Для простого варианта с сигналом, я бы выбрал семафор (в аттаче, есть класс работы и с ним). Куда интереснее второй вариант, с передачей аргументов приложения:<br>
<div class='tag-code'><span class='pre_code'></span><div class='code  code_collapsed ' title='Подсветка синтаксиса доступна зарегистрированным участникам Форума.' style=''><div><div><ol type="1"><div class="code_line">using System;</div><div class="code_line">&nbsp;</div><div class="code_line">using Terr.Diagnostics;</div><div class="code_line">&nbsp;</div><div class="code_line">internal static class Program</div><div class="code_line">{</div><div class="code_line">&nbsp;&nbsp; &nbsp;private static void Main (string[] args)</div><div class="code_line">&nbsp;&nbsp; &nbsp;{</div><div class="code_line">&nbsp;&nbsp; &nbsp; &nbsp; &nbsp;using (ProcessIpcSignal signal = new ProcessIpcSignal (&quot;MyProgram&quot;))</div><div class="code_line">&nbsp;&nbsp; &nbsp; &nbsp; &nbsp;{</div><div class="code_line">&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;if (signal.IsFirst)</div><div class="code_line">&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;{</div><div class="code_line">&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;Console.WriteLine (&quot;Текущее приложение является главным, текущие аргументы: &quot; + string.Join (&quot; | &quot;, args));</div><div class="code_line">&nbsp;</div><div class="code_line">&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;signal.GetSignal += new ArgsEventHandler (signal_GetArgsSignal);</div><div class="code_line">&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;}</div><div class="code_line">&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;else</div><div class="code_line">&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;{</div><div class="code_line">&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;Console.WriteLine (&quot;Текущее приложение является копией, отсылаем аргументы главному приложению: &quot; + string.Join (&quot; | &quot;, args));</div><div class="code_line">&nbsp;</div><div class="code_line">&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;signal.SendSignal (args);</div><div class="code_line">&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;}</div><div class="code_line">&nbsp;</div><div class="code_line">&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;Console.ReadKey ();</div><div class="code_line">&nbsp;&nbsp; &nbsp; &nbsp; &nbsp;}</div><div class="code_line">&nbsp;&nbsp; &nbsp;}</div><div class="code_line">&nbsp;</div><div class="code_line">&nbsp;&nbsp; &nbsp;private static void signal_GetArgsSignal (object sender, ArgsEventArgs e)</div><div class="code_line">&nbsp;&nbsp; &nbsp;{</div><div class="code_line">&nbsp;&nbsp; &nbsp; &nbsp; &nbsp;Console.WriteLine (&quot;Обнаружен запуск копии приложения с аргументами: &quot; + string.Join (&quot; | &quot;, e.Args));</div><div class="code_line">&nbsp;&nbsp; &nbsp;}</div><div class="code_line">}</div></ol></div></div></div></div><br>
<br>
Класс ProcessIpcSignal берётся из библиотеки в аттаче.]]></description>
        <author>TerraGhost</author>
        <category>.NET FAQ</category>
      </item>
	
      </channel>
      </rss>
	