Click here to Skip to main content
15,891,136 members
Home / Discussions / C#
   

C#

 
AnswerRe: Error Code 14 from Yahoo. Pin
Ashfield19-Mar-09 2:21
Ashfield19-Mar-09 2:21 
Questionadding more than one control to the single cell in datagridview control [modified] Pin
myms.net19-Mar-09 1:51
myms.net19-Mar-09 1:51 
AnswerRe: adding more than one control to the single cell in datagridview control Pin
musefan19-Mar-09 2:20
musefan19-Mar-09 2:20 
GeneralRe: adding more than one control to the single cell in datagridview control Pin
myms.net19-Mar-09 2:24
myms.net19-Mar-09 2:24 
GeneralRe: adding more than one control to the single cell in datagridview control Pin
musefan19-Mar-09 2:37
musefan19-Mar-09 2:37 
GeneralRe: adding more than one control to the single cell in datagridview control Pin
myms.net19-Mar-09 2:45
myms.net19-Mar-09 2:45 
GeneralRe: adding more than one control to the single cell in datagridview control Pin
musefan19-Mar-09 2:48
musefan19-Mar-09 2:48 
QuestionThreading Questions - Object Ownership Pin
Jammer19-Mar-09 1:33
Jammer19-Mar-09 1:33 
Hi All,

I have very little experience with threading and have been building an object that creates a thread internally (not sure this is even a good design idea). Just as an example I have created this class (with a delegate):

public delegate void ThreadCompleterEventHandler(object sender, ThreadCompleteEventArgs args);

public class ObjectThatCreatesAThread
{
    public event ThreadCompleterEventHandler ThreadCompleted;
    private Thread t;

    public ObservableCollection<string> StringsCollection;

    public ObjectThatCreatesAThread()
    {
        StringsCollection = new ObservableCollection<string>();
    }

    public void PopulateStringsCollectionOnOtherThread()
    {
        t = new Thread(delegate()
            {
                DoWork();
            });
        t.Name = "Populate StringsCollection Thread";
        t.Start();
    }

    private void DoWork()
    {
        StringsCollection.Add("String1");
        StringsCollection.Add("String2");
        StringsCollection.Add("String3");
        OnThreadCompleted();
    }

    private void OnThreadCompleted()
    {
        ThreadCompleted(this, new ThreadCompleteEventArgs());
    }
}


This object is created by a button click on a WPF UI. Like:

private void RunThread_Click(object sender, RoutedEventArgs e)
{
    obj = new ObjectThatCreatesAThread();
    obj.ThreadCompleted += new ThreadCompleterEventHandler(obj_ThreadCompleted);
    obj.PopulateStringsCollectionOnOtherThread();
}


In the eventhandler in the UI I have:

void obj_ThreadCompleted(object sender, ThreadCompleteEventArgs args)
{
    StringsListBox.ItemsSource = obj.StringsCollection;
}


Which throws a InvalidOperationException "The calling thread cannot access this object because a different thread owns it.". Which is easily rectified by doing this instead:

void obj_ThreadCompleted(object sender, ThreadCompleteEventArgs args)
{
    Dispatcher.BeginInvoke(DispatcherPriority.Normal, new Action(delegate()
    {
        StringsListBox.ItemsSource = obj.StringsCollection;
    }));
}


My question is why does the DoWork() method running on the spawned thread gain ownership of the StringCollection object that was in fact constructed on the UI thread?

Is there any way to move this collection back to the ownership of the UI thread without the need for the Dispatcher.BeginInvoke()

Am I actually just trying to code around a bad practise?

I would prefer to make this object as user friendly as possible so that even though it is spawning a thread internally at the end of the threads execution the collection is made available to other threads to work with, I guess specifically the UI thread. Is there anything I can set at thread construction time to facilitate this?

Thanks,


AnswerRe: Threading Questions - Object Ownership Pin
Fayu19-Mar-09 3:09
Fayu19-Mar-09 3:09 
GeneralRe: Threading Questions - Object Ownership Pin
Jammer19-Mar-09 4:44
Jammer19-Mar-09 4:44 
GeneralRe: Threading Questions - Object Ownership Pin
Fayu19-Mar-09 5:33
Fayu19-Mar-09 5:33 
GeneralRe: Threading Questions - Object Ownership Pin
Jammer19-Mar-09 8:57
Jammer19-Mar-09 8:57 
GeneralRe: Threading Questions - Object Ownership Pin
Fayu19-Mar-09 9:44
Fayu19-Mar-09 9:44 
GeneralRe: Threading Questions - Object Ownership [modified] Pin
Fayu19-Mar-09 13:40
Fayu19-Mar-09 13:40 
QuestionUsing Rejex to validate user name Pin
shabya19-Mar-09 1:24
shabya19-Mar-09 1:24 
AnswerRe: Using Rejex to validate user name Pin
musefan19-Mar-09 1:40
musefan19-Mar-09 1:40 
GeneralRe: Using Rejex to validate user name Pin
shabya19-Mar-09 2:42
shabya19-Mar-09 2:42 
GeneralRe: Using Rejex to validate user name Pin
musefan19-Mar-09 2:46
musefan19-Mar-09 2:46 
GeneralRe: Using Rejex to validate user name Pin
Ian Shlasko19-Mar-09 3:27
Ian Shlasko19-Mar-09 3:27 
GeneralRe: Using Rejex to validate user name Pin
musefan19-Mar-09 3:31
musefan19-Mar-09 3:31 
GeneralRe: Using Rejex to validate user name Pin
shabya19-Mar-09 3:59
shabya19-Mar-09 3:59 
AnswerRe: Using Rejex to validate user name Pin
Eslam Afifi19-Mar-09 3:26
Eslam Afifi19-Mar-09 3:26 
GeneralRe: Using Rejex to validate user name Pin
shabya19-Mar-09 3:57
shabya19-Mar-09 3:57 
QuestionSerialization Pin
Paul Unsworth19-Mar-09 1:03
Paul Unsworth19-Mar-09 1:03 
AnswerRe: Serialization Pin
Cracked-Down19-Mar-09 1:10
Cracked-Down19-Mar-09 1:10 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.