3
I have a 3-thread application, this application involves a chemical process simulator, and I have to take some values of these processes. values come in a single object.
The 3 threads make an infinite loop, in this loop they take the values at different times, which cannot happen.
I wanted to know if there’s any way I can synchronize the threads, so when they go to pick up the object all 3 pick up at the same time.
Here are my threads:
public void RunController()
{
Softing.OPCToolbox.Client.Application app = Softing.OpcToolbox.Client.Application.Instance;
app.Initialize();
while(true)
{
scase.Solver.Integrator.IsRunning = true;
Thread.Sleep(1000);
scase.Solver.Integrator.IsRunning = false;
scase = Interaction.GetObject(opcform.pasta);
Contrl();
GC.Collect();
}
}
public void RunExport()
{
Softing.OPCToolbox.Client.Application app = Softing.OpcToolbox.Client.Application.Instance;
app.Initialize();
while(true)
{
scase.Solver.Integrator.IsRunning = true;
Thread.Sleep(1000);
scase.Solver.Integrator.IsRunning = false;
scase = Interaction.GetObject(opcform.pasta);
Exprt();
GC.Collect();
}
}
public void RunImport()
{
Softing.OPCToolbox.Client.Application app = Softing.OpcToolbox.Client.Application.Instance;
app.Initialize();
while(true)
{
scase.Solver.Integrator.IsRunning = true;
Thread.Sleep(1000);
scase.Solver.Integrator.IsRunning = false;
scase = Interaction.GetObject(opcform.pasta);
Imprt();
GC.Collect();
}
}
I’d like them to execute the scase = Interaction.GetObject(opcform.pasta);
at the same time.
What do you mean, "all three take at the same time"? You want to make a state machine that, with each iteration, the 3 use the same input?
– rodrigogq
exact! suggested that I create a thread and that it is in the infinite loop, it would take the object and send to the threads, that would be initiated and aborted at each turn of the infinite loop
– Jovita
Is this process too heavy? Will it run for a long time with each iteration? Or is it something more or less simple?
– rodrigogq
depends on the computer, the ones I’ve been using the longest thread takes about 20 seconds to give each turn
– Jovita
Is there any reason to call
GC.Collect()
? You know you’re only supposed to call him if you know what he’s doing and you have a very strong reason. In general not applied in codes in production and even more in loops.– Maniero
I was warned this and I put a condition not to do it every time, what was happening was a memory Leak and an access error in protected memory region, the
GC.Colect()
helped with both. If you have any material that talks more about this I will be very grateful– Jovita