Threading in C #

How to call a function that takes two parameters using streams in C #? I need to call StartDNIThread (string storeID, string queryObject) from another function. I need to pass these two values. But there is a line

+5
source share
6 answers
ThreadStart threadStart = delegate{StartDNIThread(string storeID, string queryObject);};
Thread thread = new Thread(threadStart);
thread.Start();

Or with lambdas:

ThreadStart threadStart = () => StartDNIThread(string storeID, string queryObject);
Thread thread = new Thread(threadStart);
thread.Start();
+6
source

Your options:

  • Encapsulate the parameters in a new class and place the method for use by the delegate in this class.
  • Use anonymous functions (anonymous methods or lambda expressions) to do the same automatically with captured variables.

The latter, of course, is easier. You have not shown what you are doing with the stream, but you can do something like:

string storeID = "...";
string queryObject = "...";

Thread t = new Thread(() => StartDNIThread(storeID, queryObject));
t.Start();

, , , , , . , , :

string storeID = "...";
string queryObject = "...";

string storeIDCopy = storeID;
string queryObjectCopy = queryObject;
Thread t = new Thread(() => StartDNIThread(storeIDCopy, queryObjectCopy));
t.Start();
// You can now change storeID and queryObject freely

, - , . :

foreach (string storeID in stores)
{
    string storeIDCopy = storeID;
    Thread t = new Thread(() => StartDNIThread(storeIDCopy, queryObject));
    t.Start();
}

, .

+8

threadpool:

string str1 = "str1";
string str2 = "str2";
ThreadPool.QueueUserWorkItem(state =>
                            {
                               Console.WriteLine("{0}:{1}", str1, str2);
                            });

, BackgroundWorker.

+2

ParameterizedThreadStart, . , ( tyoe). , ( ), 2 , ParameterizedThreadStart.

:

Thread t = new Thread (new ParameterizedThreadStart (DoWork));
t.Start(new MyType(storeId, queryObject));

-. "", . :

public class Task
{

     private readonly int _storeId;
     private readonly string _queryObject;

     public Task(int storeId, string queryObject)
     {
         _storeId = storeId;
         _queryObject = queryObject;
     }

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

     private void DoWork()
     {
         // Do your thing here.
     }

}
+1

class myClass
{
    public void CallingCode()
    {
        ProcessRequest pr1 = new ProcessRequest("storeD","queryObj");
        ThreadStart ts1 = new ThreadStart(pr1.Go);
        Thread wrk = new Thread(ts1);
        wrk.Start();
    }
}


class ProcessRequest
{
    private string storeD;
    private string queryObj;

    public ProcessRequest(string storeD, string queryObj)
    {
        this.stroreD = storeD;
        this.queryObj = queryObj;
    }

    public void Go()
    {
        try
        {//your processing code here you can access $this->storeD and $this->queryObj

        }
        catch (Exception ex)
        {

        }
    }
}
0

:

private delegate void StartDNIThreadDelegate(string storeID, string queryObject);

private static void Main()
{
    string storeID = "...";
    string queryObject = "...";
    StartDNIThreadDelegate startDNIThread = new StartDNIThreadDelegate(StartDNIThread);
    IAsyncResult result = startDNIThread.BeginInvoke(storeID, queryObject, new AsyncCallback(StartDNIThreadDone), startDNIThread);

    // Do non-threaded stuff...

    result.AsyncWaitHandle.WaitOne(); // wait for thread to finish.
}

private static void StartDNIThread(string storeID, string queryObject)
{
    // Do StartDNIThreading stuff.
}

private static void StartDNIThreadDone(IAsyncResult result)
{
    StartDNIThreadDelegate startDNIThread = (StartDNIThreadDelegate)result.AsyncState;

    // Do after thread finished cleanup.

    startDNIThread.EndInvoke(result);
}
0

All Articles