|
ehm:
string inputString = @"#Test{border-top:Solid blue 1px; border-bottom:1% solid black;}";
string escapedString = Regex.Escape("border-bottom");
string pattern = escapedString + @"\:\s?\d+\W(\s?\w+)*\;?\s?";
Regex rgx = new Regex(pattern);
Match match = rgx.Match(inputString);
string matchedString = match.Value;
Console.WriteLine(matchedString);
It works for me. At least when I set the inputString to match you expression.
|
|
|
|
|
It is now working. It was driving me nuts, after everything failed I decided to restart Visual Studio and it started working.
|
|
|
|
|
Rule 1: Whenever a problem goes away on its own, it will come back on its own.
|
|
|
|
|
Means the code that you were looking at was not the code that was running.
Perhaps because it was running a prior build rather than with your updates.
|
|
|
|
|
public async Task<List<ActiveDirectoryUser>> ImportUsersAsync(string basePath)
{
return await Task.Run(() =>
{
List<ActiveDirectoryUser> users = new List<ActiveDirectoryUser>();
var rootEntry = new DirectoryEntry(basePath);
var searcher = new DirectorySearcher(rootEntry);
SearchResultCollection results = searcher.FindAll();
if (results != null)
{
foreach (SearchResult result in results)
{
ActiveDirectoryUser user = new ActiveDirectoryUser();
if (result.Properties.Contains("displayname"))
{
user.DisplayName = (string)result.Properties["displayname"][0];
user.LoginName = (string)result.Properties["samaccountname"][0];
foreach (var prop in result.Properties)
{
DictionaryEntry de = (DictionaryEntry)prop;
ResultPropertyValueCollection rpvc = (ResultPropertyValueCollection)de.Value;
user.Properties.Add(de.Key.ToString(), rpvc[0].ToString());
}
users.Add(user);
}
}
}
return users;
});
}
now, when i run it:
ActiveDirectoryHelper adHelper = new ActiveDirectoryHelper();
var users = adHelper.ImportUsersAsync(basePath);
var user = adHelper.GetUserInfo(serverName, userName);
Console.WriteLine("Done");
How can I know when the async method finished?
If it's not broken, fix it until it is.
Everything makes sense in someone's mind.
Ya can't fix stupid.
modified 29-Nov-17 12:17pm.
|
|
|
|
|
You don't. The way you set it up, it's "fire and forget".
You've got the await in the wrong place. That should go on the callers side, not the function ...Async side. The Async method should return a Task. That lets the caller await the Task.
Something like this:
private Task<List<string>> GetUsersAsync()
{
return Task.Run(() =>
{
List<string> userList = new List<string>() {
"User1", "User2", "User3", "User4", "User5"
};
Thread.Sleep(5000);
return userList;
});
}
Then you can call it like this (very simple Windows Forms example):
private async void button1_Click(object sender, EventArgs e)
{
var users = await GetUsersAsync();
foreach (string user in users)
{
listBox1.Items.Add(user);
}
}
Is this production quality code? NO! It's meant to be a simple example demonstrating Task/Async/Await. It doesn't handle exceptions and doesn't make sure the Click handler doesn't get called again while the first Task is still running.
System.ItDidntWorkException: Something didn't work as expected.
C# - How to debug code[ ^].
Seriously, go read these articles.
Dave Kreskowiak
|
|
|
|
|
Thank you!
If it's not broken, fix it until it is.
Everything makes sense in someone's mind.
Ya can't fix stupid.
|
|
|
|
|
var users = adHelper.ImportUsersAsync(basePath);
change in
var users = adHelper.ImportUsersAsync(basePath).Result;
|
|
|
|
|
That's not going to fix the problem at all and you're really not even changing the code. The .Result is implied in the original post.
System.ItDidntWorkException: Something didn't work as expected.
C# - How to debug code[ ^].
Seriously, go read these articles.
Dave Kreskowiak
|
|
|
|
|
No it is not implied... He is just assigning a non started Task to user.
As long as he doesnt await user() or user.Result... Then nothing ever happens with it.
|
|
|
|
|
Oh no... That Result there is making a sync method out of the async method. And Result shouldn't be used like that... You are asking for deadlocks that way.
|
|
|
|
|
Hi,
I am working on Simple injector DI framework. I need good ideas on how to register interface which is consumed in more than one provider (component).
Below is my requirement.
It is WebAPI project where I have a BaggageController class which will get a request for Add Baggage operation, Based on a provider from the request I have to route the request to concerned class. A Provider can be an Airline A or Airline B.
Request for AddBaggage(BaggageRequest, Provider) from client
--> BaggageController --> Business.Baggage.Implementation (Decision) --> Call Provider A
--> Call Provider B
Below is the code I am registering with Simple injector.
container.Register<ibaggageservice, baggageservice="">(Lifestyle.Scoped);
container.Register<iflightservice, providera="">(Lifestyle.Scoped);
container.Register<iflightservice, providerb="">(Lifestyle.Scoped); --> This line throws an error as IFlightService is already registered.
Since my decision would be taken at runtime, I can not use the RegisterConditional method.
Is there any other way to register dependency at runtime using simple injector?
Thank you
|
|
|
|
|
How would the application know which of the two to create, based on only knowing IFlightService? Register a ProviderStrategy that returns either ProviderA or ProviderB.
Bastard Programmer from Hell
If you can't read my code, try converting it here[^]
|
|
|
|
|
In order to do this, Could you please share some example? I have been searching it from quite a long time.
|
|
|
|
|
Nope, just a description. Instead of registering two different classes for a single interface,
container.Register<IFlightService, ProviderA>(Lifestyle.Scoped); You want one class to register. That one class should invoke the required stuff for either ProviderA or ProviderB.
Bastard Programmer from Hell
If you can't read my code, try converting it here[^]
|
|
|
|
|
I have a set of calculation methods (10) that return a list of decimals, what is the best (simplest) way to invoke the methods and get the return list of decimals?
public void CalcKeyRef()
{
foreach (KeyRefDB oKR in lAllKeyRef)
{
string sMethod = string.Format("DoKR{0}", oKR.KeyRefID);
List<decimal> lResults = ????
}
}
private List<decimal> DoKR1(List<decimal> lResults)
{
lResults = new List<decimal>();
return lResults;
}
Never underestimate the power of human stupidity
RAH
modified 28-Nov-17 20:28pm.
|
|
|
|
|
All you have are the names of the methods?
Where do they come from?
How often do you need to do this?
|
|
|
|
|
You could use reflection: https://www.dotnetperls.com/getmethod
or perhaps an array of delegates:
public delegate List<decimal> DoKR(List<decimal> lResults);
public DoKR[] DoKRs;
public void SomeInit()
{
DoKRs = new DoKR[] { DoKR1, DoKR2, DoKR3, ... };
}
public void CalcKeyRef()
{
List<decimal> param = new List<decimal>();
foreach (KeyRefDB oKR in lAllKeyRef)
{
List<decimal> lResults = DoKRs[oKR.KeyRefID](param);
}
}
|
|
|
|
|
Works for me - only catch was the identity field is not a zero index value.
Thank you.
Never underestimate the power of human stupidity
RAH
|
|
|
|
|
You're welcome. My pleasure.
You could add a dummy (or use -1 with the Id)
DoKRs = new DoKR[] { DoKR1, DoKR1, DoKR2, DoKr3, ... };
|
|
|
|
|
If you have the methods available: I'd be using a Dictionary:
public Dictionary<KeyRefID, Func<List<decimal>> KeyRefToFunc;
public List<decimal> GetListById(KeyRefDB oKR)
{
if(KeyRefToFunc == null) throw new ... whatever
if(KeyRefToFunc.ContainsKey(oKR.KeyRefID))
{
return KeyRefToFunc[oKR.KeyRefID]();
}
throw new ... whatever
}
«While I complain of being able to see only a shadow of the past, I may be insensitive to reality as it is now, since I'm not at a stage of development where I'm capable of seeing it.» Claude Levi-Strauss (Tristes Tropiques, 1955)
|
|
|
|
|
This reads as more obscure than Peters solution, possibly because I rarely use dictionary . I will always go for the clearest solution as I will not have to puzzle out the working in the future.
Never underestimate the power of human stupidity
RAH
|
|
|
|
|
Hi, MyCroft, When I was first discovering (2006) [^] sticking executable code into Dictionaries, it felt very weird. Now, it's even easier since we have Action, Func, and Lambdas, to get us over the Delegate "syntax hump" that confounds many newcomers to C#. Of course, a Delegate lets you have a params list, and Action, and Func, do not.
Consider:
delegate int Calc(params int[] args);
Dictionary<string, Calc> CalcDict = new Dictionary<string, Calc>
{
{"+", (iary) => iary.Sum()},
{"-", (iary) =>
{
for (var i = 1; i < iary.Length; i++)
{
iary[0] -= iary[i];
}
return iary[0];
}
}
};
From my perspective, this "saves you" writing a 'switch statement, and handles variable length input parameters.
But, trust me ... I'm not trying to "sell" you anything, just adding this for the sake of the discussion.
«While I complain of being able to see only a shadow of the past, I may be insensitive to reality as it is now, since I'm not at a stage of development where I'm capable of seeing it.» Claude Levi-Strauss (Tristes Tropiques, 1955)
|
|
|
|
|
using System;
using System.IO;
using System.Collections;
namespace Applica
{
static class Program
{
static void Main(string[] args)
{
bool tif = true;
byte[] buffer = BitConverter.GetBytes(tif);
FileInfo ap = new FileInfo("tempii.txt");
string filePath = ap.FullName;
string destinationPath = Path.Combine("C:\\check", Path.GetFileName(filePath));
using (Stream output = File.OpenWrite(destinationPath))
{
int bits = 32;
while (bits > 0)
{
for (int i = 1; i < 33; i++)
{
if (tif == true)
{
tif = false;
goto A;
}
if (tif == false) tif = true;
A:;
output.Write(buffer, 0, bits);
}
}
}
}
}
}
modified 28-Nov-17 20:21pm.
|
|
|
|
|