Home Upgrade Search Memberlist Extras Hacker Tools Award Goals Help Wiki Contact

C# 

Parallelize HttpWebRequest [Really Hard C#]

Hi there,

What I'm trying to do:
  • I log into a bunch of accounts (instances of Account.cs) on the program.

  • Each account is set a TXT of people (realIds) to message.

  • They all do it at the same time.



Account.cs:
[Image: 2RAXayO.png]

myId is my own ID (not needed in send message method).
authToken is used to login (not needed in send message method).

HttpMethods.cs:
[Image: UI9LhmK.png]

realId above is the ID of a person to message.




If anyone can solve this one for me, I'm even willing to reward. This shit is frying my brain.  🤝
(Jul 4, 2018 11:39 AM)FryPan Wrote:  If anyone can solve this one for me, I'm even willing to reward. This shit is frying my brain.  🤝

Solve what exactly? You did not specify the problem that you are having?
https://github.com/Washi1337

"I'll tell you this UDP joke, and I don't care if you will get it or not."
Read up on Multi Threading (example)
(Jul 4, 2018 11:45 AM)Charger Wrote:  Read up on Multi Threading (example)

Yes, I know about multi threading, but I'm asking on help to set it up:

IE: Parallel.For the account instances, and then normal loop through the TXT realIDs?

(Jul 4, 2018 11:43 AM)Washi Wrote:  Solve what exactly? You did not specify the problem that you are having?

Sorry for not making it clear. I'm trying to:
  • Import a bunch of accounts ( Account acc = new Account("user", "pass") )

  • Assign a txt of realIDs/Accounts to message (contains 1000 users)

  • They all do it at the same time.


(Jul 4, 2018 11:46 AM)FryPan Wrote:  Yes, I know about multi threading, but I'm asking on help to set it up:

IE: Parallel.For the account instances, and then normal loop through the TXT realIDs?


Sorry for not making it clear. I'm trying to:
  • Import a bunch of accounts ( Account acc = new Account("user", "pass") )

  • Assign a txt of realIDs/Accounts to message (contains 1000 users)

  • They all do it at the same time.


You can't fire 1000 requests at the same time. Your computer can't handle it nor can the server.
What you are trying to do is what multi threading does.
Also, directly supplying the ‘message’ into your preset JSON will break if the message is not in proper format.

I would also add some sort of response checking to make sure the server returned that the message was sent successfully.
шкідливе ПО для ентузіастів

Jabber: [email protected]
[email protected]

Multithreading is exactly what you need.

I used the threadpool for a similar purpose.

https://www.dotnetperls.com/threadpool


Code
for (int i = 0; i < _maxThreads; i++)
{
    ThreadPool.QueueUserWorkItem(new WaitCallback(Worker), i);
}


private void Worker(object state)
{
    // call your method that does the HTTP requests
}
(Jul 4, 2018 01:42 PM)Tommy Chong- Wrote:  Multithreading is exactly what you need.

I used the threadpool for a similar purpose.

https://www.dotnetperls.com/threadpool


Code
for (int i = 0; i < _maxThreads; i++)
{
   ThreadPool.QueueUserWorkItem(new WaitCallback(Worker), i);
}


private void Worker(object state)
{
   // call your method that does the HTTP requests
}

Thanks for the only message on topic. Any chance you know of an example that is similar but uses tasks?

(Jul 4, 2018 11:49 AM)Charger Wrote:  You can't fire 1000 requests at the same time. Your computer can't handle it nor can the server.
What you are trying to do is what multi threading does.

You read the thread wrong
(Jul 4, 2018 02:01 PM)FryPan Wrote:  Thanks for the only message on topic. Any chance you know of an example that is similar but uses tasks?


You read the thread wrong

Is there any reason why you want to use tasks over threadpool?
More control over a task than a thread pool.

Thread pool is like a magic invisible wall kinda like a glory hole where you just shove shit in and you see what happens.

Tasks allows more control over this shit and is non-glory hole version.

A good read is here for OP wanting to use tasks:

docs.microsoft.com/en-us/dotnet/standard/parallel-programming/task-based-asynchronous-programming
(Jul 5, 2018 03:44 AM)alexconnolly Wrote:  More control over a task than a thread pool.

Thread pool is like a magic invisible wall kinda like a glory hole where you just shove shit in and you see what happens.

Tasks allows more control over this shit and is non-glory hole version.

A good read is here for OP wanting to use tasks:

docs.microsoft.com/en-us/dotnet/standard/parallel-programming/task-based-asynchronous-programming

Thanks bud. Nice analogy lmao.
(Jul 5, 2018 03:44 AM)alexconnolly Wrote:  More control over a task than a thread pool.

Thread pool is like a magic invisible wall kinda like a glory hole where you just shove shit in and you see what happens.

Tasks allows more control over this shit and is non-glory hole version.

A good read is here for OP wanting to use tasks:

docs.microsoft.com/en-us/dotnet/standard/parallel-programming/task-based-asynchronous-programming

You are right about this, but for the simple tasks OP need to perform, the threadpool should be sufficient enough
(Jul 5, 2018 10:10 AM)Tommy Chong- Wrote:  You are right about this, but for the simple tasks OP need to perform, the threadpool should be sufficient enough

I'm going to be using Parallel.For & then a normal loop in that one.
Definitely tasks, people mentioning threadpool should get lynched.
Tasks provides more control, way easier debugging and cleaner code


EDIT: If you use async that is, if you don't use async might aswell use backgroundworker lol
You can also look up SemaphoreSlim. It allows you to control the maximum amount of tasks you can run in parallel.


Possibly Related Threads...
Thread / Author Replies Views Last Post
[C#] by HardStoNMay 2, 2021 12:59 AM 5 269 May 2, 2021 12:59 AM
Last Post: vaiperboy
[C#] by draxyeeSep 10, 2019 01:37 PM 12 389 Sep 10, 2019 01:37 PM
Last Post: MavenGradle
[.NET] by freesixJan 19, 2019 02:14 AM 13 497 Jan 19, 2019 02:14 AM
Last Post: StarZ