Start a stream with two parameters

I have a method that is called in an event that presents me two varA, varB variables (both lines). This method is called with new information quite often, so I created a separate method that takes two parameters. I want to run this method in a thread, however, I was struck by the fact that Thread.Start will not accept parameters.

I tried a few suggested methods, but still no luck. I believe that it is best to create a separate class and process it there. However, I have a List that I put in, and got stuck when a separate class tried to access this list, because it was in another class.

Can anyone help me here?

+3
source share
5 answers

The start flow method accepts an object parameter.

If your method accepts several parameters, then you can very well pass an object of your class containing parameters to it. Then you can unzip it into your method.

Thread.start (Object)

http://msdn.microsoft.com/en-us/library/system.threading.thread.start.aspx

Update

In your case, try this,

string varC = varA + "," + varB;
Thread.Start(varC);

and in your method

string args[] = ((string)par).Split(',');
+3
source

A simple solution ...
This will output varA, varB to the console.

new RunTask<string, string>("varA", "varB").StartThread();

public class RunTask<TA, TB>
{
    public TA VarA { get; private set; }
    public TB VarB { get; private set; }

    public RunTask(TA varA, TB varB)
    {
        VarA = varA;
        VarB = varB;
    }
    public void StartThread()
    {
        ThreadPool.QueueUserWorkItem(Worker, this);
    }
    public void Worker(object obj)
    {
        var state = obj as RunTask<TA,TB>;
        Console.WriteLine(state.VarA + ", " + state.VarB);
    }
}

Edit:
, , , , .
, , .
//

public class ListForm : Form
{
    private static readonly object _listResultLock = new object();
    private readonly Action<TaskResult> _listResultHandler;

    public ListForm()
    {
        Load += ListForm_Load;
        _listResultHandler = TaskResultHandler;
    }

    private void ListForm_Load(object sender, EventArgs e)
    {
        new RunTask(new Task("varA", "varB", TaskResultHandler)).StartThread();
    }
    public void TaskResultHandler(TaskResult result)
    {
        if (InvokeRequired)
        {
            Invoke(_listResultHandler, result);
            return;
        }
        lock (_listResultLock)
        {
            // Update List
        }
    }
}

public class Task
{
    public Action<TaskResult> Changed { get; private set; }
    public string VarA { get; private set; }
    public string VarB { get; private set; }

    public Task(string varA, string varB, Action<TaskResult> changed)
    {
        VarA = varA;
        VarB = varB;
        Changed = changed;
    }
}
public class TaskResult
{
    public string VarA { get; private set; }
    public string VarB { get; private set; }

    public TaskResult(string varA, string varB)
    {
        VarA = varA;
        VarB = varB;
    }
}
public class RunTask
{
    private readonly Task _task;

    public RunTask(Task task)
    {
        _task = task;
    }
    public void StartThread()
    {
        ThreadPool.QueueUserWorkItem(Worker, _task);
    }
    public void Worker(object obj)
    {
        var state = obj as Task;
        if (state == null) return;
        if (state.Changed == null) return;
        state.Changed(new TaskResult("this is " + state.VarA, "this is " + state.VarA));
    }
}
+4

, , .

http://msdn.microsoft.com/en-us/library/6x4c42hc.aspx

:

void someFunction()
{
    Thread t = new Thread(doWork);

    t.Start(new int[] { 1, 2 }); //two values passed to the thread.

    t.Start(1); //one value passed to the thread.
}



-

void doWork(object data)
{
    System.Collections.IList list = data as System.Collections.IList;
    if (list != null)
    {
        object[] _objArr = data as object[];
        foreach (object io in list)
        {
            System.Diagnostics.Trace.WriteLine(io);
        }
    }
    else
    {
        System.Diagnostics.Trace.WriteLine(data);
    }
}
+1

-: Thread, System.ComponentModel.BackgroundWorker, ThreadPool. , , ThreadPool ll fit:

        ThreadPool.QueueUserWorkItem(
        (args)=>
            {
                try
                {
                //args - object, you can put in it string[] or
                // whatever you want

                // do work
                }
                catch(Exception e)
                {
                    // dont throw exceptions in this thread
                    // or application will crashed
                }
            }
        );

msdn:

http://msdn.microsoft.com/en-us/library/system.threading.threadpool.aspx

http://msdn.microsoft.com/en-us/library/system.threading.parameterizedthreadstart.aspx

http://msdn.microsoft.com/en-us/library/system.componentmodel.backgroundworker.aspx

0
source

Instead of using, System.Threadyou can use a delegate.

eg:.

public delegate void RunMyMethod(String a, String b);

RunMyMethod myDelegate = new RunMyMethod(MyMethod);
myDelegate.BeginInvoke(someAFromSomeWhere, someBFromSomeWhere);

Where MyMethod is the method you want to run, and the parameters are retrieved from wherever needed.

0
source

Source: https://habr.com/ru/post/1740888/


All Articles