Silverlight 4 Async wait how to? - silverlight

I have a problem:
//Get All master record
entryE_QuestMaster = new ObservableCollection<E_QuestMaster>();
//Here I am calling my web service to get data
QuestVM.getExamsMasterbyExamID(eUtility.ConvertInt32(this.txtID.Text), ref entryE_QuestMaster);
//Loop to show questions
int iNumber=1;
foreach (var oIn in entryE_QuestMaster)
{
Node subNode = new Node();
subNode.Content = oIn.e_Question;
subNode.Name = "Quest_" + iNumber.ToString().Trim();
subNode.Tag = oIn.e_QID.ToString();
subNode.Icon = "/Images/Number/" + iNumber.ToString().Trim() + ".gif";
iNumber++;
this.tvMainNode.Nodes.Add(subNode);
}
Whenever I call
QuestVM.getExamsMasterbyExamID(eUtility.ConvertInt32(this.txtID.Text), ref entryE_QuestMaster);
It runs the following code
public void getExamsMasterbyExamID(int ID, ref ObservableCollection<E_QuestMaster> iCollectionData)
{
ObservableCollection<E_QuestMaster> iCollectionDataResult = iCollectionData;
eLearningDataServiceClient client = new eLearningDataServiceClient();
isSync = true;
client.getExamsMasterCompleted+=(s,e)=>
{
iCollectionDataResult = e.Result;
};
client.getExamsMasterAsync(ID);
}
My problem is I wanted to wait until my e.result comes back in iCollectionDataResult.
Currently after calling this service the system continues to the next line of code which is in the foreach loop. In this stage my entryE_QuestMaster does not have any record, I just wanted to wait till my result comes back before the loop continues.
After Answering ChrisF
no dought what chrisf said will work for me, but i wanted to do every thing in my MVVM class rather then in form level , here what i have change in my code , i still need your help guys and whated to do some professional code rather then just writing huge code.
i have added these two lines in my MVVM class
public delegate void ShowQuestionTreeView(ObservableCollection<sp_GetQuestMasterbyExamID_Result> iResultQuestMaster);
public event ShowQuestionTreeView ShowQuestionforTreeview;
then in method i have added this
/// <summary>
///
/// </summary>
/// <param name="ID"></param>
public void getExamsMasterbyExamID(int ID, ref ObservableCollection<sp_GetQuestMasterbyExamID_Result> iCollectionData)
{
ObservableCollection<sp_GetQuestMasterbyExamID_Result> iCollectionDataResult = iCollectionData;
eLearningDataServiceClient client = new eLearningDataServiceClient();
client.getExamsMasterbyExamIDCompleted+= (s, e) =>
{
iCollectionDataResult = e.Result;
**ShowQuestionforTreeview(iCollectionDataResult);**
};
client.getExamsMasterbyExamIDAsync(ID);
}
on client end i have done this
//Generate Treeview for question
QuestVM.ShowQuestionforTreeview += new eQuestCreateVM.ShowQuestionTreeView(QuestVM_ShowQuestionforTreeview);
method :
void QuestVM_ShowQuestionforTreeview(ObservableCollection<sp_GetQuestMasterbyExamID_Result> iResultQuestMaster)
{
//Loop to show questions
int iNumber = 1;
foreach (var oIn in iResultQuestMaster)
{
Node subNode = new Node();
subNode.Content = oIn.e_Question;
subNode.Name = "Quest_" + iNumber.ToString().Trim();
subNode.Tag = oIn.e_QID.ToString();
subNode.Icon = "/Images/Number/" + iNumber.ToString().Trim() + ".gif";
subNode.Title = oIn.e_Question_Text;
iNumber++;
tvCreateQuestion.Nodes[0].Nodes.Add(subNode);
}
}

