Hello, I thought I was doing things right when I created a delegate to call a function to access the container's data, but it still throws an exception.

I have something similar to:

public ref class myform : public System::Windows::Forms::Form
  private: System::Windows::Forms::ComboBox^  form_combobox;
  private: delegate String^ getItemDel();//<-- delegate

  private: String^ GetComboboxSelectedItem(){  //<-- function I call with a delegate.
  return form_combobox->SelectedItem->ToString();

  private: Thread ^work_thread;//<-- used to run DoWork() in a new thread.
  private: void DoWork() { //<-- worker function that accesses the combobox data.
  //access container's data.
  getItemDel ^f = gcnew getItemDel(this, &myform::GetComboboxSelectedItem);
  String ^result = f->Invoke();//<-- ** problem **

Sorry, I've done my best to condense it. I thought that was the accepted way to access the data, I must be mistaken.

It throws an exception saying I'm not supposed to access the container's data from a different thread.

Ok actually I solved it, there IS a difference between:

Invoke( someDelegate );



Invoke(someDelegate); will do exactly what I needed. :)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts learning and sharing knowledge.