Home > Net >  The circulation and thread way
The circulation and thread way

Time:11-15

I am to have such a requirement, the circulation perform two operations, these two operations is a timer timer, is a thread, need is to cycle through the two operations, should how to write this way? Using a for loop to perform two operations, it will not wait until the operation is performed, the cycle will continue to run, the great god give directions

CodePudding user response:

Didn't understand what you mean is performed an operation and then perform another operation?

CodePudding user response:

Two are directly winform timer is not good, also cycle?

CodePudding user response:

I am now so write
 
For (int I=0; i {
The Monitor. The Enter (obj);
Visitor_name=visitorList [I] f_name;
If (isRunNum==0)
{
//timer implementation
IdTimer. Enable=true;

If (readIDCardCompleted)
{
WeiDunFingerTool wdTool=new WeiDunFingerTool ();

WdTool. FeatureRegister=featureRegister;
WdTool. ShowMessage=ShowWndMessageMethod;
WdTool. SetParam=SetParamMethod;
WdTool. SetVisitorFingerInfo=setVisitorFingerInfoMethod;
WdTool. FeatureLen=featureLen;
if (! WdTool. OpenDevice ())
{
This. RtbFinger. Text="refers to open the vein equipment failure!" ;
return false;
}
If (0!=threadrunning)
{
This. RtbFinger. Text="registered or validation in the operation of the thread!" ;
return false;
}

Threadrunning +=1;
//thread execution
WdTool. RegisterThread ();
}
}

But this content is not performed every time, will continue the loop, I this is spelled wrong? How to write

CodePudding user response:

When we used to "future tense" programming, looking at the code is awkward,

Ok, here are not ready to accept the "future tense" programming, so we also don't say, so that a few "five years the couple want to spit again,"

It is obvious that you use thread is because you don't want to block, you use the timer is because you don't know what time the thread end,
If use "future tense" programming, it is simple a few words, because he can say "sometime in the future I'm ready or I failed"

As for you, I can only say, since the result is asynchronous, so direct on line by the asynchronous signal (remove the so-called timer, we have been tired all day and all night here, endless, that a few five years of the couple's mouth, the so-called standard answer -- -- -- -- -- -- -- what four timer how)

CodePudding user response:

In fact,
If (0!=threadrunning)
{
This. RtbFinger. Text="registered or validation in the operation of the thread!" ;
return false;
}

Threadrunning +=1;

You this code has been a "semaphore" shadow

CodePudding user response:

refer to 6th floor wanghui0380 response:

actuallyIf (0!=threadrunning)
{
This. RtbFinger. Text="registered or validation in the operation of the thread!" ;
return false;
}

Threadrunning +=1;

You this code has been a "semaphore" the shadow of the

Help to write a small example, thank you

CodePudding user response:

Feel your Timer in the wrong place, remove the Timer Thread. Directly Sleep have the effect of timing, or the Thread, the join (), ManualResetEvent waiting Thread to return, if..net version more new words, direct use of a Task, call Wait method during the mission, waiting for the Task to complete,

CodePudding user response:

 private static readonly SemaphoreSlim slim=new SemaphoreSlim (1); 
Private async void button1_Click (object sender, EventArgs e)
{
For (int I=0; i <10; I++)
{

Await slim. WaitAsync ();
Trace. WriteLine (" preparation for entry into the new thread ");
RegisterThread ();
}
}

Void RegisterThread ()
{

Thread the Thread=new Thread (RegisterThread0);
Thread. The Start ();
}

Void RegisterThread0 ()
{

Thread.sleep (TimeSpan. FromMinutes (1));
Trace. WriteLine (" thread execution and release the semaphore ");
Slim. Release ();
}

Private void button2_Click (object sender, EventArgs e)
{
Trace. WriteLine (" test signal will stop, main process run ");
}


Is this your existing code directly asynchronous signal lock, looks very unpleasant, still, and because of the need to asynchronous, so I still use the await

The following me to stick in, "future tense" code directly, do you see more than the bizarre code look pleasing to the eye

CodePudding user response:

Private async void button1_Click (object sender, EventArgs e)
{
For (int I=0; i <10; I++)
{
Await RegisterThread ();

}
}

Task RegisterThread ()
{
The return of Task. The Run (()=& gt;
{
Trace. WriteLine (" into the thread ");
Thread.sleep (TimeSpan. FromMinutes (1));
Trace. WriteLine (" at the end of thread execution ");

});
}






Private void button2_Click (object sender, EventArgs e)
{
Trace. WriteLine (" test signal will stop, main process run ");
}

CodePudding user response:

references 9 f wanghui0380 response:
 private static readonly SemaphoreSlim slim=new SemaphoreSlim (1); 
Private async void button1_Click (object sender, EventArgs e)
{
For (int I=0; i <10; I++)
{

Await slim. WaitAsync ();
Trace. WriteLine (" preparation for entry into the new thread ");
RegisterThread ();
}
}

Void RegisterThread ()
{

Thread the Thread=new Thread (RegisterThread0);
Thread. The Start ();
}

Void RegisterThread0 ()
{

Thread.sleep (TimeSpan. FromMinutes (1));
Trace. WriteLine (" thread execution and release the semaphore ");
Slim. Release ();
}

Private void button2_Click (object sender, EventArgs e)
{
Trace. WriteLine (" test signal will stop, main process run ");
}


Is this your existing code directly asynchronous signal lock, looks very unpleasant, still, and because of the need to asynchronous, so I still use the await

The following me to stick in, "future tense" code directly, see if you look pleasing to the eye than the bizarre code much



Could you tell me how to test this code. The signal will stop , this code is how to understand, every time I press button2 has said semaphore didn't stop output

CodePudding user response:

nullnullnullnullnull
  •  Tags:  
  • C#
  • Related