You need to move this code:
client.getExamsMasterCompleted+=(s,e)=>
{
iCollectionDataResult = e.Result;
};
outside your call to getExamsMasterbyExamID. This will also mean moving where you declare and initialise the eLearningDataServicesClient.
The way you have your code set up at the moment means that you are assuming that iCollectionDataResult will be set when the method returns. However, this is not the case. The method will return before the getExamsMasterCompleted event is fired.
You will need to organise your code along the following lines:
eLearningDataServiceClient client = new eLearningDataServiceClient();
client.getExamsMasterCompleted += (s,e) =>
{
//Loop to show questions
}
client.getExamsMasterAsync(ID);
Or alternatively bind the display of questions to the ObservableCollection so when it gets set the UI updated automatically.

Related

How to parallelize 3d model creation?

Bit of a special case here, because I'm using a library called helix-toolkit but bear with me.
The thing is I would like to parallelize the creation of model objects in my code using a backgroundworker.
I know that there is a big issue with mutlithreading and working on UI elements but maybe somehow there is a workaround.
Here is the rough structure of my code:
First file in which I create the Backgroundwoker, splitting the workload for creating Geometry3D objects and finally calling SetModelGeometry to bind the geometries to the viewport. The second file shows how the binding is done.
MainWindow.xaml.cs
private void Draw_Building()
{
_worker = new BackgroundWorker { WorkerReportsProgress = true, WorkerSupportsCancellation = true };
_worker.DoWork += Draw_Building_DoWork;
_worker.ProgressChanged += DrawBuilding_ProgressChanged;
_worker.RunWorkerCompleted += DrawBuilding_RunWorkerCompleted;
_worker.RunWorkerAsync(10000);
}
private void Draw_Building_DoWork(object sender, System.ComponentModel.DoWorkEventArgs e)
{
// returns a list containing Geometry3D objects ( created by meshBuilder.ToMeshGeometry3D() )
Geometryhandler.Draw_Building(sender, e);
}
private void DrawBuilding_ProgressChanged(object sender, ProgressChangedEventArgs e)
{
StatusProgressBar.Value = e.ProgressPercentage;
var i = (int)e.UserState;
var actualComponent = MyBuildingComponents.First(c => c.Id == i);
LblStatusbarInfo.Text = "Currently meshing element #" + actualComponent.Globalid + " (" +
actualComponent.Objectname + ")";
StatusProgressbarMsg.Text = "Meshing (" + e.ProgressPercentage + " %)";
}
private void DrawBuilding_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
{
StatusProgressbarMsg.Text = "-";
StatusProgressBar.Value = 0;
LblStatusbarInfo.Text = "Meshing completed.";
Geometry = e.Result as List<MeshIdandGeometry>;
// creates MeshGeometryModel3D objects and binds them to the viewport using the List of Geometries
MainViewModel.SetModelGeometry(Geometry);
}
MainViewModel.cs
public void SetModelGeometry(List<MeshIdandGeometry> geometry)
{
MyModelGeometry = new Element3DCollection();
if (geometry != null)
{
foreach (var mygeometry in geometry)
{
var s = new MeshGeometryModel3D
{
Geometry = mygeometry.Geometry,
Material = mygeometry.Material,
};
this.MyModelGeometry.Add(s);
s.Attach(MyModelViewport.RenderHost);
}
}
this.OnPropertyChanged("MyModelGeometry");
}
My problem at the moment is the following error message:
The calling thread cannot access this object because a different
thread owns it.
which is thrown in the SetModelGeometry function when trying to attach the ModelGeometry to the viewport.
I guess the compiler is complaining about the fact that the geometries were created in different threads, to which he has no access now.
Is there any workaround/solution without destroying the parallel execution of the DrawBuilding function?
EDIT:
EDIT 2: posted the wrong version of the Draw_Building method
The Draw_Building method in the Geometryhandler:
public void Draw_Building(object sender, DoWorkEventArgs e)
{
var geometry = new List<MeshIdandGeometry>();
var standardMaterial = new PhongMaterial()
{
AmbientColor = SharpDX.Color.LightGray,
//DiffuseColor = new Color4(0.35f, 0.35f, 0.35f, 1.0f),
//DiffuseMap = new BitmapImage(new System.Uri(#"Con_Diffuse_2.jpg", System.UriKind.RelativeOrAbsolute)),
//NormalMap = new BitmapImage(new System.Uri(#"Con_Normal_2.jpg", System.UriKind.RelativeOrAbsolute)),
};
var max = _mainWindow.MyBuildingComponents.Count;
var i = 1;
// Loop over building components
foreach (var component in _mainWindow.MyBuildingComponents)
{
//if (i == 5) break;
var component1 = component;
var componentTriangles = _mainWindow.MyTriangles.Where(triangle => triangle.ComponentId == component1.Id);
var meshBuilder = new MeshBuilder(true, true, true);
// Loop over triangles in building element
foreach (var triangle in componentTriangles)
{
var triangle1 = triangle;
var p1 = _mainWindow.MyVertices.Find(
vt => vt.Id == triangle1.PointId1);
var p2 = _mainWindow.MyVertices.Find(
vt => vt.Id == triangle1.PointId2);
var p3 = _mainWindow.MyVertices.Find(
vt => vt.Id == triangle1.PointId3);
if (p1 != null && p2 != null && p3 != null)
{
//meshBuilder.AddTriangle(new Vector3((float)p1.X, (float)p1.Y, (float)p1.Z),
// new Vector3((float)p2.X, (float)p2.Y, (float)p2.Z),
// new Vector3((float)p3.X, (float)p3.Y, (float)p3.Z));
// coordination are switched to match the coordinate system in SharpDX viewport
meshBuilder.AddTriangle(new Vector3(-(float)p1.Y, (float)p1.Z, -(float)p1.X),
new Vector3(-(float)p2.Y, (float)p2.Z, -(float)p2.X),
new Vector3(-(float)p3.Y, (float)p3.Z, -(float)p3.X));
}
}
var mesh = meshBuilder.ToMeshGeometry3D();
var meshandtriangle = new MeshIdandGeometry
{
Id = component1.Id,
Geometry = mesh,
Material = standardMaterial,
};
geometry.Add(meshandtriangle);
i++;
var progressPercentage = Convert.ToInt32(((double)i / max) * 100);
var backgroundWorker = sender as BackgroundWorker;
backgroundWorker?.ReportProgress(progressPercentage, component1.Id);
}
e.Result = geometry;
}
Big thanks to #egse for finding a solution.
Part of the code that causes the problem:
var standardMaterial = new PhongMaterial()
{
AmbientColor = SharpDX.Color.LightGray,
//DiffuseColor = new Color4(0.35f, 0.35f, 0.35f, 1.0f),
//DiffuseMap = new BitmapImage(new System.Uri(#"Con_Diffuse_2.jpg", System.UriKind.RelativeOrAbsolute)),
//NormalMap = new BitmapImage(new System.Uri(#"Con_Normal_2.jpg", System.UriKind.RelativeOrAbsolute)),
};
Basically the problem with the above code is that the material is created as a local variable inside the scope of the backgroundworker. This causes problems with ownership when the UI thread tries to enter the material objects.
The solution to this problem is to make sure that the material is created by the UI thread (e.g in this case, the constructor of the Geometryhandler)
TL;DR: Do not create instances of classes which inherit from DependencyObject in another thread than the UI.

