Hello, Can you please tell me I have a visual c # program

On the form there is a datagridview which displays information and a button for updating this information from the database.

If I put the function that updates the grid into a separate thread in the BackgroundWorker_DoWork event (object sender, DoWorkEventArgs e) and when I click the button, I call BackgroundWorker_DoWork.RunWorkerAsync () then the grid is wonderfully updated and everything goes well.

But if I start BackgroundWorker_DoWork.RunWorkerAsync () from a timer that works periodically, then the function that starts the update of the grid works, but starts to swear that I refer to the datagridview from the stream in which it does not exist.

And at the same time, if you run the update grid function directly from the timer, without using BackgroundWorker, then everything goes well too.

Why when launching another stream from the timer, the elements of the application form are not visible in it, and if you start from the button, everything can be seen perfectly?

    2 answers 2

    Isn't it easier to use the event model? Those. - the method that reads the data informs the main thread that the data has been read and transmits it, and already the main thread implements their mapping?

      I think you are using System.Threading.Timer instead of System.Windows.Forms.Timer.

      The first timer works in its thread, the second pushes everything into the user interface thread. And when you run a task, BackgroundWorker binds to the synchronization context of the thread in which the task was launched.

      • I used just System.Windows.Forms.Timer. This problem was solved by setting the loop with the condition of exit by variable. If the flow is initiated during the creation of the form and the cycle is working inside, everything turns out as the doctor has prescribed. - BoogVAr