In parallel call, limit executions per second - .net-4.0

Using TPL / Parallel.ForEach is there an out-of-the-box way to limit the number of times a method is called per unit of time (i.e. no more than 50 calls per second). This is different than limiting the number of threads. Perhaps there's some simple hack to make this work?

One solution is to make a thread-safe version of the following https://stackoverflow.com/a/7728872/356790
/// <summary>
/// This class limits the number of requests (method calls, events fired, etc.) that can occur in a given unit of time.
/// </summary>
class RequestLimiter
{
#region Constructors
/// <summary>
/// Initializes an instance of the RequestLimiter class.
/// </summary>
/// <param name="maxRequests">The maximum number of requests that can be made in a given unit of time.</param>
/// <param name="timeSpan">The unit of time that the maximum number of requests is limited to.</param>
/// <exception cref="ArgumentException">maxRequests <= 0</exception>
/// <exception cref="ArgumentException">timeSpan.TotalMilliseconds <= 0</exception>
public RequestLimiter( int maxRequests , TimeSpan timeSpan )
{
// check parameters
if ( maxRequests <= 0 )
{
throw new ArgumentException( "maxRequests <= 0" , "maxRequests" );
}
if ( timeSpan.TotalMilliseconds <= 0 )
{
throw new ArgumentException( "timeSpan.TotalMilliseconds <= 0" , "timeSpan" );
}
// initialize instance vars
_maxRequests = maxRequests;
_timeSpan = timeSpan;
_requestTimes = new Queue<DateTime>( maxRequests );
// sleep for 1/10th timeSpan
_sleepTimeInMs = Convert.ToInt32( Math.Ceiling( timeSpan.TotalMilliseconds / 10 ) );
}
#endregion
/// <summary>
/// Waits until an request can be made
/// </summary>
public void WaitUntilRequestCanBeMade()
{
while ( !TryEnqueueRequest() )
{
Thread.Sleep( _sleepTimeInMs );
}
}
#region Private Members
private readonly Queue<DateTime> _requestTimes;
private readonly object _requestTimesLock = new object();
private readonly int _maxRequests;
private readonly TimeSpan _timeSpan;
private readonly int _sleepTimeInMs;
/// <summary>
/// Remove requests that are older than _timeSpan
/// </summary>
private void SynchronizeQueue()
{
while ( ( _requestTimes.Count > 0 ) && ( _requestTimes.Peek().Add( _timeSpan ) < DateTime.Now ) )
{
_requestTimes.Dequeue();
}
}
/// <summary>
/// Attempts to enqueue a request.
/// </summary>
/// <returns>
/// Returns true if the request was successfully enqueued. False if not.
/// </returns>
private bool TryEnqueueRequest()
{
lock ( _requestTimesLock )
{
SynchronizeQueue();
if ( _requestTimes.Count < _maxRequests )
{
_requestTimes.Enqueue( DateTime.Now );
return true;
}
return false;
}
}
#endregion
}

The ready code samples using Timer:
for .NET 4.0 TPL Don't miss this one, this is quite a project, of all times and all peoples!
in C# 5.0/.NET 4.5, using async/wait, WPF
The code samples/examples using Reactive Extensions (Rx):
Observable.Interval Method (TimeSpan, IScheduler), MSDN example
Observable.Timer() sample, RxWiki

This solution enforces a delay between the start of each thread and could be used to fulfill your requirements.
private SemaphoreSlim CooldownLock = new SemaphoreSlim(1, 1);
private DateTime lastAction;
private void WaitForCooldown(TimeSpan delay)
{
CooldownLock.Wait();
var waitTime = delay - (DateTime.Now - lastAction);
if (waitTime > TimeSpan.Zero)
{
Task.Delay(waitTime).Wait();
lastAction = DateTime.Now;
}
lastAction = DateTime.Now;
CooldownLock.Release();
}
public void Execute(Action[] actions, int concurrentThreadLimit, TimeSpan threadDelay)
{
if (actions.Any())
{
Parallel.ForEach(actions,
new ParallelOptions() { MaxDegreeOfParallelism = concurrentThreadLimit},
(currentAction) =>
{
WaitForCooldown(threadDelay);
currentAction();
});
}
}

Related

Outlook VSTO AddIn / Custom Ribbon: How to add custom Ribbon to home group (Main Window) in Outlook 2016