asynchronous UI update from ViewModel in WPF

I am having a problem with getting data from db and showing in UI asynchronously.
I am using MVVM light, when I click the button, action is triggered in ViewModel:
private void SearchQuery(string query)
{
_redisModel.GetFriendsListAsync(query);
}
At some point GetFriendsListCompleted is called by background thread notifing viewmodel that job is done.
At this point I need to update ListBox ItemSource. But when I try to update is I get
“The calling thread cannot access this object because a different thread owns it”
I have tried Dispatcher.CurrentDispatcher.Invoke(),App.Current.Dispatcher.Invoke() and different magic, but it still doesn’t work.
I tried to give UI dispatcher to ViewModel and then call it from there - didn't work.
private string filterText = string.Empty;
public string FilterText
{
get { return filterText; }
set
{
filterText = value;
this.RaisePropertyChanged(() => this.FilterText);
this.FriendsList.View.Refresh(); // Here where exception is happening.
}
}
I tried to change this line to
Dispatcher.Invoke(DispatcherPriority.Normal, new Action(
() =>this.FriendsList.View.Refresh())); - still the same.
I am using Telerik ListBox to display items. FriendList is CollectionViewSource(http://www.telerik.com/help/wpf/radlistbox-overview.html). It works when I use Telerik example from WPF Control Examples. Problems start to occur when I use my async methods.
Type of view is System.ComponentModel.ICollectionView it is used for Filtering and Grouping.
I have also tried to just assign ObservableCollection to Items property of the ListBox and it doesn't work either.
A bit more details on how _redisModel.GetFriendsListAsync works:
In the end(after all chain of calls) it ends up here:
public GetAsyncResult(Func<T> workToBeDone, Action<IAsyncResult> cbMethod, Object state)
{
_cbMethod = cbMethod;
_state = state;
QueueWorkOnThreadPool(workToBeDone);
}
ThreadPool.QueueUserWorkItem(state =>
{
try
{
_result = workToBeDone();
}
catch (Exception ex)
{
_exception = ex;
}
finally
{
UpdateStatusToComplete(); //1 and 2
NotifyCallbackWhenAvailable(); //3 callback invocation
}
});
In viewmodel I have method:
private void GetFriendsListCompleted(object sender, ResultsArgs<Friend> e)
{
if (!e.HasError)
{
var curr = e.Results;
if (curr != null)
{
this.FriendsList= new CollectionViewSource();
this.FriendsList.Source = list;
this.FriendsList.Filter += this.FriendFilter;
FilterText = "";
Dispatcher.Invoke(DispatcherPriority.Normal, new Action(
() => this.FriendsList.View.Refresh()));
}
}
Can anybody please help me with this ?
Thank you
You are creating CollectionViewSource in one thread and refreshing that in another thread (dispatcher thread). Update your GetFriendsListCompleted to
private void GetFriendsListCompleted(object sender, ResultsArgs<Friend> e)
{
if (!e.HasError)
{
var curr = e.Results;
if (curr != null)
{
Dispatcher.Invoke(DispatcherPriority.Normal, new Action(
() => {
this.FriendsList= new CollectionViewSource();
this.FriendsList.Source = list;
this.FriendsList.Filter += this.FriendFilter;
FilterText = "";
this.FriendsList.View.Refresh();
}));
}
}
}
You haven't shown any of the code that's actually running on the background thread on completion but I'm guessing that in it you're creating a collection object that you're then trying to assign to your CollectionView. When the CV tries to update (on the UI thread) from your Refresh call it would then try to use the collection that's owned by the other thread.
If you include the relevant code it would be easier to say for sure.

wpf threading, thread must be STA

I have a window(say main window) with a frame which has a page in it. A button on the page opens another window(say popup window). Now i am trying to invoke a method in the main window from a button on the popup window. The method has to be multi-threaded, i had a similar solution running in windows forms but i keep getting the calling thread must be STA because many UI components require this in WPF.
The method on the page which opens the popup window modally
Scripts showStocks = new Scripts();
showStocks.ShowInTaskbar = false;
showStocks.ShowDialog();
if (showStocks.DialogResult==true)
{
Window1 wd1 = new Window1();
wd1.doneDeal();
}
Here window1 is our main window. The doneDeal method is
public void doneDeal()
{
// **Some Code**
BackgroundWorker wworks1 = new BackgroundWorker();
wworks1.DoWork += Tickes;
wworks1.RunWorkerCompleted += Tickes2;
wworks1.RunWorkerAsync();
// Page1 pg1 = frame1.Content as Page1;
//NextPrimeDelegate dd=new NextPrimeDelegate(okreport);
// pg1.addScriptBtn.Dispatcher.BeginInvoke(DispatcherPriority.Normal,
// new NextPrimeDelegate(okreport));
//startStopButton.Dispatcher.BeginInvoke(
// DispatcherPriority.Normal,
// new NextPrimeDelegate(CheckNextNumber));
//new Thread(() => Tick(stock, rowID, exchange)) { IsBackground = false }.Start();
}
Finally the method that i am trying to run in the background
public void Tickes(object sender, DoWorkEventArgs e)
{
}
Also i want to populate a gridview from the result of the tickes method, this will be looping and running over and over in the background but periodically returning data to be added to the grid. SHould i do that in the progress update event ? Have tried a lot to wrap my head around the dispatcher and background worker in wpf but am failing to understand the STA apartment state bit. If someone can help me to get my tickes method going or point me in the right direction, i would be very very thankful.
Well i finally was able to solve the STA thread problem am posting the answer just in case somebody comes across a similar problem in the future.
public void doneDeal()
{
if (StockData.flag == 1)
{
row1 = table.NewRow();
row1[col1] = "";
row1[col2] = "";
row1[col3] = "";
row1[col4] = "";
row1[col5] = "";
row1[col6] = "";
row1[col7] = "";
row1[col8] = "";
row1[col9] = "";
row1[col10] = "";
row1[col11] = "";
row1[col12] = "";
table.Rows.Add(row1);
string stock = StockData.stock;
int rowID = (table.Rows.Count - 1);
string exchange = StockData.exchange;
Thread bh = new Thread(delegate()
{
Tick7(stock, rowID, exchange);
});
bh.SetApartmentState(ApartmentState.STA);
bh.IsBackground = true;
bh.Start();
StockData.flag = -1;
}
}
The Tick7 method which is being called is declared like this
[STAThread]
public void Tick7(string stock, int rowID, string exchange)
{
int rowNum = rowID;
int counter = -1;
deletecounter = StockData.deletecounter;
Thread.CurrentThread.Name = StockData.stock;
.
.
.
}

problem in silverlight 4 async how to wait till result come

Here is what i have problem
i have following code :
//Get All master record
entryE_QuestMaster = new ObservableCollection<E_QuestMaster>();
QuestVM.getExamsMasterbyExamID(eUtility.ConvertInt32(this.txtID.Text), ref entryE_QuestMaster);
//
//Loop to show questions
int iNumber=1;
foreach (var oIn in entryE_QuestMaster)
{
Node subNode = new Node();
subNode.Content = oIn.e_Question;
subNode.Name = "Quest_" + iNumber.ToString().Trim();
subNode.Tag = oIn.e_QID.ToString();
subNode.Icon = "/Images/Number/" + iNumber.ToString().Trim() + ".gif";
iNumber++;
this.tvMainNode.Nodes.Add(subNode);
}
here is async method calling wcf service
/// <summary>
///
/// </summary>
/// <param name="ID"></param>
public void getExamsMasterbyExamID(int ID, ref ObservableCollection<E_QuestMaster> iCollectionData)
{
ObservableCollection<E_QuestMaster> iCollectionDataResult = iCollectionData;
eLearningDataServiceClient client = new eLearningDataServiceClient();
client.getExamsMasterCompleted+=(s,e)=>
{
iCollectionDataResult = e.Result;
};
client.getExamsMasterAsync(ID);
}
problem : when ever system run --> QuestVM.getExamsMasterbyExamID(eUtility.ConvertInt32(this.txtID.Text), ref entryE_QuestMaster);
its does not wait till i get e.result its just move to next line of code which is
foreach loop.
plssss help any one or give idea with sample code what should i do to wait till e.result
i wanted to some how wait till i get e.result
any idea ?
one simple solution. Move the code that you want to execute after getting e.result in this loop.
client.getExamsMasterCompleted+=(s,e)=>
{
iCollectionDataResult = e.Result;
};
Add an Action to the getExamsMasterbyExamID method, then execute the callback after is completed.
public void getExamsMasterbyExamID(int ID, ref ObservableCollection<E_QuestMaster> iCollectionData,Action<object> callback)
{
ObservableCollection<E_QuestMaster> iCollectionDataResult = iCollectionData;
eLearningDataServiceClient client = new eLearningDataServiceClient();
client.getExamsMasterCompleted+=(s,e)=>
{
iCollectionDataResult = e.Result;
//the callback will be executed on the calling method
callback(e.Result);
};
client.getExamsMasterAsync(ID);
}
now when you call the Async method add the loop inside the callback like this:
entryE_QuestMaster = new ObservableCollection<E_QuestMaster>();
QuestVM.getExamsMasterbyExamID(eUtility.ConvertInt32(this.txtID.Text), ref entryE_QuestMaster,r=>
{
int iNumber=1;
foreach (var oIn in entryE_QuestMaster)
{
Node subNode = new Node();
subNode.Content = oIn.e_Question;
subNode.Name = "Quest_" + iNumber.ToString().Trim();
subNode.Tag = oIn.e_QID.ToString();
subNode.Icon = "/Images/Number/" + iNumber.ToString().Trim() + ".gif";
iNumber++;
this.tvMainNode.Nodes.Add(subNode);
}
});
after 2 months prob this wont be useful for the asker but maybe for someone else...

Silverlight: Webservice method returning ObservableCollection instead of Generic List

I have a webservice that calls a method that returns a generic list of class BodyPartUrls like so:
public List<BodyPartUrls> getCharacterClassBody(int characterClassID)
{
var bpulst = new List<BodyPartUrls>();
var iqcb = ydc.ClassBodies.Where(cb => cb.characterClassID == characterClassID);
foreach (var icb in iqcb)
{
var bpu = new BodyPartUrls();
bpu.bodyPartName = icb.BodyPart.bodyPartName;
bpu.bodyName = icb.Body.bodyName;
bpu.puppetID = characterClassID;
bpulst.Add(bpu);
}
return bpulst;
}
BodyPartUrls only consists of string and integer properties, note that ydc is refering to a datacontext. This the code in the webservice :
[WebMethod]
public List<BodyPartUrls> getCharacterClassBody(int characterClassID)
{
return b.getCharacterClassBody(characterClassID);
}
Now to call the method in silverlight I utalised the following code :
public void initialiseBodiesSoapClientClient()
{
string webServiceUrl = pu.GetUrlForResource("Bodies.asmx");
System.ServiceModel.BasicHttpBinding binding = new System.ServiceModel.BasicHttpBinding();
EndpointAddress endpoint = new EndpointAddress(webServiceUrl);
bsc = new BodiesRef.BodiesSoapClient(binding, endpoint);
bsc.getCharacterClassBodyCompleted += new EventHandler<Yambushi.BodiesRef.getCharacterClassBodyCompletedEventArgs>(bsc_getCharacterClassBodyCompleted);
}
The method pu.GetUrlForResource get's the url of where the webservice is hosted, the following is method bsc_getCharacterClassBodyCompleted :
void bsc_getCharacterClassBodyCompleted(object sender, Yambushi.BodiesRef.getCharacterClassBodyCompletedEventArgs e)
{
bpulist = e.Result;
}
bpulist is a generic list of BodyPartUrls, for some reason e.Result is returning ObservableCollection instead of the generic list. I have similar code to retreive other generic lists that work fine so I really can't understand why this is acting differently.
Click Configure Service Reference. Under the Data Type section you can select what type you want collections or dictionaries to return as.

Resources