|
No, sorry, that's not correct.
The await client.GetAsync(url) and await response.Content.ReadAsStringAsync() calls will not block the current thread. They will sign up the rest of the method as a continuation, which will be invoked when the returned Task<T> has completed.
The GetAsync and ReadAsStringAsync calls (eventually) use an IO completion port to receive notification when the request has completed.
That's the whole point of async / await .
The only thing your example is doing differently is that it's using a background thread to run the very first part of the method - the call to GetAsync . As a result, the rest of the method will not run on the captured execution context from the caller of RefreshData . But you could achieve the same result by adding .ConfigureAwait(false) to the end of the GetAsync and ReadAsStringAsync calls.
"These people looked deep within my soul and assigned me a number based on the order in which I joined."
- Homer
|
|
|
|
|
I was completely lazy with that sample and I accept that, but it was just to illustrate a point. I think the issue we're having is that we're discussing different contexts.
I'm talking about the code that I'm looking at. I can't see the calling code, so am therefore not addressing it, and that's the code you're talking about. I realize that internal conventions are not the same as generally accepted practice, and my point about the return type and naming convention was not relevant, so I accept proper chastisement there.
I think we can both agree that the code we're looking at, and just that code, will not behave in an asynchronous manner.
"There are three kinds of lies: lies, damned lies and statistics."
- Benjamin Disraeli
|
|
|
|
|
But that's the point - it WILL behave in an asynchronous manner!
public async Task<List<todoitem>> RefreshDataAsync ()
{
Items = new List<todoitem> ();
RestUrl = "http://localhost:9054/TotoItem"
var uri = new Uri (string.Format (RestUrl, string.Empty));
try {
var response = await client.GetAsync (uri);
if (response.IsSuccessStatusCode) {
var content = await response.Content.ReadAsStringAsync ();
Items = JsonConvert.DeserializeObject <List<todoitem>> (content);
}
} catch (Exception ex) {
Debug.WriteLine (@" ERROR {0}", ex.Message);
}
return Items;
}
"These people looked deep within my soul and assigned me a number based on the order in which I joined."
- Homer
|
|
|
|
|
var response = await client.GetAsync (uri);<br />
if (response.IsSuccessStatusCode) {
This is an synchronous pattern within the context of the method, which is the only code that we see. No other actions will be taken by the available code until this resolves. The lack of thread blocking is not the point, the code execution flow in the method is the same as if the thread were blocked.
The Task continuation is only of interest to the calling code so that it can treat it appropriately.
"There are three kinds of lies: lies, damned lies and statistics."
- Benjamin Disraeli
|
|
|
|
|
The same argument applies to your "asynchronous" version:
public async Task<T> RefreshData<T>(string url)
{
return Task<T>.Run(()=>
{
var response = await client.GetAsync (url);
...
})
.ContinueWith(ex => { Debug.WriteLine ($"ERROR {ex.Message}");}, TaskContinuationOptions.OnlyOnFaulted));
}
You've just moved the "logically synchronous" part to a lambda method, at the expense of making the outer method more complicated.
Adding the *Async suffix to a task-returning method is the recommended naming convention. Adding an extra Task.Run for extra asynchronicity doesn't really achieve anything. It's only going to have any effect if your method has with a long-running synchronous block before the first await call, and Task.Yield would probably be a better solution for that.
"These people looked deep within my soul and assigned me a number based on the order in which I joined."
- Homer
|
|
|
|
|
As I said: it was a bad, rushed example. I will continue to accept bads on that.
"There are three kinds of lies: lies, damned lies and statistics."
- Benjamin Disraeli
|
|
|
|
|
hello guys i have windows form having three dialog box which dispaly proper in win 8 but when i run the same application in windows 10 my forms dialog box have space between each other an its look very ugly
can any one tell me how should i give same look & feel in windows 10 also i mean i run my form in any of win 10 or 8 it should display proper look (without space in dialog box).
|
|
|
|
|
Pick "Windows Classic" in Control Panel on both systems and you should be good to go.
"(I) am amazed to see myself here rather than there ... now rather than then".
― Blaise Pascal
|
|
|
|
|
|
Ask the manufacturer for the technical documentation.
|
|
|
|
|
|
To give you an answer is difficult because of 2 reasons :
- at first : your answer is not very useful ...
- second : I suppose that most of us doesn't know your device. I don't know it too.
But following Suggestion :
Look for the Vendor-ID of your device.
Try to find out if you get get with this ID Information inside the Registry of your System which tells you if the device is currently connected and which Com-Ports are assigned. Most of the USB-Devices I know are writing Information like this into the Registry.
In this case I could give you a Sample-code which allows you to read this Information into your Application ...
But now it's up to you ...
|
|
|
|
|
Com port lists are a Windows registry function; nothing to do with what is actually hooked up (or not).
"(I) am amazed to see myself here rather than there ... now rather than then".
― Blaise Pascal
|
|
|
|
|
Hello, I have been working with C# for not very long. Less than a year and Im trying to use C#. My app basically adds in the argument needed such as host name, file location, etc to an app that acts like a listener for incoming dicom files(storescp.exe). My goal is to make this work as like an agent so one could start this app put in the arguments needed and click start and bobs ur uncle. However, when I do this storescp.exe goes into a listening mode like it should but until I kill that exe my app is frozen. My long winded question is how do I keep my app from freezing after I start storescu or storescp? Please help. I tryed using Awaiting Async but it didnt seem to work as I thought it would.
__________________
private void button2_Click(object sender, EventArgs e)
{
string port;
port = textBox3.Text;
string AE;
AE = textBox1.Text;
string dicompath;
dicompath = textBox7.Text;
string finalpath;
finalpath = port + " -pm" + " --fork " + " -aet " + AE + " -od " + ((char)34) + dicompath + ((char)34);
ProcessStartInfo startInfo2 = new ProcessStartInfo();
startInfo2.CreateNoWindow = true;
startInfo2.UseShellExecute = false;
startInfo2.RedirectStandardOutput = true;
startInfo2.WindowStyle = ProcessWindowStyle.Hidden;
startInfo2.FileName = "storescp-tls";
startInfo2.Arguments = finalpath;
startInfo2.RedirectStandardOutput = true;
var proc = Process.Start(startInfo2);
Thread.Sleep(10 * 1000);
string s = proc.StandardOutput.ReadToEnd();
string t = startInfo2.Arguments;
textBox2.Text = t;
textBox8.Text = s;
|
|
|
|
|
|
I'm reading this excellent article about MEF and a question came to mind.
In the article Tim discusses importing Property and Methods, and I thought, "Why would I want to import just a property or method? Why would not want to import the entire class?"
Under what circumstances would this be needed?
If it's not broken, fix it until it is.
Everything makes sense in someone's mind.
Ya can't fix stupid.
|
|
|
|
|
You should post your question in the article forum so the author can reply.
|
|
|
|
|
I could have, but then only He and his readers would see my question. And, I was hopi9ng for more diverse opinions than just his.
If it's not broken, fix it until it is.
Everything makes sense in someone's mind.
Ya can't fix stupid.
|
|
|
|
|
You could use MEF as a DI-driven stand-in for the factory pattern in a lot of scenarios using class-level composition.
You could inject properties and methods that are appropriate for the current application composition (Such as "I'm using YAML in this app instead of XML for configuration, let's inject a method to handle that").
Using shared parts, you could provide application-wide access to a variable without exposing the parent object.
That's off the top of the head.
"There are three kinds of lies: lies, damned lies and statistics."
- Benjamin Disraeli
|
|
|
|
|
Okay, importing a property is a fairly common way to avoid having an ImportingConstructor. The idea here is that you can inject the appropriate implementation in as a property, rather than using the constructor. As an example:
public class MyClass
{
private readonly IMyFirstService firstService;
private readonly IMySecondService secondService;
[ImportingConstructor]
public class MyClass(IMyFirstService firstService, IMySecondService secondService)
{
this.firstService = firstService;
this.secondService = secondService;
}
public IMyFirstService MyFirstService{ get { return firstService; } }
public IMySecondService MySecondService { get { return secondService; } }
} Now here's the Import version
public class MyClass
{
[Import]
public IMyFirstService { get; set; }
[Import]
public IMySecondService { get; set; }
} I don't mind the extra typing of the first version but some people would rather stick with the second.
This space for rent
|
|
|
|
|
So, I'm creating a bot within C# which will get some data from an API site. The data on the API site is in JSON.
If it returns something such as,
'{"Id":2,"Username":"JohnDoe","AvatarUri":null,"AvatarFinal":false,"IsOnline":false}',
how would I return the value in say Username to be JohnDoe?
Any help is greatly appreciated.
|
|
|
|
|
|
What have you tried so far ?
|
|
|
|
|
Hi, all master, I need to read the data from textfile to datagridview, but I am fail about this Operator " | ", please suggest me to good solution
example:
this data in textfile
3333;3333;3333|
6666;6666;6666|
-- modified 18-Mar-17 19:47pm.
|
|
|
|
|
Good solution for what? You haven't described a problem at all.
That character isn't "an operator". It's just a pipe character in a text file.
|
|
|
|