I tried to add the following custom ribbon (Designer Approach) to the TabHomeGroup in Outlook (Explorer). Unfortunateyl it won't show up. Why?
I used these OfficeIds: https://www.microsoft.com/en-us/download/details.aspx?id=50745
namespace OutlookAddIn4
{
partial class Ribbon_Explorer : Microsoft.Office.Tools.Ribbon.RibbonBase
{
/// <summary>
/// Erforderliche Designervariable.
/// </summary>
private System.ComponentModel.IContainer components = null;
public Ribbon_Explorer()
: base(Globals.Factory.GetRibbonFactory())
{
InitializeComponent();
}
/// <summary>
/// Verwendete Ressourcen bereinigen.
/// </summary>
/// <param name="disposing">"true", wenn verwaltete Ressourcen gelöscht werden sollen, andernfalls "false".</param>
protected override void Dispose(bool disposing)
{
if (disposing && (components != null))
{
components.Dispose();
}
base.Dispose(disposing);
}
#region Vom Komponenten-Designer generierter Code
/// <summary>
/// Erforderliche Methode für die Designerunterstützung.
/// Der Inhalt der Methode darf nicht mit dem Code-Editor geändert werden.
/// </summary>
private void InitializeComponent()
{
this.tab1 = this.Factory.CreateRibbonTab();
this.group1 = this.Factory.CreateRibbonGroup();
this.btnSettings = this.Factory.CreateRibbonButton();
this.tab1.SuspendLayout();
this.group1.SuspendLayout();
this.SuspendLayout();
//
// tab1
//
this.tab1.ControlId.ControlIdType = Microsoft.Office.Tools.Ribbon.RibbonControlIdType.Office;
this.tab1.ControlId.OfficeId = "TabHomeGroup";
this.tab1.Groups.Add(this.group1);
this.tab1.Label = "TabHomeGroup";
this.tab1.Name = "tab1";
//
// group1
//
this.group1.Items.Add(this.btnSettings);
this.group1.Label = "group1";
this.group1.Name = "group1";
//
// btnSettings
//
this.btnSettings.ControlSize = Microsoft.Office.Core.RibbonControlSize.RibbonControlSizeLarge;
this.btnSettings.Image = global::OutlookAddIn4.Properties.Resources.do_48x48;
this.btnSettings.Label = "Settings";
this.btnSettings.Name = "btnSettings";
this.btnSettings.ShowImage = true;
this.btnSettings.Click += new Microsoft.Office.Tools.Ribbon.RibbonControlEventHandler(this.btnSettings_Click);
//
// Ribbon_Explorer
//
this.Name = "Ribbon_Explorer";
this.RibbonType = "Microsoft.Outlook.Explorer";
this.Tabs.Add(this.tab1);
this.Load += new Microsoft.Office.Tools.Ribbon.RibbonUIEventHandler(this.Ribbon_Explorer_Load);
this.tab1.ResumeLayout(false);
this.tab1.PerformLayout();
this.group1.ResumeLayout(false);
this.group1.PerformLayout();
this.ResumeLayout(false);
}
#endregion
internal Microsoft.Office.Tools.Ribbon.RibbonTab tab1;
internal Microsoft.Office.Tools.Ribbon.RibbonGroup group1;
internal Microsoft.Office.Tools.Ribbon.RibbonButton btnSettings;
internal SettingsForm settingsForm;
}
partial class ThisRibbonCollection
{
internal Ribbon_Explorer Ribbon_Explorer
{
get { return this.GetRibbon<Ribbon_Explorer>(); }
}
}
}
Are you sure the TabHomeGroup is visible at the moment your add-in is started?
Anyway, you need to use the TabHome idMso instead if you want to place your custom UI on the Home tab in the Explorer window.

StackExchange.Redis - best way to wait for a lock

