简体   繁体   English

从主线程中调用一个函数并使它与主线程中的一个函数并行工作

[英]Calling a function from the main thread and making it work in parallel with a function in the main thread

I really need your help in my issue very quickly and it's too close to the previous issue. 我真的非常需要您的帮助来解决我的问题,而这与上一期太过接近了。 I want to call a function using thread from the main thread. 我想使用主线程中的线程来调用函数。

for more explanaition, here is a sample from my code: // please look to the code begining from ( //------- ) 有关更多说明,请参见我的代码示例://请查看以(// -------开头的代码

    // 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 update on a Tree which is in the main thread Form1 (GUI) and when when I tried to use thread ( thread _t = new Thread(new ThreadStart(GetFromServer))) this error appears: " Action being performed on this control is being called from the wrong thread. Marshal to the correct thread using Control.Invoke or Control.BeginInvoke to perform this action. " ** **注意:在GetFromServer和GetFromPC中,我将在主线程Form1(GUI)中的树上进行更新,当我尝试使用线程时(线程_t = new Thread(new ThreadStart(GetFromServer))),将出现以下错误: “正在从错误的线程调用对此控件执行的操作。使用Control.Invoke或Control.BeginInvoke将其编组到正确的线程以执行此操作。” **

I hope that I explained my problem well. 我希望我能很好地解释我的问题。

Thanks in advance. 提前致谢。

You can't do operations on the UI from a different thread - but you could make GetFromServer and GetFromPC fetch the data on the other thread and then call Control.Invoke to get back to the UI thread to update the treeview. 您不能从其他线程对UI进行操作-但可以使GetFromServer和GetFromPC 提取另一个线程上的数据,然后调用Control.Invoke返回UI线程以更新树视图。

See my threading article for example of doing background work in another thread and then marshalling back. 有关在另一个线程中进行后台工作然后编组回去的示例,请参见我的线程文章 The article was written pre-C#2, so these days it's slightly less clunky. 这篇文章是在C#2之前编写的,所以如今这些东西已经不太笨拙了。 You can also use BackgroundWorker to make it easier to report progress etc. 您还可以使用BackgroundWorker使其更易于报告进度等。

For any single operation that only fetches data and then updates GUI and then its done till next time someone invokes it, use BackgroundWorker. 对于仅获取数据然后更新GUI,然后完成直到下次有人调用它的任何单个操作,请使用BackgroundWorker。

Its a class that has simplified thread coding for you. 它是为您简化线程编码的类。 That way you just write the code for fetching data in one function, and one method that displays the fetched data afterwards. 这样,您只需在一个函数中编写用于获取数据的代码,然后编写一种在之后显示获取的数据的方法。

Then just call it whenever data is to be fetched. 然后只要要提取数据就调用它。

BackgroundWorker is superb for any GUI solution where you click a button that does something in the background and updates something afterwards. 对于任何GUI解决方案,BackgroundWorker都是一流的,您只需单击一个按钮即可在后台执行某些操作,然后再进行更新。 It also supports updating the GUI as it fetches data. 它还支持在获取数据时更新GUI。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM