Create Random User algorithm in c# [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 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);
}
}

Related

Easy and correct way to loop accounts [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 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);
}

C# - How add new items to list with parallel tasks [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 5 years ago.
Improve this question
Hi Im trying to get information from a large list of object. This is my code:
List<Foo> Information = new List<Foo>();
List<string> SubPaths = getPaths(); //about 10000
var SplittedList = splitList(SubPaths, 100); //100 list of 100 (100*100=10000)
foreach(var list in SplittedList)
{
List<Foo> ProcessedList = DoSomethingWithParallelTasks(list);
Information.AddRange(ProcessedList); //when one of all task finished
}
Int number = Information.Count()//test: number = 10000
How can i get the information from multiple list and add this information to one list.
If i process the list in the normal way It takes more than an hour. #Mark Baijens
List<Foo> Information = new List<Foo>();
List<string> SubPaths = getPaths(); //about 10000
foreach(var path in SubPaths ) {
Information.Add(DoSomethingWithoutParallelTask(path);
}
It takes more than an hour
Thanks.
I think you want to do something like this. I don't know that much about multi treading so i recommend to do more research on it before implementing in a live environment since multi treading can lead into unreliable code if programmed in the wrong way.
Parallel.ForEach(SubPaths, (currentPath) => {
Information.Add(DoSomethingWithoutParallelTask(currentPath);
});

Async await foreach issue [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
private async void btnLoadFile_Click(object sender, EventArgs e)
{
if(AccountsFile.ShowDialog()==DialogResult.OK)
{
Accounts = File.ReadAllLines(AccountsFile.FileName);
foreach(string str in Accounts)
{
await LoadAccount(str);
}
}
}
I've ran into a problem, I know how Asynchronous programming works it will wait for the task to be complete but LoadAccount() will never complete because it calls a function with a never ending while loop so it will not reach the next string in Accounts.
I don't know where to start with this problem. Any solutions?
Instead of waiting for each account successively, you could wait for them collectively. This way, even if one of your accounts enters an infinite loop, the others could still proceed to load.
Accounts = File.ReadAllLines(AccountsFile.FileName);
Task completionTask = Task.WhenAll(Accounts.Select(LoadAccount));
You would typically want to store completionTask in a class variable. Subsequently, when you break out of the indefinite while loop within your LoadAccount calls (for example, by signalling cancellation via a polled CancellationToken), you can use this completionTask to wait for all your tasks to complete.

Best way to do a multithread foreach loop [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 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.

Parallel processing [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 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.

Categories