VC++/CLIのアイドル処理

解決


扉たたき  2008-03-07 05:00:45  No: 67780

VC++/CLI 2005

windows Form Application で「アイドル処理」を行いたいと思っているのですが
そのようなイベント等を見つけきれずにいます。
どのような方法がありますでしょうか。教えていただきたいです。
お願いします。


επιστημη  URL  2008-03-07 07:52:56  No: 67781

ref class IdleRunner {
public:
  void OnIdle(Object^ sender, EventArgs^ e) {
    // アイドル時の処理
  }
};

[STAThreadAttribute]
int main(array<System::String ^> ^args)
{
  // コントロールが作成される前に、Windows XP ビジュアル効果を有効にします
  Application::EnableVisualStyles();
  Application::SetCompatibleTextRenderingDefault(false); 

  // この2行追加。
  IdleRunner idle;
  Application::Idle += gcnew EventHandler(%idle,&IdleRunner::OnIdle);

  // メイン ウィンドウを作成して、実行します
  Application::Run(gcnew Form1());
  return 0;
}


扉たたき  2008-03-07 21:58:20  No: 67782

επιστημηさん、ありがとうございます。

IdleRunnerクラスを追加し、[プロジェクト名].cppに
IdleRunner idle;
Application::Idle += gcnew EventHandler(%idle,&IdleRunner::OnIdle);
の2行を追加することで目的の処理が行えました。ありがとうございます。

行えたのですが新たに疑問点が出てきたのでお答え願えないでしょうか。
アイドル処理を行った回数を数え、button1が押されるとその回数を
MessageBoxで表示させたいとおもっています。
そしてMessageBoxを閉じたあと、アイドル処理を新たに始める方法としてど
のようなものがあるでしょうか。

回数を数えるためにIdleRunnerにprivateメンバ[Counter]をつくり

//アイドル時の処理
this->Counter += 1;

としてカウントしているのですが、そこからどうbutton1のクリックイベント
につなげ、新たに始めさせるのか、というところです。
よろしくお願いします。

最初の不明点は無事行えましたので「解決」とさせていただきます。


Blue  2008-03-08 00:10:34  No: 67783

FormからIdleRunnerクラスのCounterメンバの値をとれるようにする。

○delegateをつかう方法
public ref class IdleRunner
{
    int counter;
public:
    IdleRunner() : counter(0) {}

    System::Void OnIdle(System::Object^ sender, System::EventArgs^ e)
    {
        ++this->counter;
    }

    int GetCounter() { return this->counter; }
};

[STAThreadAttribute]
int main(array<System::String ^> ^args)
{
    // コントロールが作成される前に、Windows XP ビジュアル効果を有効にします
    Application::EnableVisualStyles();
    Application::SetCompatibleTextRenderingDefault(false); 

    IdleRunner idle;
    Application::Idle += gcnew EventHandler(%idle, &IdleRunner::OnIdle);

    // メイン ウィンドウを作成して、実行します
    Form1 frm;
    frm.getcounter = gcnew Form1::GetCounterDelegate(%idle, &IdleRunner::GetCounter);
    Application::Run(%frm);
    return 0;
}

Form.h

    public:
        delegate int GetCounterDelegate();
        GetCounterDelegate^ getcounter;

    private:
        System::Void button1_Click(System::Object^  sender, System::EventArgs^  e)
        {
            if (this->getcounter != nullptr)
            {
                MessageBox::Show(this->getcounter().ToString());
            }
        }
    };


扉たたき  2008-03-08 01:15:57  No: 67784

Blueさん、ありがとうございます。

ばっちり解決できました。
ただ、[delegate]は初めて知った言葉なので何がなんだか。

delegate int GetCounterDelegate();  //???

変数?関数?  勉強が必要ですね。


Blue  2008-03-08 02:38:00  No: 67785

Form1だけがアイドルイベントを扱うので良いのであれば、

Form1.h

        int counter;
    public:
        System::Void OnIdle(System::Object^ sender, System::EventArgs^ e)
        {
            ++this->counter;
        }
    private:
        System::Void button1_Click(System::Object^  sender, System::EventArgs^  e)
        {
            MessageBox::Show(this->counter.ToString());
        }

XXX.cpp

[STAThreadAttribute]
int main(array<System::String ^> ^args)
{
    // コントロールが作成される前に、Windows XP ビジュアル効果を有効にします
    Application::EnableVisualStyles();
    Application::SetCompatibleTextRenderingDefault(false); 

    // メイン ウィンドウを作成して、実行します
    Form1 frm;
    Application::Idle += gcnew EventHandler(%frm, &Form1::OnIdle);
    Application::Run(%frm);
    return 0;
}

とすれば十分でしょう。


扉たたき  2008-03-08 02:53:25  No: 67786

Blueさん、ありがとうございます。

おっしゃるとおり、Form1だけで扱うため、上記の方法を参考にさせていただきます。

大変参考になりました。ありがとうございました。


※返信する前に利用規約をご確認ください。

※Google reCAPTCHA認証からCloudflare Turnstile認証へ変更しました。






  このエントリーをはてなブックマークに追加