微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

Windows 窗体 C++ 多线程

如何解决Windows 窗体 C++ 多线程

我正在 Winforms 中制作一个 GUI,并希望将单独的线程用于按钮功能。 但是无论我尝试以哪种方式实施它,我总是陷入无法解决的境地。 这是我尝试过的:

private: System::Void button1_MouseHover(System::Object ^ sender,System::EventArgs ^ e)
        {
            Thread^ t1 = gcnew Thread(gcnew ThreadStart( &Form1::t1));

            t1->Start();
        }

               static void t1()
               {
                   this->FireDrakeButton->Location = System::Drawing::Point(433,648);
                   Sleep(1000);
                   this->FireDrakeButton->Location = System::Drawing::Point(433,618);
                   Sleep(1000);
                   this->FireDrakeButton->Location = System::Drawing::Point(433,588);
               } 

错误(活动)E0258 'this' 只能在非静态成员函数 GUI3 中使用

如果我将 Void 设为非静态,则 Thread^ t1 函数将不起作用(出于某种原因,它仅适用于静态 void)。

private: System::Void button1_MouseHover(System::Object ^ sender,System::EventArgs ^ e)
        {
            std::thread t1(t1);
        }

               void t1()
               {
                   this->FireDrakeButton->Location = System::Drawing::Point(433,588);
               }

这里出现了这个错误

错误 C3867 'GUI3::Form1::t1':非标准语法;使用“&”创建指向成员 GUI3 的指针

不管我把 & 放在哪里,代码都不会运行。

最后我尝试了 boost::thread,它完全无法打开。

最后,这是创建 GUI 元素的代码部分(将其缩短为我们需要的按钮)。

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^ FireDrakeButton;
    private: System::Windows::Forms::Button^ LavaSlimeButton;
    private: System::Windows::Forms::Button^ LavaGolemButton;
    private: System::Windows::Forms::GroupBox^ Actions;
    private: System::Windows::Forms::Label^ label1;
    protected:



    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)
        {
            System::ComponentModel::ComponentResourceManager^ resources = (gcnew System::ComponentModel::ComponentResourceManager(Form1::typeid));
            this->FireDrakeButton = (gcnew System::Windows::Forms::Button());
            this->LavaSlimeButton = (gcnew System::Windows::Forms::Button());
            this->LavaGolemButton = (gcnew System::Windows::Forms::Button());
            this->Actions = (gcnew System::Windows::Forms::GroupBox());
            this->label1 = (gcnew System::Windows::Forms::Label());
            this->Actions->SuspendLayout();
            this->SuspendLayout();
            // 
            // FireDrakeButton
            // 
            this->FireDrakeButton->Anchor = System::Windows::Forms::AnchorStyles::None;
            this->FireDrakeButton->BackColor = System::Drawing::SystemColors::GradientActiveCaption;
            this->FireDrakeButton->BackgroundImage = (cli::safe_cast<System::Drawing::Image^>(resources->Getobject(L"FireDrakeButton.BackgroundImage")));
            this->FireDrakeButton->BackgroundImageLayout = System::Windows::Forms::ImageLayout::Zoom;
            this->FireDrakeButton->Font = (gcnew System::Drawing::Font(L"Arial Narrow",20.25F,System::Drawing::FontStyle::Regular,System::Drawing::GraphicsUnit::Point,static_cast<System::Byte>(0)));
            this->FireDrakeButton->ForeColor = System::Drawing::SystemColors::ControlText;
            this->FireDrakeButton->Location = System::Drawing::Point(433,678);
            this->FireDrakeButton->Name = L"FireDrakeButton";
            this->FireDrakeButton->Size = System::Drawing::Size(170,236);
            this->FireDrakeButton->TabIndex = 0;
            this->FireDrakeButton->UseVisualStyleBackColor = false;
            this->FireDrakeButton->Click += gcnew System::EventHandler(this,&Form1::FireDrakeButton_Click);
            this->FireDrakeButton->MouseLeave += gcnew System::EventHandler(this,&Form1::button1_MouseLeave);
            this->FireDrakeButton->MouseHover += gcnew System::EventHandler(this,&Form1::button1_MouseHover); 

感谢任何帮助。

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。