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)
Related
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 2 years ago.
Improve this question
I am new to threading and Multithreading . I have a method which Fetches Ids as input in JSON format from DB as
{
"ID": ["1",
....,
.....
"30000"]}
Now these Ids are to be processed again via WebAPI POST call. The issue is,though the code is optimized, it is taking hours to process all data.
How can I process these Ids in batch or multi threading to make it faster?
Recent versions of .NET have great libraries you can use that take care of the multi-threading for you.
Check out the Parallel For Each loop: https://learn.microsoft.com/en-us/dotnet/api/system.threading.tasks.parallel.foreach?view=netcore-3.1
You pass it a list and everything inside the loop is executed once for each item in the list and C# will do some of the iterations in parallel (multi-threaded). That means you could do your processing for more than one ID in parallel.
Whether or not it improves performance depends on the environment and work being done.
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.
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 have a windows form application which loads a list of paths to video files into an array, and then with a foreach (string[] file in fileList) loop, will go through each file and run some analysis of the video file, then write the result back to the filelist array.
The trouble is that it processes each video file at slightly less than real time which is not idea. I am aiming to split up the task across multiple threads. I have tested opening the application 5 times and ran the processing on separate files. The CPU handles this without issue.
What would be the simplest way to split up the processing across multiple threads?
Edit: I am new to multi threading, and currently learning. I know there are different ways to multi-thread but I am looking for the method I should be using in order to learn about it.
I found this example, however I don't understand how it works, it seems too simple compared to other examples I have been looking at.
Best way to do a multithread foreach loop
If the results are added to the same collection, PLINQ makes it a bit easier:
var results = fileList.AsParallel().Select(file => {
var bytes = File.ReadAllBytes(file);
var result = bytes.Length;
return result;
}).ToList();
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.
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
Once I needed to modify a variable that was inside another application in order to modify its behavior.
I wonder whether its possibile to create an application that will get access to another process running on the same computer and modify some variable value. Process is a native one, and the application is written in c++. Do you know some good tutorials that help to achieve this?
I think you are looking for WriteProcessMemory function, take a look on this
You can use shared memory for this but this is more of an advanced concept: How to implement shared memory in .NET?
You can have a look at other alternatives: Passing data between C++ (MFC) app and C#