【问题标题】:change a Windows Form Control in a thread在线程中更改 Windows 窗体控件
【发布时间】:2012-01-11 14:04:19
【问题描述】:

我已经问过类似的问题,但我找到的解决方案对我有部分帮助,所以我问的问题与我之前的问题类似。 我的问题是在线程中我希望按钮的文本发生变化。 线程工作正常,我可以看到我正在显示的 MessageBox,但按钮的文本保持不变。 我该如何改变它? 如果我需要使用委托(注释文本),我该如何解决?因为该代码引发了一些关于错误 '(' 和 '{' 的错误,但这是我之前得到的答案

#pragma once


namespace UIThread {

using namespace System;
using namespace System::ComponentModel;
using namespace System::Collections;
using namespace System::Windows::Forms;
using namespace System::Data;
using namespace System::Drawing;
using namespace System::Threading;

/// <summary>
/// Summary for Form1
///
/// WARNING: If you change the name of this class, you will need to change the
///          'Resource File Name' property for the managed resource compiler tool
///          associated with all .resx files this class depends on.  Otherwise,
///          the designers will not be able to interact properly with localized
///          resources associated with this form.
/// </summary>


public ref class Form1 : public System::Windows::Forms::Form
{

public:
    Form1(void)
    {
        InitializeComponent();
        //
        //TODO: Add the constructor code here
        //
    }

protected:
    /// <summary>
    /// Clean up any resources being used.
    /// </summary>
    ~Form1()
    {
        if (components)
        {
            delete components;
        }
    }
private: System::Windows::Forms::Button^  BtnStart;
protected: 

private:
    /// <summary>
    /// Required designer variable.
    /// </summary>
    System::ComponentModel::Container ^components;

#pragma region Windows Form Designer generated code
    /// <summary>
    /// Required method for Designer support - do not modify
    /// the contents of this method with the code editor.
    /// </summary>
    void InitializeComponent(void)
    {
        this->BtnStart = (gcnew System::Windows::Forms::Button());
        this->SuspendLayout();
        // 
        // BtnStart
        // 
        this->BtnStart->Location = System::Drawing::Point(114, 38);
        this->BtnStart->Name = L"BtnStart";
        this->BtnStart->Size = System::Drawing::Size(124, 37);
        this->BtnStart->TabIndex = 0;
        this->BtnStart->Text = L"button1";
        this->BtnStart->UseVisualStyleBackColor = true;
        this->BtnStart->Click += gcnew System::EventHandler(this, &Form1::BtnStart_Click);
        // 
        // Form1
        // 
        this->AutoScaleDimensions = System::Drawing::SizeF(6, 13);
        this->AutoScaleMode = System::Windows::Forms::AutoScaleMode::Font;
        this->ClientSize = System::Drawing::Size(284, 262);
        this->Controls->Add(this->BtnStart);
        this->Name = L"Form1";
        this->Text = L"Form1";
        this->ResumeLayout(false);

    }
#pragma endregion
private: System::Void BtnStart_Click(System::Object^  sender, System::EventArgs^  e) {
    Form1^ f=gcnew Form1;
    Thread^ oThread = gcnew Thread( gcnew ThreadStart( f, &Form1::ThreadMethod ) );
    oThread->Start();
}

private: void ThreadMethod(/*Object^ state*/)
{   
    BtnStart->Text="hello";
    MessageBox::Show("AAAAAA");
    //this->Invoke((Action^)delegate(){BtnStart->Text = "Hello";});
}
};

}

【问题讨论】:

    标签: c++ winforms multithreading c++-cli


    【解决方案1】:

    您设法摆脱了告诉您您做错了的运行时异常。通过创建一个 new 表单对象。一个不可见的,因为您从未调用过它的 Show() 方法。所以你也看不到按钮文本更新。您要做的是更新 现有 表单对象:

    System::Void BtnStart_Click(System::Object^  sender, System::EventArgs^  e) {
        Thread^ oThread = gcnew Thread( gcnew ThreadStart( this, &Form1::ThreadMethod ) );
        oThread->Start();
    }
    

    您对上一个问题的建议很糟糕,该语法仅适用于 C#。 C++/CLI 不支持匿名委托。你必须把它写出来,就像你为 ThreadStart 委托所做的那样:

    void UpdateButton() {
        BtnStart->Text="hello";
    }
    
    void ThreadMethod() {   
        this->Invoke(gcnew MethodInvoker(this, &Form1::UpdateButton));
    }
    

    【讨论】:

    • 谢谢,我现在有一些不同了,我收到一个运行时错误,上面写着(我从意大利语翻译),在没有创建 Windows 句柄之前,无法在控件上调用 Invoke 或 BeginInvoke。有什么想法吗?
    • 确定!这是一个愚蠢的错误,Thread^ oThread = gcnew Thread(gcnew ThreadStart(this, &Form1::ThreadMethod));使用这个而不是 f 是正确的......非常感谢你,希望加倍支持你的答案
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-06-15
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多