Click here to Skip to main content
15,884,010 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Merhaba,

Bir tane backgroundworker nesnesi kullanarak form üzerinde bulunan bir treeview' e database' den veri yüklüyorum. DoWork' un yapısı,

delegate void delege(object sender, System.ComponentModel.DoWorkEventArgs e);

private void backgroundWorker1_DoWork(object sender, System.ComponentModel.DoWorkEventArgs e)
{

if (treeView1.InvokeRequired)
{
delege Yeni_delege = new delege(backgroundWorker1_DoWork);
treeView1.Invoke(Yeni_delege, new object[] { null, null });
return;
}
foreach(TreeNode in nodes treeView1.Nodes) { kodlar }

}
şeklinde. Ancak form donuyor. Yani backgroundworker kullanmam donmayı çözmüyor. Invoke yerine;

CheckForIllegalCrossThreadCalls = false;

kullandığımda sorun çözülüyor ancak ben bu şekilde yapmak istemiyorum. Yani unsafe kod olmasını istemiyorum.

Bu konuda yardımcı olabilecek arkadaşlara şimdiden teşekkür ederim.
Posted
Comments
OriginalGriff 2-Oct-13 5:38am    
I'm sure English is not your native language, but it is the default language for this site.
In English, your question makes no sense at all.
Please, either try to find a translation of your question to English, or find a site in your own native language, as they may be able to help you better than we can!
Use the "Improve question" widget to edit your question and provide better information.
ncaewar 2-Oct-13 5:42am    
I don't want to use "CheckForIllegalCrossThreadCalls = false". But it is working. "Invoke" is not working (Form is not responding)

what is wrong?

delegate void delege(object sender, System.ComponentModel.DoWorkEventArgs e);

private void backgroundWorker1_DoWork(object sender, System.ComponentModel.DoWorkEventArgs e)
{

if (treeView1.InvokeRequired)
{
delege Yeni_delege = new delege(backgroundWorker1_DoWork);
treeView1.Invoke(Yeni_delege, new object[] { null, null });
return;
}
foreach(TreeNode in nodes treeView1.Nodes)
{
................
}

}

I know that isn't your actual code (because it won't compile - you mispelled "delegate")
And I'm not sure you want to do that anyway, because the whole idea of Invoke is to move code back onto the UI thread so you can access controls - and that means that your background worker just shifts the execution right back onto the UI thread immediately (since a control in a BackgroundWorker will always return true for InvokeRequired unless it has been moved to the UI thread).

So your UI thread freezing doesn't surprize me too much.

I am assuming that you are trying to walk through a treeview and do some work on each node - and it takes time, probably because there are a lot of nodes...but you can't use use a BackgroundWorker and Invoke as "some kind of magic solution" because it won't work. If you want to use a different thread, then you need to pass it the info from the TreeView without it accessing the treeview, and only get your UI thread to reflect updates it needs to make.

You can do it by invoking the individual access parts and leaving the bulk of the code in the background worker, but that may be more trouble than doing it "properly" in the first place.
 
Share this answer
 
Comments
ncaewar 2-Oct-13 6:46am    
Thanks for answer
OriginalGriff 2-Oct-13 6:56am    
You're welcome!
Form is not responding....

C#
using System;
using System;
using System.Windows.Forms;

namespace WindowsFormsApplication1
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();

            for (int i = 0; i < 1000; i++) treeView1.Nodes.Add(i.ToString());
        }

        private void button1_Click(object sender, EventArgs e)
        {
            backgroundWorker1.RunWorkerAsync();
        }

        private void backgroundWorker1_DoWork(object sender, System.ComponentModel.DoWorkEventArgs e)
        {
            treeView1.Invoke((MethodInvoker)delegate
                {
                    foreach (TreeNode node in treeView1.Nodes)
                    {
                        for (int j = 0; j < 1000; j++) node.Text = (j * j).ToString();
                    }
                });
        }
    }
}


There is no problem.

C#
using System;
using System.Windows.Forms;

namespace WindowsFormsApplication1
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();

            CheckForIllegalCrossThreadCalls = false;

            for (int i = 0; i < 1000; i++) treeView1.Nodes.Add(i.ToString());
        }

        private void button1_Click(object sender, EventArgs e)
        {
            backgroundWorker1.RunWorkerAsync();
        }

        private void backgroundWorker1_DoWork(object sender, System.ComponentModel.DoWorkEventArgs e)
        {
            foreach (TreeNode node in treeView1.Nodes)
            {
                for (int j = 0; j < 1000; j++) node.Text = (j * j).ToString();
            }
        }
    }
}


But I don't want to use "CheckForIllegalCrossThreadCalls = false"
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900