سؤال

لا أحد يعرف كيفية تمرير معلمات متعددة في الموضوع.بداية الروتين ؟

فكرت في توسيع الطبقة ، ولكن ال C# الخيط فئة غير مختومة.

هنا هو ما أعتقد التعليمات البرمجية تبدو مثل:

...
    Thread standardTCPServerThread = new Thread(startSocketServerAsThread);

    standardServerThread.Start( orchestrator, initializeMemberBalance, arg, 60000);
...
}

static void startSocketServerAsThread(ServiceOrchestrator orchestrator, List<int> memberBalances, string arg, int port)
{
  startSocketServer(orchestrator, memberBalances, arg, port);
}

راجع للشغل, أبدأ عدد من المواضيع مع مختلف منسقين ، وأرصدة والموانئ.يرجى النظر في موضوع السلامة أيضا.

هل كانت مفيدة؟

المحلول

وحاول استخدام تعبير لامدا للقبض على الحجج.

Thread standardTCPServerThread = 
  new Thread(
    unused => startSocketServerAsThread(initializeMemberBalance, arg, 60000)
  );

نصائح أخرى

وهنا هو جزء من التعليمات البرمجية التي تستخدم نهج كائن صفيف المذكورة هنا بضع مرات.

    ...
    string p1 = "Yada yada.";
    long p2 = 4715821396025;
    int p3 = 4096;
    object args = new object[3] { p1, p2, p3 };
    Thread b1 = new Thread(new ParameterizedThreadStart(worker));
    b1.Start(args);
    ...
    private void worker(object args)
    {
      Array argArray = new object[3];
      argArray = (Array)args;
      string p1 = (string)argArray.GetValue(0);
      long p2 = (long)argArray.GetValue(1);
      int p3 = (int)argArray.GetValue(2);
      ...
    }>

وتحتاج إلى التفاف عليها في كائن واحد.

وجعل فئة مخصصة لتمرير في المعلمات الخاص بك هو خيار واحد. يمكنك أيضا استخدام صفيف أو قائمة الكائنات، وتعيين كافة المعلمات الخاصة في ذلك.

استخدم نمط 'العمل':

public class MyTask
{
   string _a;
   int _b;
   int _c;
   float _d;

   public event EventHandler Finished;

   public MyTask( string a, int b, int c, float d )
   {
      _a = a;
      _b = b;
      _c = c;
      _d = d;
   }

   public void DoWork()
   {
       Thread t = new Thread(new ThreadStart(DoWorkCore));
       t.Start();
   }

   private void DoWorkCore()
   {
      // do some stuff
      OnFinished();
   }

   protected virtual void OnFinished()
   {
      // raise finished in a threadsafe way 
   }
}

و. NET 2 تحويل JaredPar الجواب

Thread standardTCPServerThread = new Thread(delegate (object unused) {
        startSocketServerAsThread(initializeMemberBalance, arg, 60000);
    });

وأنت لا تستطيع ذلك. إنشاء كائن التي تحتوي على بارامس التي تحتاج إليها، وتمرير غير ذلك. في وظيفة موضوع يلقي الكائن مرة أخرى إلى نوعه.

void RunFromHere()
{
    string param1 = "hello";
    int param2 = 42;

    Thread thread = new Thread(delegate()
    {
        MyParametrizedMethod(param1,param2);
    });
    thread.Start();
}

void MyParametrizedMethod(string p,int i)
{
// some code.
}

كنت أقرأ لك المنتدى لمعرفة كيفية القيام بذلك و أنا فعلت هذا بهذه الطريقة قد تكون مفيدة لشخص ما.مررت من الحجج في منشئ مما يخلق لي موضوع العمل التي سيتم تنفيذها لي طريقة - تنفيذ() الأسلوب.

 using System;

using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Diagnostics;
using System.Windows.Forms;
using System.IO;
using System.Threading;
namespace Haart_Trainer_App

{
    class ProcessRunner
    {
        private string process = "";
        private string args = "";
        private ListBox output = null;
        private Thread t = null;

    public ProcessRunner(string process, string args, ref ListBox output)
    {
        this.process = process;
        this.args = args;
        this.output = output;
        t = new Thread(new ThreadStart(this.execute));
        t.Start();

    }
    private void execute()
    {
        Process proc = new Process();
        proc.StartInfo.FileName = process;
        proc.StartInfo.Arguments = args;
        proc.StartInfo.UseShellExecute = false;
        proc.StartInfo.RedirectStandardOutput = true;
        proc.Start();
        string outmsg;
        try
        {
            StreamReader read = proc.StandardOutput;

        while ((outmsg = read.ReadLine()) != null)
        {

                lock (output)
                {
                    output.Items.Add(outmsg);
                }

        }
        }
        catch (Exception e) 
        {
            lock (output)
            {
                output.Items.Add(e.Message);
            }
        }
        proc.WaitForExit();
        var exitCode = proc.ExitCode;
        proc.Close();

    }
}
}

ويمكنك أن تأخذ مجموعة كائن وتمريرها في الموضوع. ممر

System.Threading.ParameterizedThreadStart(yourFunctionAddressWhichContailMultipleParameters) 

وإلى منشئ الموضوع.

yourFunctionAddressWhichContailMultipleParameters(object[])

وأنت بالفعل تعيين كل قيمة في objArray.

وتحتاج إلى abcThread.Start(objectArray)

هل يمكن أن الكاري وظيفة "العمل" مع التعبير امدا:

public void StartThread()
{
    // ...
    Thread standardTCPServerThread = new Thread(
        () => standardServerThread.Start(/* whatever arguments */));

    standardTCPServerThread.Start();
}

تحتاج إلى تمرير كائن واحد ، ولكن إذا كان الكثير من المتاعب إلى تحديد الخاصة بك كائن واحد ، يمكنك استخدام Tuple.

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top