I have the following method that waits for an exclusive lock on a Redis key. This method works but I'm wondering if there is a better way without a for loop and a Thread.Sleep.
/// <summary>
/// wait up to 2 seconds to achieve a lock!
/// The lock is good for a maximum of 3 seconds
/// </summary>
/// <param name="codeID"></param>
internal void WaitForSingleUseLock(CodeID codeID)
{
var key = _redemptionRepo.SingleUseCodeLockPrefix + codeID.Value;
var expiration = TimeSpan.FromSeconds(3);
for (var i = 0; i < 20; i++)
{
var lockAchieved = _cacheRepo.LockTake(key, "1", expiration);
if (lockAchieved)
{
break;
}
Thread.Sleep(TimeSpan.FromMilliseconds(100));
}
}
The only thing I could suggest different would be to consider pub/sub as a side channel (meaning: in addition, not replacement) for indicating when the lock might now be available - i.e. publish when releasing, and use the sub to release a timer (via a monitor or an async-wait-handle).
Other than that: nope. Redis doesn't have the idea of a pending queue. You could perhaps possibly construct one using lists, but...
After taking #Marc's comments into consideration and meeting with my team about the benefits of Task.Delay() over Thread.Sleep() in this context I decided on this as a final solution:
/// <summary>
/// wait up to 3 seconds to achieve a lock!
/// The lock is good for a maximum of 3 seconds
///
/// Returns the total amount of time until the lock was taken
/// </summary>
internal virtual async Task<TimeSpan> WaitForSingleUseLock(CodeID codeID)
{
var key = _redemptionRepo.SingleUseCodeLockPrefix + codeID.Value;
var totalTime = TimeSpan.Zero;
var maxTime = TimeSpan.FromSeconds(3);
var expiration = TimeSpan.FromSeconds(3);
var sleepTime = TimeSpan.FromMilliseconds(50);
var lockAchieved = false;
while (!lockAchieved && totalTime < maxTime)
{
lockAchieved = _cacheRepo.LockTake(key, "1", expiration);
if (lockAchieved)
{
continue;
}
await Task.Delay(sleepTime);
totalTime += sleepTime;
}
return totalTime;
}

How to initialize RavenDb database with bundle programatically?

I have this code it works well EXCEPT I want to ensure that the expiration bundle is in the database and I dont want to create this database manually. What is the correct way to initialize a database with the expiration bundle enabled?
_documentStore = new DocumentStore()
{
Url = SettingsManager.RavenDbUrl,
DefaultDatabase = SettingsManager.RavenDbDatabaseName
};
_documentStore.Initialize();
Well I couldnt find docs and I couldnt find help so I cobbled this together:
static class RavenDbExtensions
{
/// <summary>
/// Ensure a bundle is activated
/// </summary>
/// <param name="documentStore"></param>
/// <param name="bundleName"></param>
/// <param name="databaseName"></param>
public static void ActivateBundle(this IDocumentStore documentStore, string bundleName, string databaseName)
{
using (var session = documentStore.OpenSession())
{
var databaseDocument = session.Load<DatabaseDocument>("Raven/Databases/" + databaseName);
var settings = databaseDocument.Settings;
var activeBundles = settings.ContainsKey(Constants.ActiveBundles) ? settings[Constants.ActiveBundles] : null;
if (string.IsNullOrEmpty(activeBundles))
settings[Constants.ActiveBundles] = bundleName;
else if (!activeBundles.Split(new char[]{';'}).Contains(bundleName, StringComparer.OrdinalIgnoreCase))
settings[Constants.ActiveBundles] = activeBundles + ";" + bundleName;
session.SaveChanges();
}
}
}
Then I do this little dance when initializing the document store. It seems to work well. It isnt clear whether the bundle is named Expiration or DocumentExpiration so I try both it doesnt crash and it seems to fold in the expiration functionality I need.
_documentStore = new DocumentStore()
{
Url = SettingsManager.RavenDbUrl
};
_documentStore.Initialize();
_documentStore.ActivateBundle("Expiration", Assembly.GetExecutingAssembly().GetName().Name);
_documentStore.ActivateBundle("DocumentExpiration", Assembly.GetExecutingAssembly().GetName().Name);
_documentStore.DefaultDatabase = Assembly.GetExecutingAssembly().GetName().Name;

How to get access to the VSTO Application object of multiple instances of Visio 2010 using C#?

