|
OK I understand, but the question still remains..
How do you properly make a piece of code async? Most examples only show how to CALL an async method.
This SO question is exactly what I'm asking
public async List<SomeEntity> GetSomeEntities()
{
return await SomePlace.SomeMethod();
}
What makes 'SomeMethod' async? What I want is for the client to make a call, fire it off, and then handle the results when its finished. But down the end something has to be coded in such a way that its async. What does that look like?
The SO posting has this answer
await Task.Run(() => ...);
[Here's another similar SO post](https://stackoverflow.com/questions/21700846/how-to-write-an-awaitable-method)
but it sounds like you're saying that this isnt't the way to make the method async.
If it's not broken, fix it until it is.
Everything makes sense in someone's mind.
Ya can't fix stupid.
modified 10-May-19 16:50pm.
|
|
|
|
|
Using Task.Run is fine in a desktop application if you want to push CPU-bound work onto a background thread, so that your UI doesn't freeze.
In server-side code, it doesn't make a lot of sense to use async for CPU-bound work. There is no UI, so a background thread will just make more work for the server.
Have a look at the link from Alberto's answer:
However, avoid "fake asynchrony" in libraries. Fake asynchrony is when a component has an async-ready API, but it’s implemented by just wrapping the synchronous API within a thread pool thread. That is counterproductive to scalability on ASP.NET.
For server-side code, async is best suited for IO-bound code. In other words, when your code is waiting for some external resource - eg: a file-system call, a network request, a database query. In that case, the async code will most likely use an IO completion port[^] to resume the method when the request has completed, and no threads will be tied up waiting for a response.
Actually writing a low-level async method which isn't just composed of calling other async methods is usually not a trivial task. Probably one of the simplest examples would be Task.Delay[^], which uses a Timer to trigger the completion of a task. Stephen Toub also wrote a whole series of posts about building async coordination primitives[^] using the TaskCompletionSource class.
"These people looked deep within my soul and assigned me a number based on the order in which I joined."
- Homer
|
|
|
|
|
Everything Richard said, with one quick note:
If you're binding async results to a WPF UI, you likely will need to run a completion task on the presentation layer that will perform assignment on the UI thread. I've had issues with that before.
public void GetEmployees()
{
_bl.GetAllEmployees().ContinueWith(task => {
ui.EmployeeList = task.result;
},
TaskScheduler.FromCurrentSynchronizationContext());
}
"Never attribute to malice that which can be explained by stupidity."
- Hanlon's Razor
|
|
|
|
|
Agreed. Thank you
If it's not broken, fix it until it is.
Everything makes sense in someone's mind.
Ya can't fix stupid.
|
|
|
|
|
I have this simple Lookup Items Editor that I use all over my app. It's used for many different types of lookups. The items in the Lookup Editor are loaded into lists and combos. The PK of the selected lookup item then becomes an FK in some other table.
The Remove button is a new customer requirement.
Before, once an item was added, it could not be removed. Now, they want to be able to keep the lookup lists clean and remove items not used.
So the question then is this... .how do I know if/where a lookup item has been used. I could write a method that looks in each table that uses it to see if the FK appears, but I'm not sure if this is the best approach.
Suggestions are welcome.
If it's not broken, fix it until it is.
Everything makes sense in someone's mind.
Ya can't fix stupid.
|
|
|
|
|
Add a disabled field as a nullable datetime to the lookup items table. Then filter the lookup lists, this maintains the FK requirement.
If your client is not satisfied with that then get a new client, they can set requirements they can't design your data structure.
Never underestimate the power of human stupidity -
RAH
I'm old. I know stuff - JSOP
|
|
|
|
|
My client is very reasonable. Great working relationship.
The fix we came up with it to assign rights to it so that only a few users can edit lookups. They're mostly concerned with duplication entries like
"2x4"
And
"2 x 4"
Having one or 2 users manage this will probably solve it because these guys know what's acceptable
Thanks
If it's not broken, fix it until it is.
Everything makes sense in someone's mind.
Ya can't fix stupid.
|
|
|
|
|
Ah that is a different problem to the FK requirement. The mechanical way is to check the FK constraint to find the table it joins (assuming the lookup item table services many lookups) to then check the table(s) for usage of the value to be deleted.
Or simply rely on limited user capabilities and their common sense
Never underestimate the power of human stupidity -
RAH
I'm old. I know stuff - JSOP
|
|
|
|
|
That's exactly my concern. Two different items that look close and even mean the same are really 2 PKs.
As I said I could write code to check usage but my security infrastructure is already in place.
It doesn't completely remove the problem but it minimizes it
If it's not broken, fix it until it is.
Everything makes sense in someone's mind.
Ya can't fix stupid.
|
|
|
|
|
Kevin Marois wrote: Two different items that look close and even mean the same are really 2 PKs. If the user doesn't know that the combination of those fields (which a PK usually is), then the user doesn't know what makes an item unique, and will be causing duplicate entries.
An overengineered solution; save a soundex of your item. If another is added, check if the soundex (or Levenshtein distance) of it is already there, and open a messagebox suggesting a similar item is already there (Google's "did you mean x?"). You can put these in a separate table.
Bastard Programmer from Hell
If you can't read my code, try converting it here[^]
"If you just follow the bacon Eddy, wherever it leads you, then you won't have to think about politics." -- Some Bell.
|
|
|
|
|
If you were to design a new software these days for line of business, would you go for J2EE or the new stuff if dot net was not a choice at all ?
Caveat Emptor.
"Progress doesn't come from early risers – progress is made by lazy men looking for easier ways to do things." Lazarus Long
|
|
|
|
|
I wouldn't; I'd challenge the choice and go for .NET.
Bastard Programmer from Hell
If you can't read my code, try converting it here[^]
"If you just follow the bacon Eddy, wherever it leads you, then you won't have to think about politics." -- Some Bell.
|
|
|
|
|
Sounds like one of my jobs.
(No .NET; sent me on a bunch of Java courses, yada, yada ... then said we could use .NET after all; so hang in there, I guess).
"(I) am amazed to see myself here rather than there ... now rather than then".
― Blaise Pascal
|
|
|
|
|
abmv wrote: , would you go for J2EE or the new stuff
You you need to be a bit more specific than that.
In general no I would not use "J2EE" mainly be cause it is 20 years old. So "new stuff" in this context would be more like "pick something that is currently supported."
My focus would be on Spring probably with either Tomcat or JBoss.
abmv wrote: if dot net was not a choice at all
Both technologies have advantage and disadvantages. With more than 10 years of experience in both, Java has a better service oriented eco system so I would normally pick that.
HOWEVER, that only works if you are building/hiring a new group. If you are using an existing group that has experience one of the stacks then you should be using that stack rather than forcing them to use something new.
|
|
|
|
|
A while back I posted questions about this, then walked away. Now I'm considering going back at it again.
The basic idea was to use an enhancement of FileSystemWatcher that I found here (I think it was this one) that solves some issues like multiple events firing for the same file/folder. It works really well and that's not my question.
The big issues I ran into are:
- How to handle multiple versions of the same file being dropped into the watch folder on different PC's at the same time. With latency, I guess I could always take the last file, but it's still a concern.
- How to deal with a file or folder being deleted or edited during a sync. Example, user creates a file/folder, the the sync starts, then as it's syncing, deletes it? or opens it with some app?
I tried to do some file locking as soon as the sync starts, but it's tricky to get the timing right.
The summary of this is, what's the right way to manage files & folders like DropBox does regardless of what's happening to a file or folder?
I'd appreciate some insight.
Thanks
If it's not broken, fix it until it is.
Everything makes sense in someone's mind.
Ya can't fix stupid.
|
|
|
|
|
dropbox basically tracks and maintains some info--- C:\Users\xxx\AppData\Local\Dropbox\instance1 -- i think they are considering each box running on diff machine as an instance and do it like in a database u lock and win.. u can run process mon and see what happen ....
Caveat Emptor.
"Progress doesn't come from early risers – progress is made by lazy men looking for easier ways to do things." Lazarus Long
|
|
|
|
|
Something like Dokan.Mem, A filesystem Prototype[^]? It's a bit dated tough.
Bastard Programmer from Hell
If you can't read my code, try converting it here[^]
"If you just follow the bacon Eddy, wherever it leads you, then you won't have to think about politics." -- Some Bell.
|
|
|
|
|
Save 'em all; tack on a time stamp and source code; sort it all out later.
Some are probably more right than others, by the sounds of it.
"(I) am amazed to see myself here rather than there ... now rather than then".
― Blaise Pascal
|
|
|
|
|
Gerry Schmitz wrote: Save 'em all; tack on a time stamp and source code; sort it all out later.
Source code?
If it's not broken, fix it until it is.
Everything makes sense in someone's mind.
Ya can't fix stupid.
|
|
|
|
|
The "source" of the data: IP address; user id; MAC .... stuff like that. Audit trail.
"(I) am amazed to see myself here rather than there ... now rather than then".
― Blaise Pascal
|
|
|
|
|
I am trying to design a class for hierarchical data, I have some categories and dynamic number of subcategories for ex:
1.Technology --> C# --> Threading
-->IO
-->Exception
2.Domain --> Finance-->Equity
-->IR
-->Currency
like that multiple categories user can configure and then he will be able to add articles for any of above category. I have multiple thoughts like below codes and having confusion between below designs of classes
Public class Category{
public int Id { get; set;}
public string description { get; set;}
public int ParentCategoryId {get; set;}
}
Public class Category{
public int Id { get; set;}
public string description { get; set;}
public List<Category>SubCategories {get; set;}
}
Public class Category{
public int Id { get; set;}
public string description { get; set;}
public Category Parent {get; set;}
}
|
|
|
|
|
Use a recursive structure.
Public class Category {
public int Id {get;set;}
public int Depth {get;set;}
public Category Parent {get;set;}
public List<Category> Children = new List<Category>();
public bool HasChildren {get{return this.Children.Count > 0;}}
}
Depth 0 is a "root".
Depth 1 has a parent of depth 0
Depth 2 has a parent of depth 1
...
etc.
And the reverse: A child has a parent at depth - 1
Same model as used by "tree views".
"(I) am amazed to see myself here rather than there ... now rather than then".
― Blaise Pascal
|
|
|
|
|
Other reply answers the specific question but you might want to consider your design rather than how to implement it.
Hierarchical structures tend to be hard to search.
Is it possible that what you really want are entities with properties?
So...
Entity 1: Domain, Finance, Equity
You might even consider making the categories hierarchical but the entities point to them. Since the categories themselves are a much smaller set then complicated searches on them take less time.
|
|
|
|
|
The fact that one can have multiple "views" of a single collections means they can be "easy" to search: by building "secondary" collections / dictionaries of item "references" over the primary collection.
A tree view, as it's base, uses a list view; which is a "flat" structure.
The "nodes" are indented list items. The "collapsing" is controlled by toggling visibility.
"(I) am amazed to see myself here rather than there ... now rather than then".
― Blaise Pascal
|
|
|
|
|
I was suggesting alternatives that should be explored before deciding on a path.
Gerry Schmitz wrote: The fact that one can have multiple "views" of a single collections means they can be "easy" to search: b
Could be. But that depends on actual business needs.
Gerry Schmitz wrote: A tree view,
Just to be clear this is a parent/child structure which could be implemented as a tree.
And searching tree structures based on parent/child structures will be 'slow' in some sense of the word. It is quite possible that the business needs, to which this post is insufficient to describe, might allow for a different structure which would provide better performance, based on the business needs.
|
|
|
|