I am trying to achieve the display of an animated gif in a windows
app. I had hoped that by displaying it in a webbrowser contro that it would always be animated.
Currently it is only animated when the system is not busy however when the app is processing such as returning data the animation freezes. To prevent this wouldit be possible to get the webbrowser control to run in a different thread, and could this solve my problem?
Currently it is only animated when the system is not busy however when the app is processing such as returning data the animation freezes. To prevent this wouldit be possible to get the webbrowser control to run in a different thread, and could this solve my problem?
However I am getting hte error
Cross-thread operation not valid: Control 'uctrlBusyDisplay' accessed from a thread other than the thread it was created on.
The error seems to occur when I try and add the webbrowser control to the usercontrol.
C# |
this.Controls.Add(wb);
Code |
I have triebut still get the error as I dont really understand the method.
Also jumping ahead to when I have achieved this when I have added the control will the webbrowser still control be running in its own thread when it is added to the main form?

Sam HobbsRank #107 · 20.5K reputationPosted Sep 8, 2010, 10:45 PM EDT
James Wilcock14 reputationPosted Sep 8, 2010, 11:55 PM EDT
Maybe there is a better way to achieve this?
James Wilcock14 reputationPosted Sep 9, 2010, 6:21 AM EDT
In terms of threading do I need to kill the threads when the data has been returned? If the user presses the get data button numerous times is it risky to be spawning lots of new threads?
Sam HobbsRank #107 · 20.5K reputationPosted Sep 9, 2010, 6:41 AM EDT
I don't think you have specified whether the animation changes due to time or due to progress.
James Wilcock14 reputationPosted Sep 9, 2010, 6:58 AM EDT
I want the animation (the browser control) to be invisible. When the user presses the button to return some data I will make the animation visible.
I think at this point I will strat a new thread to call the getdata method
Once the getdata method has completed I will make the webbrowser control invisible again.
So I think I am looking at some thing like this.
void btnRefresh_Click(object sender, EventArgs e) { try { objReportAudit.AuditUpdate(); this.objCtrlFactors.ShowImage(); this.factors = objCtrlFactors.GetFactors(this.reportVersion); if (factors.IsValid) { this.objCtrlFactors.btnRefresh.Enabled = false; myThread = new Thread(new ThreadStart(this.GetData(factors);)); this.GetData(factors); objReportAudit.AuditUpdate(); } else { MessageBox.Show(factors.ValidationMessage, "Factors validation error", MessageBoxButtons.OK, MessageBoxIcon.Hand, MessageBoxDefaultButton.Button1); } } catch (Exception ex) { objReportAudit.AuditUpdate(ex.Message.ToString()); Error.ReportError(Properties.Settings.Default.Corp_RptConnectionString, objReportAudit.GetAuditID(), Globals.ThisWorkbook.Name, objReportAudit.GetUsername(), ex); } finally { this.objCtrlFactors.btnRefresh.Enabled = true; this.objCtrlFactors.HideImage(); } } public void GetData(Factors f) { Excel.Application excelApp = this.Application; excelApp.Cursor = Excel.XlMousePointer.xlWait; excelApp.StatusBar = "Executing database query"; Excel.Workbook newWorkbook; string newWorkbookName = ""; try { DAL.GetData(ds, f); Globals.ThisWorkbook.Activate(); excelApp.ScreenUpdating = false; newWorkbook = ReportRender.CopyToNewWorkbook(this.Application, Globals.ThisWorkbook.Sheets, Globals.ThisWorkbook.Name); this.WriteReportTitle((Excel.Worksheet)newWorkbook.Worksheets["Usage"]); ReportRender.GenericDrawRoutine((Excel.Worksheet)newWorkbook.Worksheets["Usage"], (DataTable)Globals.ThisWorkbook.ds.UP_CRSUsageAudit, columnMappings, functionMappings, "A8"); newWorkbookName = newWorkbook.Name; Marshal.ReleaseComObject(newWorkbook); newWorkbook = null; } finally { Thread.Sleep(2000); excelApp.Cursor = Excel.XlMousePointer.xlDefault; excelApp.ScreenUpdating = true; excelApp.StatusBar = "Ready"; Marshal.ReleaseComObject(excelApp); excelApp = null; GC.Collect(); GC.WaitForPendingFinalizers(); GC.Collect(); GC.WaitForPendingFinalizers(); } }James Wilcock14 reputationPosted Sep 9, 2010, 8:08 AM EDT
I think it is most likely because i am trying to pass a parameter through to the method im trying to invoke in the new thread, which it doesnt seem to like.
James Wilcock14 reputationPosted Sep 9, 2010, 8:14 AM EDT
Sam HobbsRank #107 · 20.5K reputationPosted Sep 9, 2010, 8:15 AM EDT
I think the ProgressBar is capable of showing animation; have you looked at that?
Also, you should use the BackgroundWorker class; it is designed to be easier to use.
Sam HobbsRank #107 · 20.5K reputationPosted Sep 9, 2010, 8:21 AM EDT
James Wilcock14 reputationPosted Sep 10, 2010, 12:15 AM EDT
Obj controlFactors is a UserControl which I have added to my form, it is the control upon which the animation is shown.
Anyway I guess I have digressed now from the animation problem and all I want to do now is execute my get data method (which takes a parameter) in a new thread.
Or perhaps using a progress bar I could use this
http://www.codeguru.com/columns/vb/article.php/c10755
If I could convert to C#
worker = new System.ComponentModel.BackgroundWorker(); worker.WorkerReportsProgress = true; worker.DoWork += new EventHandler DoWorkEventHandler(AddressOf OnWork) worker.ProgressChanged += new ProgressChangedEventHandler(AddressOf OnProgressChanged)Right now its not liking the delegate.
Sam HobbsRank #107 · 20.5K reputationPosted Sep 10, 2010, 1:20 PM EDT