Calling a function from the main thread and running it in parallel with the function in the main thread

I really need your help on my problem very quickly and this is too close to the previous problem. I want to call a function using a thread from the main thread.

for a more detailed explanation, here is a sample from my code: // look at the code starting with (// -------)

    // this function is being called from the Form1 class Like this 
    //(this.Load += new System.EventHandler(this.Form1_Load);)

    private void Form1_Load(object sender, EventArgs e)
    {
        Common.MainForm = this;

        ftpServerIP = "74.220.215.77/ASGAQuraan";
        ftpUserID = "sanbouk@asgatech.com";
        ftpPassword = "asga_root";

        iStartConnection = true;
        iGetNarratorData = false;
        iGetNarratorsSuras = false;
        _isExpandedIndecies = new string[10];

        refreshPhons = true;
        count = 0;
        _btnDownload2PC.Enabled = false;
        _btnDownload2Phone.Enabled = false;

        //--------------------------------------------------------------------------------------
        //timer1.Tick() is a function which Gets Data rom Phone
        //Now, GetFromServer, and GetFromPC are 2 functions which i want to them 
        // to work in paralel with Timer1.Tick()
        //So, Fincally i want all 3 function work together with no gabs

         Timer1.Enabled = true;
        Timer1.Start();
        if (InvokeRequired)
        {
            Invoke(new GetFromServerHandler(GetFromServer));
        }
        else
        {
            ServerQuranTreeView.Nodes.Clear();
            GetFromServer();
            GetFromPC();
        }
    }

      

** NOTE: in GetFromServer and GetFromPC I will be updating the tree which is in the main thread of Form1 (GUI) and when I tried to use thread (thread _t = new Thread (new ThreadStart (GetFromServer))) this error appears: "Action performed over this control is called from the wrong thread. Marshal to the correct thread using Control.Invoke or Control.BeginInvoke to do this. " **

Hope I have explained my problem well.

Thanks in advance.

+1


source to share


2 answers


You cannot perform UI operations from another thread, but you can force GetFromServer and GetFromPC to fetch data from another thread and then call Control.Invoke to return to the UI thread to update the treeview.



See my article on threads , for example, for how to do a background job on another thread and then sort back. The article was written before C # 2, so it's a little less clumsy these days. You can also use BackgroundWorker to make it easier to report on progress, etc.

+4


source


For any single operation that only fetches data and then updates the GUI and then executes it until the next time someone calls it, use BackgroundWorker.

Its a class that makes it easy to code streams for you. Thus, you simply write code to retrieve data in one function and one method that displays the retrieved data afterwards.



Then just call it whenever the data is fetched.

BackgroundWorker is great for any GUI solution where you press a button that does something in the background and refreshes something afterwards. It also supports updating the GUI when it fetches data.

+1


source







All Articles