Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C# throttling For loop

Initial Situation

I'm developing a .NET Framework 4.0, C#, Winform Application. The Application will list (and test) WebServiceOperations in a GridView (with currently 60 DataRows => WebServiceOperations).

Objective

I have to test/call all of this operations with one click on a button. Every operation creates a new instance of a class. Within this class, i call the WebServiceOperation async and wait for the result. The result is then beeing validated. The whole code works smooth using delegates and events.

Now it comes to the challenge/question: When clicking on that button, i use a for loop (int i = 0; i < gridViewWsOperations.RowCount; i++) => with other words, currently i'm firing 60 operations at them 'same time' => the Server gets overloaded processing 60 requests at the same time and i get timeouts. So i need to somehow throttle the number of concurrend requests to let's say 10 at the same time. Consider, the for loop (where i have to enqueue the requests) isn't in the same thread as the method (process_result event) where i dequeue the requests. I tried it using ConcurrentQueue since this type of collection seems to be thread-safe.

Links

ConcurrentQueue at MSDN

a sample code would really help!

--- this is my solution/sample code ---

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections.Concurrent;
using System.Threading;

namespace ConcurrentQueueSample
{
    class Program
    {
        static SemaphoreSlim semaphoreSlim = new SemaphoreSlim(3);

        static void Main(string[] args)
        {
            System.Timers.Timer timer = new System.Timers.Timer();
            timer.Elapsed += new System.Timers.ElapsedEventHandler(timer_Elapsed);
            timer.Interval = 1234;
            timer.Enabled = true;
            timer.Start();

            for (int i = 0; i < 10; i++) new Thread(go).Start(i);
        }

        static void timer_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
        {
            semaphoreSlim.Release();
        }

        static void go(object i)
        {
            Console.WriteLine("id: {0}", i);
            semaphoreSlim.Wait();
            Console.WriteLine("id: {0} is in", i);
            Thread.Sleep(1250);
            Console.WriteLine("id: {0} left!", i);
        }
    }
}
like image 451
Christian Casutt Avatar asked Jan 23 '13 16:01

Christian Casutt


People also ask

What C is used for?

C programming language is a machine-independent programming language that is mainly used to create many types of applications and operating systems such as Windows, and other complicated programs such as the Oracle database, Git, Python interpreter, and games and is considered a programming foundation in the process of ...

What is C in C language?

What is C? C is a general-purpose programming language created by Dennis Ritchie at the Bell Laboratories in 1972. It is a very popular language, despite being old. C is strongly associated with UNIX, as it was developed to write the UNIX operating system.

What is the full name of C?

In the real sense it has no meaning or full form. It was developed by Dennis Ritchie and Ken Thompson at AT&T bell Lab. First, they used to call it as B language then later they made some improvement into it and renamed it as C and its superscript as C++ which was invented by Dr.

Is C language easy?

Compared to other languages—like Java, PHP, or C#—C is a relatively simple language to learn for anyone just starting to learn computer programming because of its limited number of keywords.


1 Answers

You may want to take a look at using the SemaphoreSlim class to throttle the threads that have access to your queue.

like image 129
Kwal Avatar answered Sep 18 '22 00:09

Kwal