Home > Net >  Trouble you see the following, how
Trouble you see the following, how

Time:09-21

The following requirements
There are four tasks (a, b, c, d), and some may contain inside each task start the Timer to perform other tasks, and then the next task must wait after finishing a task to perform,
Such as the currently executing a, b in there will be a timer starts after 5 seconds to perform an action, there may be a timer in the action, execution, after the execution to b, b
May also have a timer, execution of the ability to execute the c, and the possibility of execution for a, b, c or a, c, d can also be a, b, d,

CodePudding user response:

Add 4 not waiting thread?

CodePudding user response:

Can use the thread, the Join

CodePudding user response:

refer to the second floor Amon Amon response:
can use threads, Join

Can the specific tell me

CodePudding user response:

1, a, b, c, d four tasks should be executed sequentially, can be in a call, call c b, c in call d,
2, define a logo flag, such as a start in the timer1, logo flag++ timer1 perform logo flag - the end of the
3, start in the timer1 timer2 flag++ logo timer2 perform logo flag - the end of the
4, while (true) {
If (0!=flag) continue;
break;
}
b();

CodePudding user response:

If each task is unitary, need not use timer, directly with threads,
Such as a, b, c, d four thread
A.s tart ();
A. oin ();
B.s trart ();
B.j oin ();
C.s. tart ();
SAN Antonio oin ();
D.s tart ();
D.j oin ();
If each task is complex, there are also other action, suggest you use the switch case + timer, so are the benefits of the UI BuKa,

CodePudding user response:

1. If you want to achieve thread synchronization at the same time, have interaction between threads, will use synchronous event,
There are two kinds of synchronization events: AutoResetEvent and ManualResetEvent,
They only different place is in the active threads, state is automatically changed from termination to the end,
2. In case of a thread is not recommended to use the timer,
Especially if non-reentrant timer, can cause many problems
Using a thread pool or abstract the basic task of higher is better

CodePudding user response:

I didn't understand.

such as the currently executing a, b in there will be a timer starts after 5 seconds to perform an action, there may be a timer in the action, execution, after the execution to b, b
May also have a timer, execution of the ability to execute the c, and the possibility of execution for a, b, c or a, c, d can also be a, b, d,


Now that, after a to b, b performed, can do c, then behind several situation is how to?

CodePudding user response:

If the order is fixed, such as a, b, c
So, I think it is the task directly. Wait,

CodePudding user response:

To build a class: a thread, a busy idle symbol (Boolean)

Create a class of arrays, thread start, mark; End of idle,
New tasks to find idle thread to perform the tasks in the array,

I did a small tool, taken from the database, task, assigned to a thread, each thread can handle one or more tasks,



Possible answer to your question,

CodePudding user response:

references on 7th floor is wrath of god reply:
I didn't understand,

such as the currently executing a, b in there will be a timer starts after 5 seconds to perform an action, there may be a timer in the action, execution, after the execution to b, b
May also have a timer, execution of the ability to execute the c, and the possibility of execution for a, b, c or a, c, d can also be a, b, d,


Now that, after a to b, b performed, can do c, then behind several situation is how to?

I this is a Console application, the reason can be a, b, c, d can also be a, c, d, and so on is determined by the user input command line parameters,
Such as the
The console. Exe a b c
The console. Exe a b c d
The console. Exe b c d
The console. Exe d c a b

CodePudding user response:

refer to the eighth floor is nu month god reply:
if the order is fixed, such as a, b, c
So, I think it is the task directly. Wait,

Not fixed order

CodePudding user response:

Use a thread pool,
Do a good job in a queue, the first to join the thread pool, performed, and the first out and last in, it's that simple,
If you had not task, a task put queue waiting for 1 second,
Need to perform the abcd one of the tasks, in queue.

The only thing you want to do, is to maintain the queue,

CodePudding user response:

11 references, Ming edge response:
Quote: refer to the eighth floor is nu month god reply:

If the order is fixed, such as a, b, c
So, I think it is the task directly. Wait,

Not fixed order

Well, then you can use # 12's advice, it is ok to use a queue,

CodePudding user response:

Abstract in software development is very powerful,

If a task can be abstracted as a ITask, no matter what the internal implementation, whether to use the Timer, a ITask must be able to start, return mission assurance over,
The task queue is very simple?

Public interface ITask
{
String TaskName {get; }
Task RunAsync ();
}

Assuming that the class A, B, C implementation ITask, program can be written as:
 class Program 
{
The static void Main (string [] args)
{
RunTasksAsync (" a ", "b", "c", "a"), Wait ();//RunTasksAsync (args). Wait ();
Console.ReadLine();
}
Async static Task RunTasksAsync (params string [] taskKeys)
{
Var knownTasks=new ITask [] {new (), A new B (), the new C ()}, ToDictionary (x=& gt; X.T askName);
The foreach (var taskKey taskKeys) in
{
If (knownTasks. TryGetValue (taskKey, out var task))
{
Console. WriteLine ($" Start & gt;> {task. TaskName} ");
Await task. RunAsync ();
Console. WriteLine ($" Done & lt; <{task. TaskName} \ r \ n ");
}
}
}
}

CodePudding user response:

Look from CSDN, means to get to the next next thread, must wait for after the end of a thread to execute,

Join is a kind of block the calling thread (that is, call the method of thread), until the thread synchronization method its Join call has been completed, use this method to ensure that the thread has terminated, if the thread was not terminated, the call will block indefinitely, in the example below, the Thread1 thread calls the Join () method Thread2, and this will lead to Thread1 to block, until Thread2 completed,


using System;
using System.Threading;

Public class Example
{
The static Thread mainThread, thread1 thread2;

Public static void Main ()
{
MainThread=Thread. CurrentThread;
Thread1=new Thread (ThreadProc);
Thread1. Name="thread1";
Thread1. Start ();

Thread2=new Thread (ThreadProc);
nullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnull
  •  Tags:  
  • C#