Answer the question
In order to leave comments, you need to log in
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.
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
StatusBarProgress.Dispatcher.Invoke(DispatcherPriority.Normal, new Action(()=>StatusBarProgress.Value = e.ProgressPercentage))
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question