c# - Cross-Threading issue with listView -


ok before post duplicate let me inform have looked @ other post , im still lost use delegates or background worker etc... how make thread safe want delete files on own thread.

here code working with.

private void button1_click(object sender, eventargs e) {     cleanfiles.runworkerasync(); }  private void cleanfiles_dowork(object sender, doworkeventargs e) {     if (listview1.checkeditems.count != 0)     {         // if so, loop through checked files , delete.         (int x = 0; x <= listview1.checkeditems.count - 1; x++)         {             string tempdirectory = path.gettemppath();             foreach (listviewitem item in listview1.checkeditems)             {                 string filename = item.text;                 string filepath = path.combine(tempdirectory, filename);                  try                 {                     file.delete(filepath);                 }                 catch (exception)                 {                     //ignore files being in use                 }             }         }         paintlistview(tfile);         messagebox.show("files removed");         toolstripstatuslabel1.text = ("ready");     }     else     {         messagebox.show("please put check files want delete");     } } 

as reed mentioned, cannot access ui elements thread other ui thread itself. so, you'll have pass on delegate control.invoke() executed ui thread, this

try

    private void cleanfiles_dowork(object sender, doworkeventargs e)     {         if (listview1.checkeditems.count != 0)         {             // if so, loop through checked files , delete.             (int x = 0; x <= listview1.checkeditems.count - 1; x++)             {                 string tempdirectory = path.gettemppath();                 foreach (listviewitem item in listview1.checkeditems)                 {                     string filename = item.text;                     string filepath = path.combine(tempdirectory, filename);                      try                     {                         file.delete(filepath);                     }                     catch (exception)                     {                         //ignore files being in use                     }                 }             }              paintlistviewandsetlabel();         }         else         {             showmessagebox();         }     }      private void showmessagebox()     {         if(invokerequired)         {             this.invoke(new action(showmessagebox), new object[0]);             return;         }         messagebox.show("please put check files want delete");     }      private void paintlistviewandsetlabel()     {         if (invokerequired)         {             this.invoke(new action(paintlistviewandsetlabel),new object[0]);             return;         }         paintlistview(tfile);         messagebox.show("files removed");         toolstripstatuslabel1.text = ("ready");     } 

Comments

Popular posts from this blog

php - What is the difference between $_SERVER['PATH_INFO'] and $_SERVER['ORIG_PATH_INFO']? -

fortran - Function return type mismatch -

queue - mq_receive: message too long -