For example, this code fragment performs 5,000 thousand iterations (with the interval frc = 0 to frc1 = 50)

for (double fr = frc; fr <= frc1; fr += shag) { double resulting = (1 / (2 * Math.PI)) * integration.Calculate(angularFrequency => GetSpectralDensityOfAmplitude(GetSpectralDensity(U, angularFrequency, T)) * Math.Cos(angularFrequency * t), 0, frc1); list1.Add(fr,resulting); } 

Naturally, the program hangs for 2-3 minutes, but it gives the result, but still. It happens all 10 minutes freezes (it depends on how much I enter the interval)

And I would like this to not hang. My first attempts to throw a method into the stream were unsuccessful.

  Thread thd = new Thread(drawgraph(U,T,frc,frc1,frequency,shag)); thd.Start(); thd.Join(); 

In theory, it is written that the method to be called in the stream must be without arguments and parameters, must be of type void and not return anything

The problem is that my method is where this void is but there are parameters in it, it’s impossible without them. How can I proceed in my case?

2 procedures are performed in a total of 1 minutes 10 seconds (if you enter the minimum parameters of type 0 and 50)

If you enter large parameters for example -100 to 100, the program freezes for 5 minutes.

Most of the theory that I have read offer work with methods of the void type, but there are no parameters there, and in mine there are nowhere without them

And therefore it is interesting, is it possible in some way to put a method with parameters into a stream?

  • Join waits for the completion of the thread, so it is logical that nothing will change from such a record. - αλεχολυτ
  • You have already used closures for calculations - what's stopping you from doing the same for the flow? .. - Pavel Mayorov
  • @alexout I will fix this, but what about the method that has parameters? - beginner
  • @PavelMayorov in this case does not help, for the reason that I have 2 schedules read and a total of several thousand iterations are being made - beginner
  • @beginner you carry nonsense. How does the number of iterations prevent you from creating a thread? - Pavel Mayorov

1 answer 1

See it.

You must separate the calculations from the UI code. The UI code must be executed in the main thread, but the calculations can be easily downloaded to the background thread.

Then, using streams directly is not the best idea. Currently, it is much better to use async / await.

In this case, your code will be as follows (conditionally):

 var data = await Task.Run(() => ComputeData(parameters)); DisplayData(data); 

Methods, of course, do not have to be called so.

Well, your procedure, inside which you use this code, should be an async procedure. (I think it makes sense for you to read about it, either here on the site or on the Internet. There is a lot of information about async / await.)


Now two more words about why the code

 Thread thd = new Thread(drawgraph(U,T,frc,frc1,frequency,shag)); thd.Start(); thd.Join(); 

did not work.

First, in the Thread constructor, you need to pass a function (or delegate) without parameters. A drawgraph(U,T,frc,frc1,frequency,shag) is not a function, it is a value that is the result of a function call. To fight this is simple:

 () => drawgraph(U,T,frc,frc1,frequency,shag) 

It is a lambda that “packs” the necessary calculation into a (delegate) function without parameters.

Secondly (and this is more serious), the problem is with thd.Join() . At this point, your main thread will simply block and wait until the background thread runs. That is, it does not bring any benefits compared to executing code in the same thread: all the same, the stream is blocked for all the time of the calculations! The solution with await has no problems at this point.