在线程中使用返回值

时间:2022-04-04 21:01:42

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.

但经过2个小时的搜索和尝试,我没有得到它。

2 个解决方案

#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;
  }
}

#2


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. 您可以在线程代码(Serialno())中调用与UI相关的方法。相反,在CallAdb()签名中添加一个参数,只需调用这样的线程:var thread = new Thread(delegate(){CallAdb(a,b,titel,width,height,windowstate,Serialno());}); thread.Start();

  3. 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.
  4. 您可能需要稍后在ToViewer()方法中使用InvokeRequired,或者更确切地说使用Invoke()方法。本质上,当从WinForms中的单独线程访问UI时,您需要this.Invoke(()=> your.code.goes.here();)有关更多详细信息,请参阅此文章。

#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;
  }
}

#2


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. 您可以在线程代码(Serialno())中调用与UI相关的方法。相反,在CallAdb()签名中添加一个参数,只需调用这样的线程:var thread = new Thread(delegate(){CallAdb(a,b,titel,width,height,windowstate,Serialno());}); thread.Start();

  3. 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.
  4. 您可能需要稍后在ToViewer()方法中使用InvokeRequired,或者更确切地说使用Invoke()方法。本质上,当从WinForms中的单独线程访问UI时,您需要this.Invoke(()=> your.code.goes.here();)有关更多详细信息,请参阅此文章。