-2

I have a method which returns a string. I want to use this string in a thread.

private string Serialno()
{
    if (cbSerials.SelectedValue!=null)
    {
        string serial = cbSerials.SelectedValue.ToString();
        return serial;
    }
    else
    {
        return String.Empty;
    }
}

The thread,

private void CallAdb(string a, string b, string c, int x, int y, FormWindowState windowstate = FormWindowState.Normal)
{
    var filename = "cmd.exe";
    var arguments = "/C " + a + " tools\\adb " + Serialno() + " " + b;
    var startInfo = new ProcessStartInfo
    {
        FileName = filename,
        Arguments = arguments,
        UseShellExecute = false,
        CreateNoWindow = true,
        RedirectStandardOutput = true
    };
    var process = new Process { StartInfo = startInfo };

    process.Start();

    string s = process.StandardOutput.ReadToEnd();

    ToViewer(s, c, x, y, windowstate);
    process.StandardOutput.Dispose();
}

I know the is something like:

if (InvokeRequired)

But after 2 hours searching and trying, I do not get it.

Ninjakannon
  • 3,751
  • 7
  • 53
  • 76
Labo
  • 99
  • 9
  • I don't see anything that is threaded here...? – Ron Beyer May 03 '15 at 16:59
  • I call the CallAdb this way: var thread = new Thread(delegate() { CallAdb(a, b, titel, width, height, windowstate); }); thread.Start(); – Labo May 03 '15 at 17:01
  • And where is the output that you are trying to get, and where do you want to return it to? Is it in the `ToViewer` call? – Ron Beyer May 03 '15 at 17:04
  • are you getting any exceptions when calling Serialno() – faljbour May 03 '15 at 17:07
  • I am trying to get the return value Serialno() and paste it in: var arguments = "/C " + a + " tools\\adb " + **Serialno()** + " " + b; @faljbour: Yes, I am getting a "System.InvalidOperationException", can't accses the ComboBox value from another thread. – Labo May 03 '15 at 17:07

2 Answers2

1

try this,

//* declare a delegate function
public delegate string SerialnoDlg();

//* modify your Serialno this way
public string Serialno()
{
  if (this.InvokeRequired)
  {
    SerialnoDlg dlg = new SerialnoDlg(this.Serialno);
    this.Invoke(dlg);
    return String.Empty;
  } 
  if (cbSerials.SelectedValue!=null)
  {
    string serial = cbSerials.SelectedValue.ToString();
    return serial;
  }
  else
  {
    return String.Empty;
  }
}
faljbour
  • 1,367
  • 2
  • 8
  • 15
  • I get 3 errors: Error 1: Delegate contructor is invoked with 0 arguments. --> SerialnoDlg dlg = new SerialnoDlg(); Error 2: Cannot resolve method 'Invoke(method group)' --> this.Invoke(Serialno); Error 4: Return value is missing. --> return; – Labo May 03 '15 at 17:27
  • Edited the first comment – Labo May 03 '15 at 17:36
  • I made some changes, try again – faljbour May 03 '15 at 17:36
  • Only one error left: Delegate constructor is invoked with 0 arguments. I did it: SerialnoDlg dlg = new SerialnoDlg(); Changed to SerialnoDlg dlg = new SerialnoDlg(Serialno); – Labo May 03 '15 at 17:39
0

There are 2 things in this code:

  1. You call UI-related method in thread code(Serialno()). Instead, add a parameter to your CallAdb() signature and just invoke thread like this: var thread = new Thread(delegate() { CallAdb(a, b, titel, width, height, windowstate, Serialno()); }); thread.Start();
  2. You'll probably need to use InvokeRequired, or rather Invoke() method later in your ToViewer() method. Essentially, when accessing the UI from separate thread in WinForms, you need to this.Invoke(()=>your.code.goes.here();) See this post for more details.
Community
  • 1
  • 1
Mike Makarov
  • 1,287
  • 8
  • 17