I'm trying to generate and dispatch (while having my UI Thread with progressRing.IsActive = true;
), three List
Objects on a BackgroundWorker
and then transfer said list to the UI Thread
, but I'm running into issues with...
Must create DependencySource on same Thread as the DependencyObject.
Resources, I've read
- C# lock (MSDN Documentation)
- Sending Arguments to Background Worker (Stackoverflow)
- C# Background Worker (MSDN Documentation)
- C# getting Background Worker to return a result (Stackoverflow)
Method BackgroundLogin()
of Partial Class MainWindow
private void BackgroundLogin()
{
//process the form on UI Thread
this.LoginForm(FadeOut);
this.LoadingRing(FadeIn);
//Start a new Thread
BackgroundWorker worker = new BackgroundWorker();
worker.DoWork += worker_DoWork;
worker.RunWorkerCompleted += worker_RunWorkerCompleted;
//initialize custom class WorkerThread object to store/process a result
WorkerThread wt = new WorkerThread(this, txtEmailAddress.Text, txtPassword.Password);
//start the worker and send the object across.
worker.RunWorkerAsync(wt);
}
Method worker_DoWork
of Partial Class MainWindow
private void worker_DoWork(object sender, DoWorkEventArgs e)
{
//grab the object
WorkerThread wt = (WorkerThread)e.Argument;
//check if we can login
if (!wt.Login())
{
//cancel the thread
e.Cancel = true;
}
else
{
//load additional data
wt.UpdateAuthLbl(".. Loading New Data ..");
wt.LoadLists();
wt.UpdateAuthLbl(".. Data Loaded ..");
}
//pass the object back
e.Result = wt;
}
Method loadLists()
of Class WorkerThread
/// <summary>
/// Load data into the list
/// </summary>
public void LoadLists()
{
this.gene_fact_list = db.loadGeneFactTable();
this.gene_trait_fact_list = db.loadGeneTraitFactTable(this.gene_fact_list);
this.category_trait_list = db.loadCategoryTraits();
}
Method worker_RunWorkerCompleted
of Partial Class MainWindow
, Object gl
of Class GeneList
private void worker_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
{
//grab the finished object
WorkerThread wt = (WorkerThread) e.Result;
//work out if we are logged in
Boolean LoginFlag = !e.Cancelled && e.Error == null;
if (LoginFlag)
{
lblAuthentication.Content = ".. Loading Interface ..";
//pass the finished object into memory
this.gl = wt;
//reset the listbox
this.resetListBox();
}
this.LoginForm(LoginFlag);
}
Method resetListBox()
and ListBoxItems
/// <summary>
/// Load the data for the form
/// </summary>
public void resetListBox()
{
if (this.gl.notNullOrEmpty())
{
this.ListBoxItems.Clear();
//begin compiling the mainTab
foreach (KeyValuePair<long, GeneNotesDataModel> kvp in this.gl.gene_fact_list)
{
this.ListBoxItems.Add(kvp.Value);
}
}
} //close function
//declare WPF list binding
private ObservableCollection<GeneNotesDataModel> _listBoxItems = new ObservableCollection<GeneNotesDataModel>();
/// <summary>
/// Control the listbox of rsid codes
/// </summary>
public ObservableCollection<GeneNotesDataModel> ListBoxItems
{
get { return _listBoxItems; }
set { _listBoxItems = value; }
}
XAML ListBox
lstSnpCodes
<ListBox ItemsSource="{Binding ElementName=UI, Path=ListBoxItems}" Margin="6,38,0,60" BorderThickness="2" HorizontalAlignment="Left" Width="180" Name="lstSnpCodes" SelectionChanged="lstSnpCodes_SelectionChanged" KeyUp="OnKeyUpHandler" />
The line this.ListBoxItems.Add(kvp.Value);
causes the Exception
to occur (If I replace it with Debug.WriteLine(kvp.Value.getValueAsString());
it will run just fine). Any ideas on why I'm getting a DependencySource exception? and why isn't possible to transfer ListA
from Slave Thread
to Main Thread
?
PasteBin Links expire in April 2013