Asynchronus database population [closed] - c#

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 8 years ago.
Improve this question
In my application I am using SOAP service to download CSV files which are used to populate different tables in a Sqlite database. I am using SQLite.Net library to for database operations.
For populating the database I have written methods for each table (because of different schema). Those methods are running synchronously. Inside those methods I am using instance of a synchronous SQL connection.
When I changed my methods from void to async Task and calling them with await I don't know if those methods finished, and when.
My question is what approach should I take to be able to change those operations to asynchronous and how to define events when each methods finishes. I never wrote event before so it would be greatly appreciated if some one could give some sample how to do that.

The Task returned from the asynchronous methods gives you what you need. The Task type will notify the calling code when the asynchronous method completes.
The easiest way to take advantage of this is to have the calling code use await. For example, if you want to (asynchronously) wait for each table to be populated:
await PopulateTable1Async();
await PopulateTable2Async();
await PopulateTable3Async();
Alternatively, you may want to populate all tables simultaneously, in which case you can use Task.WhenAll:
await Task.WhenAll(PopulateTable1Async(), PopulateTable2Async(), PopulateTable3Async());

Related

Do async writes to a single file have to be awaited? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 2 years ago.
Improve this question
Is it safe to call File.WriteAllTextAsync to write to a single file multiple times without awaiting the result, as long as all the calls are made in a single thread?
By safe I mean:
no IO exception will be thrown
afterwards the file will have the content of the last call made
This seems to run fine, but is it guaranteed to do so?
for (var i = 0; i < 1000; ++i)
{
File.WriteAllTextAsync(fileName, i.ToString());
}
No, it is not safe. You may get "File already in use " exception.
Either you need await or do it synchronously.
No, it's not safe. It is not guaranteed that no exceptions will be thrown¹ or that all text will be written in the file. The only reason that it seems to work is because the asynchronous filesystem APIs are not implemented efficiently in .NET, and most of them block the calling thread instead of launching the operations asynchronously as they should. My suggestion is to use the synchronous API instead (File.WriteAllText).
¹ It is possible though that the thrown exceptions will remain unobserved, since the asynchronous operations are launched in a fire-and-forget fashion.

Thread State Management in Asynchronous Programming [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 6 years ago.
Improve this question
I am new to asynchronous programming in .Net using C#. All I have understood so far is :
With asynchronous programming, threads that are waiting for a web service or database to return data are freed up to service new requests until the data the is received.
Once the data is received, the thread is restarted and continue processing the code that comes after that call.
Now , I wanted to know in details How the state is managed for the thread so that it can start executing from the point the async call was made.
If it uses a stack to handle that, Can some one please give me an insight into the process?
Thanks,
Mayank
Now , I wanted to know in details How the state is managed for the thread so that it can start executing from the point the async call was made.
Async method are divided into smaller chunks. Basically, when compiling async method, for every await keyword new method is generated.
Keep in mind, that this is a big simplification and it's all done behind the scenes and you really don't need to know how it works in order to use it.

multiple awaits and async methods in an application [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 6 years ago.
Improve this question
I'm in the process of converting our data layer for a fairly large and complex WCF application to talk to the database asynchronously.
This has resulted in async and awaits being littered everywhere in the calling/consuming code.
Looking at the stack trace for a typical request I can already see many sections for the System.Runtime.CompilerServices.TaskAwaiter doing it's thing with await. And I have only just started this task!
I understand what .net does when it encounters async/await, so my question is the following: Is the extra overhead associated working with async/await worth it when the result is quite a few async methods from the beginning of a request to the end? I understand the benefits of calling the database asynchronously but is there a limit? Particularly when the calling application is fairly large and complex (or more appropriately a large and long call-stack).
Thanks.

Async/await is a fine syntactic sugar [closed]

Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 7 years ago.
Improve this question
Can we say that the C# async/await pattern is some kind of
(smart) sugar syntax ?
Because as far as I can see, at the very end, it "just" hides all the implementation of an (internal) worker thread for the programmer.
Yes, there is no CLR support for await. It goes away on compilation. Anything you can write with await you can write without it.
Awaiting tasks it is a fancy way of calling ContinueWith :)
Decompile an assembly that uses await to see how it works under the covers.
async/await is indeed syntactic sugar, however, it doesn't just hide threads. Just because you use async and await in a method does not mean that you are creating threads.
From the MSDN Article Asynchronous Programming with Async and Await (C# and Visual Basic):
The async and await keywords don't cause additional threads to be
created. Async methods don't require multithreading because an async
method doesn't run on its own thread. The method runs on the current
synchronization context and uses time on the thread only when the
method is active. You can use Task.Run to move CPU-bound work to a
background thread, but a background thread doesn't help with a process
that's just waiting for results to become available.

C# need 2 threads for long execution [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 7 years ago.
Improve this question
I have a program that loads data from a SharePoint site. It loads txt files, xml files, etc. Any of these "load" actions can take a lot of time because of the user's connectivity to the SharePoint. Therefore the whole windows form UI gets unresponsive until the data is loaded.
So I would like to know how can I easily create a thread for that "retrieval" of information while the whole windows forms UI still works and is operative.
You have a few options. I'm not going to provide exact code for any of them, but, I will provide you with research topics.
You can use a BackgroundWorker, Task.Run() or manage your own threading by doing Thread.Start(). Do you need to fire off an event when the downloading is finished? If so, you can do something like this:
var task = new Task(() => DoSomething());
task.ContinueWith(() => SignalDone(), TaskScheduler.FromCurrentSynchronizationContext());
task.Run();
The ContinueWith and TaskScheduler.FromCurrentSynchronizationContext will ensure that the signaling will be done on the UI thread to minimize race conditions. You're on your own if you're doing databinding to anything being populated.

Categories

Resources