Pausing all tasks when condition is met


hello.  i'm trying consume remote vendor service using 25 tasks.  the service crashes occasionally.  the vendor thinks issue when try access account locked service doesn't unlock account until logs condition , requesting lock before unlocks crashes.  so, vendor has requested pause tasks few seconds when receive success=false response.

reading cancellationtokensource.  i'm ok cancelling tasks if need i'm not sure how use it.  

assistance appreciated.

here code:

private static object lockobject = new object();

public semaphore sema { get; set; }

public overdraftdata getoverdraftitem()
{
    lock (lockobject)
    {
        return this.overdraftcollection.dequeue();
    }
}

public void domywork()
{
    this.sema= new semaphore(25,25);

    queue<overdraft> data = new queue<overdraft>();
    (decimal = 0; < 40000; i++)
    {
        data.enqueue(new overdraft { account = i, limit = });
    }

    list<task> tasks = new list<task>();
    do
    {
        this.sema.waitone();

        overdraftdata od= this.getoverdraftitem();

        tasks.add(task.factory.startnew(() => { makecall(od); }));
    }
    while (data.count > 0);

    task.waitall(tasks.toarray());
    this.sema.dispose();
}

public servicereponse makecall(overdraft od)
{
    try
    {
        servicereponse response= this.modifylimits(od);
        if( response.successful==false)
        {
             this.insertfailurereason(response.message);
        }        
    }
    finally
    {
        this.sema.release();
    }
 }

public serviceresponse modifylimits(overdraft od)
{    
    servicereponse response= servicecall1();
    if( response.successful==false)
        return response;
        
    response= servicecall2();
    if( response.successful==false)
        return response;

    return servicecall3();
}


i'm no on multi-threading, but:

this worrysome, if vendor **thinks** issue due something, and pause tasks **a few seconds** when something.

1st, **think** guess, expect rework **think** when previous **think** doesn't stand

2nd, **a few seconds**, define ~~few~~.

2 seconds? 10 seconds?

3rd, , i'm not sure u doing **cancelling** equivalent pause.



Visual Studio Languages  ,  .NET Framework  >  Visual C#



Comments

Popular posts from this blog

Azure DocumentDB Owner resource does not exist

job syspolicy_purge_history job fail in sqlserver 2008

Trying to register with public marketplace error with 'Get-AzureStackStampInformation'