Microsoft Solver Foundation multithreading in C #

I have a problem using Microsoft Solver Foundation in my multithreaded C # application:

I start two threads at the same time and in each thread, which calls the function as follows:

private void exampleFunction() { SolverContext context; Model model; for(int i=0;i<10;i++) { context = SolverContext.GetContext(); model = context.CreateModel(); //adding decisions, constraints and goal to model Solution sol = context.Solve(); string s = sol.Quality.ToString();//and here sometimes I get that sol is null! context.ClearModel(); } } 

It seems that the context that I have in each thread is the same object, so when I call

 context.ClearModel(); 

in thread1, then it is also cleared in thread2 (even if the problem in thread2 has not yet been resolved).

So the question is: is there a way to solve two models at the same time in MSF? Is it possible to declare two SolverContext that will not be the same?

+4
source share

Source: https://habr.com/ru/post/1502160/


All Articles