Does anyone know how to get access to the VSTO Application object of multiple instances of Visio 2010 using C#? Marshal.GetActiveObject() only returns the active instance.
There are forum posts on how to get multiple Application objects for all instances of Excel - where the author is using the technique to iterate through each Excel instance by process, get the native object model from a child window of the Excel instance, and finally to get the Application object.
This technique works well for for Excel, but I am unable to get a valid native OM (IDispatch or IAccessible) from the call to AccessibleObjectFromWindow() for a Visio window handle that would allow me to reference the parent Application object.
Here's my code snippet:
using VISIO = Microsoft.Office.Interop.Visio;
Process[] processes = Process.GetProcessesByName(Visio.APP_PROCESS_NAME);
foreach (Process process in processes)
{
int hwnd = (int)process.MainWindowHandle;
// We need to enumerate the child windows to find one that
// supports accessibility. To do this, instantiate the
// delegate and wrap the callback method in it, then call
// EnumChildWindows, passing the delegate as the 2nd arg.
if (hwnd != 0)
{
int hwndChild = 0;
cb = new EnumChildCallback(EnumChildProc);
EnumChildWindows(hwnd, cb, ref hwndChild);
// If we found an accessible child window, call
// AccessibleObjectFromWindow, passing the constant
// OBJID_NATIVEOM (defined in winuser.h) and
// IID_IDispatch - we want an IDispatch pointer
// into the native object model.
if (hwndChild != 0)
{
const uint OBJID_NATIVEOM = 0xFFFFFFF0;
Guid IID_IDispatch = new Guid("{00020400-0000-0000-C000-000000000046}");
EXL.Window ptr = null;
int hr = AccessibleObjectFromWindow(
hwndChild, OBJID_NATIVEOM,
IID_IDispatch.ToByteArray(), ref ptr);
if (hr >= 0)
{
// If we successfully got a native OM
// IDispatch pointer, we can QI this for
// an Viso Application (using the implicit
// cast operator supplied in the PIA).
VISIO.Application app = (VISIO.Application)ptr.Application;
allInstances.Add(app);
}
}
}
}
I found a solution that iterates through the Runtime Object Table (ROT) to get all monikers corresponding to Visio documents (".vdx") and get their corresponding VSO.Document object (using VSO=Microsoft.Office.Interop.Visio); with the VSO.Document object, I can get the VSO.Application object from the Document.Application property.
From this article by Andrew Baker: http://www.vbusers.com/codecsharp/codeget.asp?ThreadID=69&PostID=1, I created a ROTUtil utility/helper class:
/// <summary>
/// The COM running object table utility class.
/// </summary>
public class ROTUtil
{
#region APIs
[DllImport("ole32.dll")]
private static extern int GetRunningObjectTable(int reserved,
out UCOMIRunningObjectTable prot);
[DllImport("ole32.dll")]
private static extern int CreateBindCtx(int reserved,
out UCOMIBindCtx ppbc);
[DllImport("ole32.dll", PreserveSig = false)]
private static extern void CLSIDFromProgIDEx([MarshalAs(UnmanagedType.LPWStr)] string progId, out Guid clsid);
[DllImport("ole32.dll", PreserveSig = false)]
private static extern void CLSIDFromProgID([MarshalAs(UnmanagedType.LPWStr)] string progId, out Guid clsid);
[DllImport("ole32.dll")]
private static extern int ProgIDFromCLSID([In()]ref Guid clsid, [MarshalAs(UnmanagedType.LPWStr)]out string lplpszProgID);
#endregion
#region Public Methods
/// <summary>
/// Converts a COM class ID into a prog id.
/// </summary>
/// <param name="progID">The prog id to convert to a class id.</param>
/// <returns>Returns the matching class id or the prog id if it wasn't found.</returns>
public static string ConvertProgIdToClassId(string progID)
{
Guid testGuid;
try
{
CLSIDFromProgIDEx(progID, out testGuid);
}
catch
{
try
{
CLSIDFromProgID(progID, out testGuid);
}
catch
{
return progID;
}
}
return testGuid.ToString().ToUpper();
}
/// <summary>
/// Converts a COM class ID into a prog id.
/// </summary>
/// <param name="classID">The class id to convert to a prog id.</param>
/// <returns>Returns the matching class id or null if it wasn't found.</returns>
public static string ConvertClassIdToProgId(string classID)
{
Guid testGuid = new Guid(classID.Replace("!", ""));
string progId = null;
try
{
ProgIDFromCLSID(ref testGuid, out progId);
}
catch (Exception)
{
return null;
}
return progId;
}
/// <summary>
/// Get a snapshot of the running object table (ROT).
/// </summary>
/// <returns>A hashtable mapping the name of the object in the ROT to the corresponding object
/// <param name="filter">The filter to apply to the list (nullable).</param>
/// <returns>A hashtable of the matching entries in the ROT</returns>
public static Hashtable GetActiveObjectList(string filter)
{
Hashtable result = new Hashtable();
int numFetched;
UCOMIRunningObjectTable runningObjectTable;
UCOMIEnumMoniker monikerEnumerator;
UCOMIMoniker[] monikers = new UCOMIMoniker[1];
GetRunningObjectTable(0, out runningObjectTable);
runningObjectTable.EnumRunning(out monikerEnumerator);
monikerEnumerator.Reset();
while (monikerEnumerator.Next(1, monikers, out numFetched) == 0)
{
UCOMIBindCtx ctx;
CreateBindCtx(0, out ctx);
string runningObjectName;
monikers[0].GetDisplayName(ctx, null, out runningObjectName);
if (filter == null || filter.Length == 0 || runningObjectName.IndexOf(filter) != -1)
{
object runningObjectVal;
runningObjectTable.GetObject(monikers[0], out runningObjectVal);
result[runningObjectName] = runningObjectVal;
}
}
return result;
}
/// <summary>
/// Returns an object from the ROT, given a prog Id.
/// </summary>
/// <param name="progId">The prog id of the object to return.</param>
/// <returns>The requested object, or null if the object is not found.</returns>
public static object GetActiveObject(string progId)
{
// Convert the prog id into a class id
string classId = ConvertProgIdToClassId(progId);
UCOMIRunningObjectTable prot = null;
UCOMIEnumMoniker pMonkEnum = null;
try
{
int Fetched = 0;
// Open the running objects table.
GetRunningObjectTable(0, out prot);
prot.EnumRunning(out pMonkEnum);
pMonkEnum.Reset();
UCOMIMoniker[] pmon = new UCOMIMoniker[1];
// Iterate through the results
while (pMonkEnum.Next(1, pmon, out Fetched) == 0)
{
UCOMIBindCtx pCtx;
CreateBindCtx(0, out pCtx);
string displayName;
pmon[0].GetDisplayName(pCtx, null, out displayName);
Marshal.ReleaseComObject(pCtx);
if (displayName.IndexOf(classId) != -1)
{
// Return the matching object
object objReturnObject;
prot.GetObject(pmon[0], out objReturnObject);
return objReturnObject;
}
}
return null;
}
finally
{
// Free resources
if (prot != null)
Marshal.ReleaseComObject(prot);
if (pMonkEnum != null)
Marshal.ReleaseComObject(pMonkEnum);
}
}
Then here's the method in my class to get the instances:
/// <summary>
/// This strategy of getting the VSO.Application instances uses the appropriate file monikers
/// from the Runtime Object Table to get the VSO.Document object and hence the VSO.Document.Application
/// property.
/// </summary>
/// <param name="allInstances"></param>
protected void GetInstancesROTStrategy(IList<VSO.Application> allInstances)
{
// Iterate through all the objects in the ROT
string filter = ".vdx";
Hashtable runningObjects = ROTUtil.GetActiveObjectList(filter);
Hashtable appInstances = new Hashtable();
// Display the object ids
foreach (DictionaryEntry de in runningObjects)
{
// get visio document file monikers
string progId = de.Key.ToString();
object getObj = ROTUtil.GetActiveObject(progId);
if (getObj != null)
{
// try to cast the object to a VSO.Document
VSO.Document doc = getObj as VSO.Document;
if (doc != null)
{
VSO.Application app = doc.Application;
// only add to results IList if not duplicate
if (!appInstances.ContainsKey(app.WindowHandle32))
{
appInstances.Add(app.WindowHandle32, app);
allInstances.Add(app);
}
}
}
}
}

C# Compact-Framework friendly command line parser

I read this question: Command Line Parser for .NET.
I thought that was what I was looking for, but the library Command Line Parser Library is not Compact framework friendly...
I REALLY don't want to write a CL parser and I have been drifting away from the real purpose of my little app because of this unfortunate trial.
Does someone know of a library that fits the compact-framework? (preferably with simplicity and functionality like the one mentioned above)
Does not matter whether version 2 or 3.5
I developed this framework, maybe it helps:
The SysCommand is a powerful cross-platform framework, to develop Console Applications in .NET. Is simple, type-safe, and with great influences of the MVC pattern.
https://github.com/juniorgasparotto/SysCommand
namespace Example.Initialization.Simple
{
using SysCommand.ConsoleApp;
public class Program
{
public static int Main(string[] args)
{
return App.RunApplication();
}
}
// Classes inheriting from `Command` will be automatically found by the system
// and its public properties and methods will be available for use.
public class MyCommand : Command
{
public void Main(string arg1, int? arg2 = null)
{
if (arg1 != null)
this.App.Console.Write(string.Format("Main arg1='{0}'", arg1));
if (arg2 != null)
this.App.Console.Write(string.Format("Main arg2='{0}'", arg2));
}
public void MyAction(bool a)
{
this.App.Console.Write(string.Format("MyAction a='{0}'", a));
}
}
}
Tests:
// auto-generate help
$ my-app.exe help
// method "Main" typed
$ my-app.exe --arg1 value --arg2 1000
// or without "--arg2"
$ my-app.exe --arg1 value
// actions support
$ my-app.exe my-action -a
This is what I'm using. I borrowed it from somewhere, but not sure where:
using System.Collections.Specialized;
using System.Text.RegularExpressions;
/// <summary>
/// Parses the command line arguments into a name/value collection
/// </summary>
public class CommandLineArgumentParser
{
#region Fields
private StringDictionary parameters;
#endregion
#region Constructors
/// <summary>
/// Initializes a new instance of the <see cref="CommandLineArgumentParser"/> class.
/// </summary>
/// <param name="args">command-line arguments
/// </param>
public CommandLineArgumentParser(string[] args)
{
this.parameters = new StringDictionary();
Regex spliter = new Regex(#"^-{1,2}|^/|=|:", RegexOptions.IgnoreCase | RegexOptions.Compiled);
Regex remover = new Regex(#"^['""]?(.*?)['""]?$", RegexOptions.IgnoreCase | RegexOptions.Compiled);
string parameter = null;
string[] parts;
// Valid parameters forms:
// {-,/,--}param{ ,=,:}((",')value(",'))
// Examples:
// -param1 value1 --param2 /param3:"Test-:-work"
// /param4=happy -param5 '--=nice=--'
foreach (string txt in args)
{
// Look for new parameters (-,/ or --) and a
// possible enclosed value (=,:)
parts = spliter.Split(txt, 3);
switch (parts.Length)
{
// Found a value (for the last parameter
// found (space separator))
case 1:
if (parameter != null)
{
if (!this.parameters.ContainsKey(parameter))
{
parts[0] = remover.Replace(parts[0], "$1");
this.parameters.Add(parameter, parts[0]);
}
parameter = null;
}
// else Error: no parameter waiting for a value (skipped)
break;
// Found just a parameter
case 2:
// The last parameter is still waiting.
// With no value, set it to true.
if (parameter != null)
{
if (!this.parameters.ContainsKey(parameter))
{
this.parameters.Add(parameter, "true");
}
}
parameter = parts[1];
break;
// Parameter with enclosed value
case 3:
// The last parameter is still waiting.
// With no value, set it to true.
if (parameter != null)
{
if (!this.parameters.ContainsKey(parameter))
{
this.parameters.Add(parameter, "true");
}
}
parameter = parts[1];
// Remove possible enclosing characters (",')
if (!this.parameters.ContainsKey(parameter))
{
parts[2] = remover.Replace(parts[2], "$1");
this.parameters.Add(parameter, parts[2]);
}
parameter = null;
break;
}
}
// In case a parameter is still waiting
if (parameter != null)
{
if (!this.parameters.ContainsKey(parameter))
{
this.parameters.Add(parameter, "true");
}
}
}
#endregion
#region Properties
/// <summary>
/// Gets a count of command line arguments
/// </summary>
public int Count
{
get
{
return this.parameters.Count;
}
}
/// <summary>
/// Gets the value with the given parameter name
/// </summary>
/// <param name="param">name of the parameter</param>
/// <returns>the value of the parameter</returns>
public string this[string param]
{
get
{
return this.parameters[param];
}
}
#endregion
}
http://commandline.codeplex.com/ I've used this so many times I've lost count. Maybe it works for CE. If not, it'll provide a fantastic starting point.