传递线程之间的数据线程、数据

2023-09-04 06:26:49 作者:━━━╋う 妷控

我有以下的code,在此我想处理大量的数据,并更新了用户界面。我试着用一个背景工人同样的事情,但我得到一个类似的问题。这个问题似乎是,我试图使用未初始化的新线程类(实际的错误是,当前线程没有自己的实例)。我的问题是,有没有我可以线程之间传递这个实例的方式来避免这个错误?

I have the following code, in which I’m trying to process a large amount of data, and update the UI. I’ve tried the same thing using a background worker, but I get a similar issue. The problem seems to be that I’m trying to use a class that was not instantiated on the new thread (the actual error is that the current thread doesn't "own" the instance). My question is, is there a way that I can pass this instance between threads to avoid this error?

DataInterfaceClass dataInterfaceClass = new DataInterfaceClass();

private void OutputData(List<MyResult> Data)
{            
    progressBar1.Maximum = Data.Count;
    progressBar1.Minimum = 1;
    progressBar1.Value = 1;

    foreach (MyResult res in Data)
    {                       
        // Add data to listview
        UpdateStatus("Processing", res.Name);

        foreach (KeyValuePair<int, string> dets in res.Details)
        {
            ThreadPool.QueueUserWorkItem((o) =>
            {
                // Get large amount of data from DB based on key
                // – gives error because DataInterfaceClass was
                // created in different thread.
                MyResult tmpResult = dataInterfaceClass
                    .GetInfo(dets.DataKey);

                if (tmpResult == null)
                {
                    // Updates listview
                    UpdateStatus("Could not get details", 
                        dets.DataKey); 
                }
                else
                {
                    UpdateStatus("Got Details", dets.DataKey);
                }

                progressBar1.Dispatcher.BeginInvoke(
                    (Action)(() => progressBar1.Value++));
            });
        }
    }
}

编辑:

DataInterfaceClass实际上definated并且它使用的功能之外创建的,但它是一个实例,而不是静态的。

DataInterfaceClass is actually definated and created outside of the function that it is used in, but it is an instance and not static.

推荐答案

更新: 你似乎已经修改了贴源$ C ​​$ C,所以......

UPDATE: You seem to have modified the posted source code, so...

您应该专门为每个后台线程或任务创建DataInterfaceClass的一个实例。有足够的输入提供你的任务是创建自己的实例。

You should create an instance of the DataInterfaceClass exclusively for each background thread or task. Provide your task with enough input to create its own instance.

这就是说,如果您尝试访问在一个数据库中的一个高度并行的双向数据,这可能会导致数据库超时。即使你能得到你的数据访问在多线程的方式工作,我会建议的发生限制的同时后台任务数prevent这一点。

That being said, if you try to access data in a single database in a highly parallel way, this might result in database timeouts. Even if you can get your data access to work in a multithreaded way, I would recommend limiting the number of simultaneous background tasks to prevent this from occurring.

您可以使用 信号量 的(或类似的),以确保不超过一定量的任务更在同一时间都在运行。

You could use a Semaphore (or similar) to ensure that no more than a certain amount of tasks are running at the same time.