Friday 15 August 2014

c# - Calling multiple methods asynchronously -


Is there any way to call many methods at the same time? I have a Winform app that loads an unreliable amount of data on loading:

  Private Zero Form 1_load (Object Sender, EventEurge E) {LoadValues1 (); LoadValues2 (); LoadValues3 (); LoadValues4 (); LoadValues5 (); LoadValues6 (); LoadValues7 (); LoadValues8 (); }   

These methods have been retrieved and DevExpress LookUpEdits (similar to Windows Dropdownlists) have been populated, so they all look like this:

  DBContext dbContext = new DBContext); ObservableCollection & LT; String & gt; Values1 = New Observational Collection & lt; String & gt; (I.Value1 i.Value1 for i.Value1! = Null & amp; i.Value1.Length & gt; order in dbContext.Items (from i). Undoubtedly ()); Lookup vouchers 1 Properties Datasource = Dismissed year; DevExpress.XtraEditors.Controls.LookUpColumnInfoCollectioncolInfo = Lookup Value1 Properties Column; ColInfo.Clear (); ColInfo.Add (New DevExpress.XtraEditors.Controls.LookUpColumnInfo ("Columns")); ColInfo [0]. Caption = "value1";   

And some of these methods take some time to complete, yet none of them depend on each other, so I thought I could do them at the same time :

  Tasks. Factor Start (-) => load value 1 ()); Work.factor STARTNU (() => LOAD VALUE 2 ()); e.t.c.   

But when the second work is going on, I keep getting an error, and saying that no control can be made from a separate thread that was created on it.

As you have stated, you need to make sure that When accessing UI elements, you need to make sure that you do it on proper reading. It is so easy to use the Task Parallelism library:

  Private Task Scheduler m_TaskScheduler = TaskScheduler.FromCurrentSynchronizationContext ();   

Once you have a task scheduler, you can schedule your tasks to run on the UI thread like this:

  Task. Factor Start New (() = & gt; {Load Values ​​1 ();}, Cancellation Token One, Functioning Option .nm, M_ Task Scheduler);   

Here's the warning that running all your works on the UI thread can still lock things up to you. I recommend that you create a set of categories or collections of certain types , In which you can populate relevant information and once all the data is for you to call another method on the UI thread, which adds your control to the data or whatever specific actions you need Are necessary.

No comments:

Post a Comment