Z
Z
Zakhar Alekseenko2014-03-14 13:46:30
.NET
Zakhar Alekseenko, 2014-03-14 13:46:30

How to call a form from a thread?

Hello. I'm making a web face for an application. For this I use the mongoose web server. The application is written in c++ .Net.
The main application and the server are in different threads. For interaction between them, an intermediary class is implemented, implemented as a singleton.
The task is reduced to that by means of the intermediary from the server to call functions of the form.
Form Code

public ref class MainForm : public System::Windows::Forms::Form{
...
public:MainForm(void){
  doWithEffect^ up;
  up = gcnew doWithEffect(this,&hitekfloor::MainForm::moveEffectUp);
  doWithEffect^ down;
  down = gcnew doWithEffect(this,&hitekfloor::MainForm::moveEffectDown);
  MediatorBetweenProgrammAndWebServer::getInstance()->setFucntionForEffects(up,down);
}
public: bool moveEffectUp(int num){/*Эта функция должна вызываться из MediatorBetweenProgrammAndWebServer  */
  if (this->InvokeRequired){
    doWithEffect^ d = gcnew doWithEffect(this, &moveEffectUp);
    this->Invoke(d, gcnew array<Object^> {num});
  }else
    SchedulerModel->MoveItemUp(num);
  return true;
}
};

Delegate Intermediary code
public delegate bool doWithEffect(int num);
ref class MediatorBetweenProgrammAndWebServer : System::Object{
public:
  static MediatorBetweenProgrammAndWebServer^ getInstance();
  void resetInstance();
  void setFucntionForEffects(doWithEffect^ up, doWithEffect^ down);

  void MoveEffectUp(unsigned int itemIndex);
  void MoveEffectDown(unsigned int itemIndex);

protected:
  MediatorBetweenProgrammAndWebServer();
  ~MediatorBetweenProgrammAndWebServer();
private:
  static MediatorBetweenProgrammAndWebServer^ m_mediator;
  doWithEffect^ upEffect;
  doWithEffect^ downEffect;
};

mediator.cpp
...
void MediatorBetweenProgrammAndWebServer::setFucntionForEffects(doWithEffect^ up, doWithEffect^ down){
  upEffect = up;
  downEffect = down;
  return;
}
...

As you can see from the above snippets of code, my idea is that instead of passing a pointer to the form to the intermediary, pass delegates of the required functions to it. Unfortunately, I get the following result when calling moveEffectUp from the broker:
A first chance exception of type 'System.InvalidOperationException' occurred in System.Windows.Forms.dll

Answer the question

In order to leave comments, you need to log in

1 answer(s)
T
Trrrrr, 2014-03-19
@Zaher220

msdn.microsoft.com/ru-ru/library/zyzhdc6b%28v=vs.1... Maybe you need Invoke?

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question