本文整理汇总了C#中System.Threading.Semaphore.Close方法的典型用法代码示例。如果您正苦于以下问题:C# Semaphore.Close方法的具体用法?C# Semaphore.Close怎么用?C# Semaphore.Close使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类System.Threading.Semaphore
的用法示例。
在下文中一共展示了Semaphore.Close方法的6个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: TestThrowOnMoreInitialUsersThanMaximumUsers
public void TestThrowOnMoreInitialUsersThanMaximumUsers() {
Assert.Throws<ArgumentOutOfRangeException>(
delegate() {
Semaphore semaphore = new Semaphore(2, 1);
semaphore.Close();
}
);
}
示例2: TryStartProcess
private RemoteService TryStartProcess(CancellationToken cancellationToken)
{
Process newProcess = null;
int newProcessId = -1;
Semaphore semaphore = null;
try
{
string semaphoreName;
while (true)
{
semaphoreName = "HostSemaphore-" + Guid.NewGuid();
bool semaphoreCreated;
semaphore = new Semaphore(0, 1, semaphoreName, out semaphoreCreated);
if (semaphoreCreated)
{
break;
}
semaphore.Close();
cancellationToken.ThrowIfCancellationRequested();
}
var remoteServerPort = "HostChannel-" + Guid.NewGuid();
var processInfo = new ProcessStartInfo(HostPath)
{
Arguments = remoteServerPort + " " + semaphoreName,
WorkingDirectory = _initialWorkingDirectory,
CreateNoWindow = true,
UseShellExecute = false
};
newProcess = new Process { StartInfo = processInfo };
newProcess.Start();
cancellationToken.ThrowIfCancellationRequested();
try
{
newProcessId = newProcess.Id;
_childProcessManager.AddProcess(newProcess);
}
catch
{
newProcessId = 0;
}
// sync:
while (!semaphore.WaitOne(MillisecondsTimeout))
{
if (!newProcess.IsAlive())
{
return null;
}
cancellationToken.ThrowIfCancellationRequested();
}
// instantiate remote service:
Service newService;
try
{
newService = (Service)Activator.GetObject(
typeof(Service),
"ipc://" + remoteServerPort + "/" + nameof(Service));
cancellationToken.ThrowIfCancellationRequested();
newService.Initialize(_references.ToArray(), _imports.ToArray(), _nuGetProvider, _initialWorkingDirectory, OnDumped, OnExecutionCompleted);
}
catch (RemotingException) when (!newProcess.IsAlive())
{
return null;
}
return new RemoteService(newProcess, newProcessId, newService);
}
catch (OperationCanceledException)
{
if (newProcess != null)
{
RemoteService.InitiateTermination(newProcess, newProcessId);
}
return null;
}
finally
{
semaphore?.Close();
}
}
示例3: MainWindow
public MainWindow()
{
Settings.LoadFromXmlFile();
CommonManager.Instance.NWMode = Settings.Instance.NWMode;
CommonManager.Instance.MM.ReloadWorkData();
CommonManager.Instance.ReloadCustContentColorList();
Settings.Instance.ReloadOtherOptions();
CommonUtil.ApplyStyle(Settings.Instance.NoStyle == 0 ? Settings.Instance.StyleXamlPath : null);
SemaphoreSecurity ss = new SemaphoreSecurity();
ss.AddAccessRule(new SemaphoreAccessRule("Everyone", SemaphoreRights.FullControl, AccessControlType.Allow));
semaphore = new Semaphore(int.MaxValue, int.MaxValue, "Global\\EpgTimer_Bon3", out firstInstance, ss);
semaphore.WaitOne(0);
if (!firstInstance && Settings.Instance.ApplyMultiInstance == false)
{
ConnectSrv();
DisconnectServer();
semaphore.Release();
semaphore.Close();
semaphore = null;
CloseCmd();
return;
}
InitializeComponent();
#if DEBUG
appName += "(debug)";
#endif
initExe = true;
try
{
infoWindowViewModel = new InfoWindowViewModel();
// 多重起動時は最小化しない
if (firstInstance && Settings.Instance.WakeMin == true)
{
// Icon化起動すると Windows_Loaded イベントが来ないので
// InitializeComponent 後に ConnectCmd しておく。
Dispatcher.BeginInvoke(new Action(() =>
ConnectCmd(Settings.Instance.NWMode && Settings.Instance.WakeReconnectNW == false)
), DispatcherPriority.Loaded);
if (Settings.Instance.ShowTray && Settings.Instance.MinHide)
{
this.Visibility = Visibility.Hidden;
}
else
{
Dispatcher.BeginInvoke(new Action(() =>
{
this.WindowState = System.Windows.WindowState.Minimized;
minimizedStarting = true;
}));
}
}
//ウインドウ位置の復元
if (Settings.Instance.MainWndTop != -100)
{
this.Top = Settings.Instance.MainWndTop;
}
if (Settings.Instance.MainWndLeft != -100)
{
this.Left = Settings.Instance.MainWndLeft;
}
if (Settings.Instance.MainWndWidth != -100)
{
this.Width = Settings.Instance.MainWndWidth;
}
if (Settings.Instance.MainWndHeight != -100)
{
this.Height = Settings.Instance.MainWndHeight;
}
this.WindowState = Settings.Instance.LastWindowState;
//上のボタン
Action<string, Action> ButtonGen = (key, handler) =>
{
Button btn = new Button();
btn.MinWidth = 75;
btn.Margin = new Thickness(2, 2, 2, 5);
btn.Click += (sender, e) => handler();
btn.Content = key;
buttonList.Add(key, btn);
};
ButtonGen("設定", OpenSettingDialog);
ButtonGen("再接続", OpenConnectDialog);
ButtonGen("再接続(前回)", () => ConnectCmd());
ButtonGen("検索", OpenSearchDialog);
ButtonGen("スタンバイ", () => SuspendCmd(1));
ButtonGen("休止", () => SuspendCmd(2));
ButtonGen("終了", CloseCmd);
ButtonGen("EPG取得", EpgCapCmd);
//.........这里部分代码省略.........
示例4: MainWindow
public MainWindow()
{
Settings.LoadFromXmlFile();
CommonManager.Instance.NWMode = Settings.Instance.NWMode;
CommonManager.Instance.MM.ReloadWorkData();
CommonManager.Instance.ReloadCustContentColorList();
if (Settings.Instance.NoStyle == 0)
{
if (System.IO.File.Exists(System.Reflection.Assembly.GetEntryAssembly().Location + ".rd.xaml"))
{
//ResourceDictionaryを定義したファイルがあるので本体にマージする
try
{
App.Current.Resources.MergedDictionaries.Add(
(ResourceDictionary)System.Windows.Markup.XamlReader.Load(
System.Xml.XmlReader.Create(System.Reflection.Assembly.GetEntryAssembly().Location + ".rd.xaml")));
}
catch (Exception ex)
{
MessageBox.Show(ex.ToString());
}
}
else
{
//既定のテーマ(Aero)をマージする
App.Current.Resources.MergedDictionaries.Add(
Application.LoadComponent(new Uri("/PresentationFramework.Aero, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35;component/themes/aero.normalcolor.xaml", UriKind.Relative)) as ResourceDictionary
);
}
}
SemaphoreSecurity ss = new SemaphoreSecurity();
ss.AddAccessRule(new SemaphoreAccessRule("Everyone", SemaphoreRights.FullControl, AccessControlType.Allow));
semaphore = new Semaphore(int.MaxValue, int.MaxValue, "Global\\EpgTimer_Bon2", out firstInstance, ss);
semaphore.WaitOne(0);
if (!firstInstance)
{
CheckCmdLine();
if (Settings.Instance.ApplyMultiInstance == false)
{
semaphore.Release();
semaphore.Close();
semaphore = null;
CloseCmd();
return;
}
}
InitializeComponent();
initExe = true;
try
{
// 多重起動時は最小化しない
if (firstInstance && Settings.Instance.WakeMin == true)
{
// Icon化起動すると Windows_Loaded イベントが来ないので
// InitializeComponent 後に ConnectCmd しておく。
if (Settings.Instance.NWMode == false || Settings.Instance.WakeReconnectNW == true)
{
ConnectCmd(false);
}
if (Settings.Instance.ShowTray && Settings.Instance.MinHide)
{
this.Visibility = Visibility.Hidden;
}
else
{
Dispatcher.BeginInvoke(new Action(() =>
{
this.WindowState = System.Windows.WindowState.Minimized;
}));
}
}
//ウインドウ位置の復元
if (Settings.Instance.MainWndTop != -100)
{
this.Top = Settings.Instance.MainWndTop;
}
if (Settings.Instance.MainWndLeft != -100)
{
this.Left = Settings.Instance.MainWndLeft;
}
if (Settings.Instance.MainWndWidth != -100)
{
this.Width = Settings.Instance.MainWndWidth;
}
if (Settings.Instance.MainWndHeight != -100)
{
this.Height = Settings.Instance.MainWndHeight;
}
this.WindowState = Settings.Instance.LastWindowState;
//.........这里部分代码省略.........
示例5: continueBtn_Click
private void continueBtn_Click(object sender, EventArgs e)
{
CloseApp ca = (CloseApp)this.stepPanel.Controls[0];
ca.continueBtn.Enabled = false;
ca.richTextBox1.Text = "Closing Program...";
semaphore = new Semaphore(0, 1);
Thread thread = new Thread(ThreadFun);
thread.Start();
semaphore.WaitOne();
semaphore.Close();
ca.continueBtn.Click -= new EventHandler(continueBtn_Click);
UnInstalling ui = new UnInstalling();
this.stepPanel.Controls.RemoveAt(0);
this.stepPanel.Controls.Add(ui);
ca.Dispose();
uninstalling = true;
backgroundWorker1.RunWorkerAsync();
}
示例6: ReproduceByParallelThreads
private void ReproduceByParallelThreads()
{
#region Parallel Reproduct Code
Thread[] th = new Thread[countCPUCore];
// Create a semaphore that can satisfy up to three
// concurrent requests. Use an initial count of zero,
// so that the entire semaphore count is initially
// owned by the main program thread.
//
Semaphore sem = new Semaphore(countCPUCore, countCPUCore);
bool[] isAlive = new bool[countCPUCore];
bool[] isCompleted = new bool[countCPUCore];
int length = (Npop - N_keep) / countCPUCore;
int divideReminder = (Npop - N_keep) % countCPUCore;
for (int proc = 0; proc < th.Length; proc++)
{
ThreadToken tt = new ThreadToken(proc,
length + ((proc == countCPUCore - 1) ? divideReminder : 0),
N_keep + (proc * length));
th[proc] = new Thread(new ParameterizedThreadStart((x) =>
{
// Entered
sem.WaitOne();
isAlive[((ThreadToken)x).No] = true;
// work ...
PReproduction(((ThreadToken)x).startIndex, ((ThreadToken)x).length, ((ThreadToken)x).rand);
// We have finished our job, so release the semaphore
isCompleted[((ThreadToken)x).No] = true;
sem.Release();
}));
setThreadPriority(th[proc]);
th[proc].Start(tt);
}
startloop:
foreach (bool alive in isAlive) // wait parent starter for start all children.
if (!alive)
goto startloop;
endLoop:
sem.WaitOne();
foreach (bool complete in isCompleted) // wait parent to interrupt for finishes all of children jobs.
if (!complete)
goto endLoop;
// Continue Parent Work
sem.Close();
#endregion
}