C++ CLI:进程结束后与后台工作者的跨线程异常

问题描述 投票:0回答:0

我正在尝试制作一个 C++ CLI 窗口,它有一个后台工作程序,它向计算机添加了一些 .xml wifi 配置文件,但是这样做之后,我得到一个 System.InvalidOperationException 异常:'跨线程操作无效:控制“FirstLoginPage”是从创建它的线程以外的线程访问的。我试图让它一步一步地完成所有事情,但现在我不确定后台工作者是否最适合这个想法。

我几乎没有使用 CLI 的经验,只有基本的 C++。

代码:

    private: System::Void buttonRetry_Click(System::Object^ sender, System::EventArgs^ e)
    {

    }
    //Conntect Wifi, Test Connection
    private: System::Void backgroundWorkerWifi_DoWork(System::Object^ sender, System::ComponentModel::DoWorkEventArgs^ e)
    {
        //These Work
        //enable the text and loading gif to inform user about progress
        this->labelWifi->Visible = true;
        this->pictureBoxWifi->Visible = true;


        //If this part is commented out, then there is no exception
        //Add Wifi to known networks
        System::Diagnostics::Process^ wifi = gcnew System::Diagnostics::Process();
        wifi->StartInfo->FileName = "cmd.exe";
        wifi->StartInfo->Arguments = "/c netsh wlan add profile filename=\"" + Application::StartupPath + "\\resources\\xmls\\WifiAuto.xml" + "\"";
        wifi->StartInfo->WorkingDirectory = Application::StartupPath + "\\resources\\xmls";
        wifi->StartInfo->CreateNoWindow = true;
        wifi->StartInfo->UseShellExecute = false;
        wifi->Start();
        wifi->WaitForExit();
        wifi->StartInfo->FileName = "cmd.exe";
        wifi->StartInfo->Arguments = "/c netsh wlan add profile filename=\"" + Application::StartupPath + "\\resources\\xmls\\WifiBackup.xml" + "\"";
        wifi->StartInfo->WorkingDirectory = Application::StartupPath + "\\resources\\xmls";
        wifi->StartInfo->CreateNoWindow = true;
        wifi->StartInfo->UseShellExecute = false;
        wifi->Start();
        wifi->WaitForExit();

        //This one works aswell
        //update loading gif to green checkmark png
        this->pictureBoxWifi->Image = Image::FromFile(Application::StartupPath + "\\resources\\pictures\\checkmark.png");
        
        //these two throw an exception:System.InvalidOperationException: 
        //'Cross-thread operation not valid: Control 'FirstLoginPage' accessed from a thread other than the thread it was created on.'
        
        //make "connecting..." label visible to let user know, that the program is checking for internet connection
        this->labelConnection->Visible = true;
        this->pictureBoxConnection->Visible = true;

        //This part hasn't been tested fully yet, exception occurs before this part
        //Try Connection
        //try 100 times for connection
        /*
        for (int i = 1; i < 2; i++)
        {
            //If connection is made, start downloading and installing Tools, else install offline files
            if (system("ping www.google.com"))
            {
                this->labelConnection->Text = "Connected!";
                this->pictureBoxConnection->Image = Image::FromFile(Application::StartupPath + "\\resources\\pictures\\checkmark.png");
                this->buttonRetry->Visible = false;
                //this->labelDownload->Visible = true;
                //this->progressBarDownload->Visible = true;
                //backgroundWorkerDownload->RunWorkerAsync();
            }
            else if (i == 2)
            {
                this->buttonRetry->Visible = true;
            }
        }*/
    }

尝试了 Invoke() thingy(我什至不知道它的作用或含义),然后尝试首先在 backgroundworker 中创建标签和图片框,但它们都没有帮助。

exception command-line-interface c++-cli clr
© www.soinside.com 2019 - 2024. All rights reserved.