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 9 years ago.
Improve this question
I have a list of sites for which log files are generated. These logs have to be robocopied, unzipped, parsed and analysed with ruby code by running respective processes.
Can anybody suggest the best way to run these processes in parallel for all the site's logs?
Considering your data model like this:
class Website
{
public List<WebSiteLog> Logs;
}
A possible parallel solution using TPL (Task Parallel Library) is something like this:
// var sites = your sites list
var processTask = Task.Foreach(sites, site =>
{
Task.Factory.StartNew(theSite=>
{
theSite.UnzipLogs()
}.ContinueWith(unzipTask=>{
{
theSite.ParseLogs();
}.ContinueWith(parseTask=>{
{
theSite.AnalyzeLogs();
}
});
Task.WaitAll(processTask);
This is a very initial solution. Lots of exception management, partitioning and even more paralellizing on UnzipLogs, ParseLogs, AnalyzeLogs are applicable.
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 12 months ago.
Improve this question
I am new to xamarin and swift and I am creating a demo and in that, I have to write down some iOS related code which is mentioned below in code but the code is swift and I want to add that code in my .cs file so that I can use it with my UI page and it is giving errors as mentioned in the below image. It would be great if you can guide me with this and provide me with what code should I need to write and where.
UIApplication.shared.open(redirectUrl, options: [:], completionHandler: { success in
if success {
// Handle success.
} else {
// Handle failure. Most likely app is not present on the user's device. You can redirect to App Store using these links:
}
})
This should work for you. My advise, please refer to the official documentation it's all there.
var param = new NSDictionary();
UIApplication.SharedApplication.OpenUrl(new NSUrl("https://google.com"), param, (completed) =>
{
if (completed)
{
}
else
{
}
});
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 2 years ago.
Improve this question
I have 3 foreach function's, writing the list l of type / class Person to Console
foreach(Person p in l)
{
Console.WriteLine(p.first);
Console.WriteLine(p.last);
}
l.ForEach(p =>
{
Console.WriteLine(p.first);
Console.WriteLine(p.last);
});
Parallel.ForEach(l, p =>
{
Console.WriteLine(p.first);
Console.WriteLine(p.last);
});
They are all doing the same except that Parallel is working with threading i guess.
What should i use when?
Here you are using a method that is defined on the List<> class:
l.ForEach(p => { ... });
Internally this uses a for loop to iterate the collection. Use it when you want shorter and more readable code.
A standard foreach provides a bit more control over the loop e.g. breaking out of the loop when a specific condition is met. If want to read more about this just have a look at this article which provides in-depth information about this kind of topic.
Compared to the standard foreach loop, the Parallel.ForEach loop executes in a parallel manner. It uses multiple threads, as you already suggested. Use this when you want to execute multiple processes concurrently. Read more about this here.
Parallel.ForEach(l, p => { ... });
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 creating a bot for a website where the system logs in with several accounts and does a certain action the problem is when it is time to repeat the process example I have 10 accounts and I would like all accounts to do the same process as the first, the accounts are browned in a txt file. What is the correct way to do this?
Sometimes the function runs ahead of time, I'm new to C # I'm studying
My code looping :/
Task.Delay(2000).ContinueWith(t => setMail());
Task.Delay(3500).ContinueWith(t => nextButton());
Task.Delay(5000).ContinueWith(t => setPass());
Task.Delay(6500).ContinueWith(t => logionButton());
Task.Delay(7500).ContinueWith(t => SucessLogin());
You are creating 5 independent tasks which will all run together, rather than one after the other. Instead, just collapse them to one async/await function:
async Task TestStuff(Account account)
{
await Task.Delay(2000);
setMail();
await Task.Delay(3500);
nextButton();
await Task.Delay(5000);
setPass();
await Task.Delay(6500)
logionButton();
await Task.Delay(7500);
SucessLogin();
}
You mentioned that you've got accounts stored in a file. In this example you'll need to create an Account class and populate it with the information you get from the file. For example:
List<Account> accounts = LoadAccounts("some-file.txt")
foreach(var account in accounts)
{
await TestStuff(account);
}
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 want to create users based on the number of photos in a folder.
for example:
user.1 random(4)[photos1-4]
dosomething(user.1)
user.2 random(6)[photos5-10]
dosomething(user.2)
user.3 random(3)[photos11-13]
dosomething(user.3)
user.last [photos.leftover]
dosomething(user.last)
ideas on how to do this?
The best way to do this is load your list of work, randomize the list, then put the list in to a queue of some form to be pulled out by the end workers.
private BlockingCollection<string> GetWorkSoruce()
{
List<string> sourceList = GetListOfFiles(); //How you get your list is up to you.
Shuffle(sourceList); //see http://stackoverflow.com/questions/273313/randomize-a-listt-in-c-sharp
//Create a thread safe queue that many consumers can pull from.
var collection = BlockingCollection<string>(new ConcurrentQueue<string>(sourceList));
collection.CompleteAdding();
return collection;
}
Now each of your workers (Users) can pull out of the queue and be given work to do. Because the queue is a ConcurrentQueue you can have many workers from many threads all working at the same time.
private void WorkerDoWork(BlockingCollection<string> workSource, int itemsToTake)
{
foreach(var imagePath in workSource.GetConsumingEnumerable().Take(itemsToTake))
{
ProcessImage(imagePath);
}
}
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 9 years ago.
Improve this question
I have a send email method with a foreach, like this:
static void Main(string[] args)
{
foreach(var user in GetAllUsers())
{
SendMail(user.Email);
}
}
I need to improve that method. Using a multithread, because i dont want to wait the SendMail method executes each time for each user.
Any sugestions to do that?
Thanks
Try using a parallel foreach. I.e.
Parallel.ForEach(GetAllUsers(), user=>
{
SendMail(user.Email);
});
You can try something like this
private void Send()
{
Parallel.Foreach(GetAllUsers(), user =>
{
SendMail(user.Email);
});
}
I think the easiest way to do this would be thread pooling. .Net makes this pretty easy and you can read more about it here.