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

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.

Related

Does parallel processing help to execute async-await code more faster in c#? [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 3 years ago.
Improve this question
I have multi-core laptop and I wanna make my application more faster on that machine, I think async-await and parallel programming help to achieve that feat.
Typically, Async and await is more about not blocking the current thread during a long-running operation.
Typically, parallel processing is used when the goal is to achieve an increase in performance.
That said, I think it's not uncommon to see both used together--you might have a desktop app that you want to ensure the UI thread is not blocked while you perform a long-running operation
so you could use Async and await to start the long-running operation on a non-UI thread (this way, your desktop app's GUI can still be responsive while the long-running operation is running on another thread
and you might also decide to introduce parallelism in the implementation of that long-running operation to have it complete faster

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.

C# threading for file uploader [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 8 years ago.
Improve this question
I'm working with c# winform
I have a list of files .With a loop I need to upload all the files (5000 images ) to another server how can I implement it with multithreading
And another point how can I know when one thread is finished and then use it for the next file waiting to be uploaded do I need to use monitor class?
You can use PLINQ for that:
IEnumerable<string> yourFiles = new[]{ "C:\\file.txt", "D:\\data.dat" };
int numberOfThreads = 10;
yourFiles.AsParallel().WithDegreeOfParallelism(numberOfThreads).ForAll(UploadFile);
private static void UploadFile(string file)
{
// do the actual uploading
}
Maybe Parallel.For is something for you. It is easy to use. You know when a thread is finished because you can add some variable into the end of your method in the another thread. Something like ManualResetEvent. I think Parallel.For is the fasted to implement. You can use a thread pool aswell. Read trough the microsoft websites.
Parallel.For (.NET 4)
For(Int32, Int32, Action<Int32>)
ThreadPool (.NET 2)
ThreadPool.QueueUserWorkItem(waitCallback)

Synchronous method vs Thread.Sleep() [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 8 years ago.
Improve this question
I can't understand the difference between Thread.Sleep() and synchronous methods. Please explain..
Is synchronous method in C# is similar to Thread.Sleep()?.
No, other than Thread.Sleep is a synchronous method.
A synchronous method is one where the entirety of their functionality happens before returning control to the caller.
Thread.Sleep is a function which causes the executing thread to block for the specified number of milliseconds (not exactly, but close due to the CPU thread scheduler). It executes synchronously and wouldn't be as useful in an asynchronous method.
However, saying they are the same thing would be a misnomer, as there are plenty of synchronous methods that are not Thread.Sleep and do not use it either.

Asynchronus database population [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 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());

Categories

Resources