0

When I click the button, the cycle starts to read the database and send each row of the query to another server. When get response - the cycle continues.

Code is implemented as follows

private ManualResetEvent _mre = new ManualResetEvent(true);

and

Thread startUpload = new Thread(() =>
{
   //read database

   foreach (DataRow dr in dt.Rows)
      {

         //send request

         _mre.WaitOne();
      }
});

startUpload.Start();

The problem is that when requests are sent may not be the answer. In my case this is normal. But if not, the answer comes, then the cycle stops. I need to do inside the loop timer, which in the case of stopping the cycle due to lack of response will continue to cycle in 30 seconds.

The timer will have to do

_mre.Set();
Uwe Keim
  • 39,551
  • 56
  • 175
  • 291
alexander
  • 195
  • 3
  • 13
  • Where exactly is the problem? What are you trying to achive? Can you elaborate a little bit more? – keenthinker Jul 05 '15 at 14:32
  • @pasty for example when in foreach: timer = new System.Threading.Timer(new TimerCallback(DoSomething), null, 0, 30000); The loop is not waiting for an answer. Just squander – alexander Jul 05 '15 at 14:35

1 Answers1

2

IMPORTANT: You're setting your ManualResetEvent initial state to true, set it to false if you're willing to stop current process and wait for signal.

EDIT:

Example

private ManualResetEvent _mre = new ManualResetEvent(false);

private void ReadTheDatabase()
{
    Thread startUpload = new Thread(() =>
    {
        // Read the data

        foreach (DataRow dr in dt.Rows)
        {
            // Send request
            Thread requestMethod = new Thread(() =>
            {
                // Imitate process with thread sleep
                Thread.Sleep(5000);
                _mre.Set();
            });

            requestMethod.Start();

            _mre.WaitOne(30000, false);
        }
    });

    startUpload.Start();
}
msmolcic
  • 6,407
  • 8
  • 32
  • 56
  • Because now I have this algorithm: 1) Read the database, 2) I Bring in a temporary table, 3) Start a loop through the temporary table, 4) For each row in the query, 5) When the answer comes and updated database, we continue the cycle – alexander Jul 05 '15 at 15:02
  • Is it possible to manually continue the cycle with the help of AutoResetEvent ? – alexander Jul 05 '15 at 15:05
  • wait 30 seconds or manually continue – alexander Jul 05 '15 at 15:06
  • 1
    @alexander Yes it is, but you can do the same thing with ManualResetEvent. Keep in mind you need to change initial state to false. – msmolcic Jul 05 '15 at 15:06
  • 1
    @alexander In your code, when you're declaring and initializing ManualResetEvent, you need to pass 'false' in it's constructor. private ManualResetEvent _mre = new ManualResetEvent(false); // <-- here, otherwise code will just move on without waiting for it. – msmolcic Jul 05 '15 at 15:10
  • @msmocic thanx, but how i can combine AutoResetEvent and ManualResetEvent ? – alexander Jul 05 '15 at 15:15
  • 1
    You don't need AutoResetEvent, it was just my initial thought. I'll edit my answer with an example. – msmolcic Jul 05 '15 at 15:16
  • Very simple solution! Thanx a lot, is what I needed!) – alexander Jul 05 '15 at 15:19
  • @alexander No problem, glad I could help. :) – msmolcic Jul 05 '15 at 15:21