C # stream functions

Can i make any function like

public void myfunc()
{
   //some processing
}

stream function using

Thread t = new Thread (new ThreadStart (myfunc));

then some where

t.Start();

and can I pass any arguments for this?

+3
source share
3 answers

In theory, you can make any method run in a separate thread if you follow some rules (for example, synchronization, calling delegates to update ui, etc.).

From your question, I understand that you have little experience in multithreaded programming, so I advise you to read a lot about thread information and learn about the dangers and problems that may arise. You can also use the desktop class, which takes on some responsibilities.

, , :

private class ThreadParameters
{
   ....
}

...

public void ThreadFunc(object state)
{
    ThreadParameters params = (ThreadParameters)state;
    ....
}

Thread t = new Thread(new ParameterizedThreadStart(ThreadFunc));
t.Start(new ThreadParameters() { ... });
+6

, , IMO - threadstart ( ) :

int a = ...
string b = ...
Thread t = new Thread (delegate() { SomeFunction(a,b);});

( ) - * a b , ( ) - 'do do:

int a = ...
string b = ...
Thread t = new Thread (delegate() { SomeFunction(a,b);});
a = 12; // this change will be visible to the anonymous method - be careful ;-p

( ) ;

    int[] data = {1,2,3,4,5};
    foreach(int i in data) {
        ThreadPool.QueueUserWorkItem(delegate {
            Console.WriteLine(i); });
    }
    Console.ReadLine();

(, , 5,5,5,5,5)

    int[] data = {1,2,3,4,5};
    foreach (int i in data) {
        int tmp = i;
        ThreadPool.QueueUserWorkItem(delegate {
            Console.WriteLine(tmp); });
    }
    Console.ReadLine();

( 1-5 )


, Meeh (); (99.999% - )?

    string s = "dreams";
    ThreadPool.QueueUserWorkItem(delegate {
        Console.WriteLine(s);
    });
    s = "reality";
    Console.ReadLine();
+7
protected void Page_Load(object sender, EventArgs e)
    {
        if (Session["intCompany_Accounting_Year_ID"] == null || Session["vcrAdmin_Id"] == null)
        {
            Response.Redirect("User_Login.aspx");
        }

        if (!IsPostBack)
        {
            ViewState["Page_Index"] =  Request.QueryString["Page_Index"];
            ViewState["ID_Field"] = Request.QueryString["ID_Field"];
            Initialize_Page();                
            Bind_Search_Grid();                
        }
    }
    protected void btnFind_Click(object sender, EventArgs e)
    {
        Bind_Search_Grid();
    }                               




protected void grdSearch_Result_RowDataBound(object sender, GridViewRowEventArgs e)
{
    if (e.Row.RowType == DataControlRowType.Header)
    {
        Label lblHeader = (Label)e.Row.FindControl("lblHeader");
        lblHeader.Text = ViewState["Header_Name"].ToString();
    }
    else if (e.Row.RowType == DataControlRowType.DataRow)
    {
        LinkButton lnk = (LinkButton)e.Row.FindControl("lnkDisplay_Text");
        lnk.OnClientClick = "Close_Window('" + lnk.CommandArgument + "','" + ViewState["ID_Field"].ToString() 
            + "')";
    }

}
protected void grdSearch_Result_RowCreated(object sender, GridViewRowEventArgs e)
{
    e.Row.Cells[1].Visible = false;
    e.Row.Cells[2].Visible = false;
    e.Row.Cells[3].Visible = false;
}

}
+1

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


All Articles