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

I'm a beginner and just need some help solving this problem.

I think its simple but have tried to figure it out but im getting no where.
I currently have ArrayList arry_eventss to which
[0] 1998397003
[1] 2

I add data to the listview.
but error in this line
error : System.InvalidOperationException: 'Cross-thread operation not valid: Control 'listView1' accessed from a thread other than the thread it was created on.'

listView1.Items.Add(ftrs.ToString());


i want to output like that

TYPE | STATUS
1998397003 | 2
4548484541 | 5

What I have tried:

public void CB_SubscribeEvent(PLAT_SUBSCRIBE_EVENT_V20 pstEvent, IntPtr pUser)
        {
           ArrayList arry_eventss = new ArrayList();

           arry_eventss.Add(pstEvent.iState);
           arry_eventss.Add(pstEvent.iEventType);
           int c_arry_events = arry_eventss.Count;

           string[] ftrs = new string[c_arry_events];
           for (int j = 0; j < c_arry_events; j++)
           {

               listView1.Items.Clear();

               ftrs[j] = arry_eventss[j].ToString();

           }
           listView1.Items.Add(ftrs.ToString());
         }
Posted
Updated 14-Mar-18 2:56am
v3

1 solution

If you are using Windows Forms
you should read this article:
Using the Invoke Design Pattern with Anonymous Methods[^]

e.g.
Invoke(new MethodInvoker(
        delegate { listView1.Items.Add(ftrs.ToString()); }
    ));
}
if you are using WPF you should read this:
Update a WPF UI from Another Thread – Stephen Haunts { Coding in the Trenches }[^]

e.g.
Application.Current.Dispatcher.Invoke(() =>
{
    listView1.Items.Add(ftrs.ToString());
});
 
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