---
title: "How to handle 'Cross thread exception' in winforms"  
description: "How to handle 'Cross thread exception' in winforms"  
author: "Anupam Mishra"  
published: 2016-01-18  
updated: 2016-01-19  
canonical: https://www.mindstick.com/forum/33892/how-to-handle-cross-thread-exception-in-winforms  
category: "c#"  
tags: ["c#", ".net"]  
reading_time: 1 minute  

---

# How to handle 'Cross thread exception' in winforms

I have created one **[listview](https://www.mindstick.com/articles/12744/listview-in-android)** and display [items](https://www.mindstick.com/forum/33812/getting-number-of-items-selected-in-uicollectionview-in-ios) with the using **BackgroundWorker** thread , But it will thrown an '[Cross](https://www.mindstick.com/forum/156081/what-is-cross-linking-what-is-the-function-of-crosslinking) [thread exception](https://answers.mindstick.com/qa/33375/we-are-writing-an-android-app-which-loads-some-data-from-remote-database-using-a-url-while-doing-so-we-encountered-network-on-main-thread-exception-what-did-we-do-wrong-and-how-can-we-handle-it) ' in **winforms**.Please give me a solution.\
thank you.

## Replies

### Reply by Anupam Mishra

As we say i am using a **BackgroundWorker** thread in a winforms and try to add item in **DoWork** method. So, for this situation we use code similar like this. it is working:\

```
  private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)    {        var test = new ListViewItem("item");        backgroundWorker1.ReportProgress(0, test);     }  private void backgroundWorker1_ProgressChanged(object sender, ProgressChangedEventArgs e)    {        listView1.Items.Add((ListViewItem)e.UserState);    }
```

**or**

```
// avoid exception 'Cross thread exception'  if we using this

ListViewItem _item = new ListViewItem();     if (listView1.InvokeRequired)                    listView1.Invoke(new MethodInvoker(delegate                    {                       listView1.Items.Add(_item);                               }));                else          listView1.Items.Add(_item);
```


---

Original Source: https://www.mindstick.com/forum/33892/how-to-handle-cross-thread-exception-in-winforms

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
