Note that there are some explanatory texts on larger screens.

plurals
  1. POWant to set the UI threads in a progressbar in C# when I import data into a database
    primarykey
    data
    text
    <p>I've got a method called Import that imports the data of an xml file into the database. It looks like this:</p> <pre><code>private void SavingXMLFile() { //Save the file: if (rootElement != null) { try { using (FileStream fsHosp = new FileStream("Data/ConfigOrgHospital.xml", FileMode.Truncate, FileAccess.Write)) { using (XmlWriter x = XmlWriter.Create(fsHosp)) { SerializeHospitalData(x, rootElement); } } } catch (Exception e) { MessageBox.Show(e.Message); } try { Import("Hospitals"); } catch (Exception e) { MessageBox.Show(e.Message); } } </code></pre> <p>Then it goes to the import method:</p> <pre><code>public void Import(string sourceFile) { IProgressReporter progressReporter = new ProgressReporter(); BackgroundThreading.RunInBackground&lt;int&gt;((object input) =&gt; { foreach (IConfigRunner runner in Runners.OrderBy(r =&gt; r.Priority)) { runner.DoImport(progressReporter, sourceFile); } return 0; }, (answer, error, w) =&gt; { if (error != null) { //ShowError(error); } else { //AddProgress("Export ready"); } //DoReady(); }, (error) =&gt; { //ShowError(error); }); } </code></pre> <p>Then it does a DoImport:</p> <pre><code>public interface IConfigRunner { int Priority { get; } void DoExport(IProgressReporter progress); void DoImport(IProgressReporter progress, string filename); } [Export] public void DoImport(IProgressReporter progress, string filename = null) { if (filename.Equals("Hospitals")) { if (File.Exists(HOSPITALFILE)) { progress.AddProgress("Importing " + HOSPITALFILE); using (OrgEntities orgEntityModel = ModelFactory.GetOrgEntities()) { Tools.ValidateXml(HOSPITALFILE, "Xsd\\hospital.xsd"); XmlSerializer inSerializer = new XmlSerializer(typeof(HospitalRoot)); TextReader reader = new StreamReader(HOSPITALFILE); HospitalRoot root = (HospitalRoot)inSerializer.Deserialize(reader); reader.Close(); try { OrgHospitalXml.Insert(orgEntityModel, root.Hospitals); } catch (ImportException e) { progress.AddProgress(e.Message + ": " + e.Item); throw e; } } } } </code></pre> <p>Is there any way that I can show the progression of this in a progressbar? Or how to find all the UI Threads? Thx in advance</p> <p>BackgroundWorker:</p> <pre><code>public static class BackgroundThreading { public static BackgroundWorker RunInBackground&lt;T&gt;(object param, Func&lt;object, T&gt; call, Action&lt;T, Exception, BackgroundWorker&gt; callBack, Action&lt;Exception&gt; errorHandler = null) where T : new() { BackgroundWorker worker = new BackgroundWorker(); DoWorkEventHandler workHandler = null; RunWorkerCompletedEventHandler completeHandler = null; workHandler = delegate(object s, DoWorkEventArgs args) { args.Result = call(args.Argument); args.Cancel = worker.CancellationPending; }; completeHandler = delegate(object s, RunWorkerCompletedEventArgs args) { if (!args.Cancelled) { if (args.Error != null) { if (!(args.Error is FaultException)) { T result = new T(); callBack(result, args.Error, (BackgroundWorker)s); } else { if (errorHandler == null) { string message; if (args.Error.InnerException != null) { message = args.Error.InnerException.Message; } else { message = args.Error.Message; } Logger.LogError("SVC", Logger.SVC_ERROR_001, new object[1] { message }); throw args.Error; } else { errorHandler(args.Error); } } } else { callBack((T)args.Result, null, (BackgroundWorker)s); } ((BackgroundWorker)s).DoWork -= workHandler; ((BackgroundWorker)s).RunWorkerCompleted -= completeHandler; } }; worker.DoWork += workHandler; worker.RunWorkerCompleted += completeHandler; worker.WorkerSupportsCancellation = true; worker.RunWorkerAsync(param); return worker; } public static BackgroundWorker RunInBackground&lt;T&gt;(Func&lt;object, T&gt; call, Action&lt;T, Exception, BackgroundWorker&gt; callBack, Action&lt;Exception&gt; errorHandler = null) where T : new() { return RunInBackground&lt;T&gt;(null, call, callBack, errorHandler); } </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload