D
D
DrFreez2015-06-06 01:04:53
C++ / C#
DrFreez, 2015-06-06 01:04:53

Why can't C# access a ToolStripProgressBar from a thread?

Map.Paint draws N images. The progress of this operation should be displayed. During each iteration, an event is fired in Map.Paint . I hang a handler on it that calls BW.ReportProgress() and get an exception:

System.Windows.Forms Illegal operation across multiple threads: An attempt was made to access control 'Form1' from a thread other than the thread it was created on.

If BW.ReportProgress() is called directly from BW_DoWork() , then everything is OK. How to be?
PS. For some reason, the problem is only with the ToolStripProgressBar , but in the ToolStripStatusLabel the text is calmly placed
private void BW_DoWork (object sender, DoWorkEventArgs e) {
            string ImgSavePath = SavePath + "\\" + DB.TimeStart.ToString("yyyy-MM-dd HH-mm");

            Mapper Map = new Mapper(TimeStart, TimeEnd);
            Map.SaveImageEventHendler += new MapperEventHandler(OnSaveImageHandler);
            Map.Paint(ImgSavePath);
        }

        private void OnSaveImageHandler (object sender, MapperEvent e) {
            StatusBarMessage.Text = "Create map for " + e.Time.ToString("yyyy-MM-dd HH:mm");
            BW.ReportProgress(e.QueueValue * e.QueueSize / 100);
        }

        private void BW_ProgressChanged (object sender, ProgressChangedEventArgs e) {
            StatusBarProgress.Value = e.ProgressPercentage;
        }

Answer the question

In order to leave comments, you need to log in

2 answer(s)
R
Roman, 2015-06-06
@yarosroman

StatusBarProgress.Dispatcher.Invoke(DispatcherPriority.Normal, new Action(()=>StatusBarProgress.Value = e.ProgressPercentage))

A
AG15, 2015-06-06
@AG15

stackoverflow.com/questions/661561/how-to-update-t...

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question