0

I'm a newbie at C#, and tho I try to dig in and fix my errors by myself, I finally found one i cant seem to solve by my self.

I'm developing a small program that interacts with an android Emulator, and i have put all the android VM details in a listbox, containing a few details that i need for my current app, such as VM ID.

All was working ok, until I put that part of the program working as a new task, instead of using the GUI task ( was locking the GUI ) But now this code is giving me headaches, as if i use If InvokeRequired on it, I lose access to the data, data that I actually need in the rest of the program.

Using it as it is throw an error of cross thread operation not valid. Any ideas on this one?

//Get VM Details from Listbox
for (int i = 0; i < AndroidList.Items.Count; i++) {
    for (int j = 0; j < AndroidList.Items[i].SubItems.Count; j++) {
        string Path = @"Includes/Settings/";
        string jsonfile = File.ReadAllText(Path + AndroidList.Items[i].SubItems[j].Text + ".Json");
        var fData = JsonConvert.DeserializeObject < AppSetupDetails > (jsonfile);
    }
}

            

i need the values of i, j and fData to be passed along to be used in other parts of the code, just like the following

      // Set VM resolution for the App

            using (Process p = new Process())
            {
                p.StartInfo.CreateNoWindow = true;
                p.StartInfo.RedirectStandardOutput = true;
                p.StartInfo.WorkingDirectory = memu.Text + @"/MEmu";
                p.StartInfo.UseShellExecute = false;
                p.StartInfo.Arguments = "/c memuc setconfig -i " + fData.MemuID.ToString() + " height 640";
                p.StartInfo.FileName = "cmd.exe";
                p.Start();
                p.WaitForExit();
                p.Close();
                p.Dispose();
            }

i start the new task via button

public void AppStart_Click(object sender, EventArgs e)
    {
        
            Task task = new Task(new Action(AppFunctions));
            task.Start();
        }
  • I'm not seeing any code here that shows you using a new Task or thread to do any work, so its hard to help you with that. I see a loop that all creates variable in the content of each iteration, not passing that on to anything... so not sure what you expect to happen there. And there the process.. I'm unsure how that is related.. – Dave Jul 25 '20 at 19:07
  • 1
    Does this answer your question? [Cross-thread operation not valid: Control accessed from a thread other than the thread it was created on](https://stackoverflow.com/questions/142003/cross-thread-operation-not-valid-control-accessed-from-a-thread-other-than-the) – PMF Jul 25 '20 at 19:37
  • 1
    Duplicate of https://stackoverflow.com/questions/142003/cross-thread-operation-not-valid-control-accessed-from-a-thread-other-than-the – PMF Jul 25 '20 at 19:38
  • You are not allowed to change properties on controls from a thread other than the main UI thread. You will have to use `Invoke` to send the request to change the control back to the UI thread. You can call `Invoke` on the form or one of the controls, passing a delegate that when called will make the appropriate modification to the controls. – Lasse V. Karlsen Jul 25 '20 at 19:44
  • You can set `Control.CheckForIllegalCrossThreadCalls = false;` before you access the control. But this is not the best choice. "Control methods" should only be called from the thread that created the control. So try to use Invoke to access the UI thread in the branch thread, like `[control].Invoke(new MethodInvoker(delegate { [access control here] }))` – 大陸北方網友 Jul 27 '20 at 01:28

0 Answers0