-1
catch (Exception ex)\\error
{
    clsLogs.LogError("Error: " + ex.Message + this.Name + " || ImportData");
    result = false;    
}

;Cross-thread operation not valid: Control 'cmbDeviceName' accessed from a thread other than the thread it was created on

Soner Gönül
  • 97,193
  • 102
  • 206
  • 364

2 Answers2

0

You can do it like this using Invoke:

this.Invoke((MethodInvoker)delegate()
    {
        //// your code 
    });
Neel
  • 11,625
  • 3
  • 43
  • 61
  • thnx it's work...but i use this code in when i click button then one method execute and then parallel progress bar open..when method execute finish then progressbar stop.. – user3959583 Aug 21 '14 at 08:46
  • so problem is that..button click event progressbar open but before execute method progressbar finish..any suggestion? – user3959583 Aug 21 '14 at 08:48
  • sorry for being late..is it working now? @user3959583 – Neel Aug 21 '14 at 10:28
  • but problem is that progressbar first start and then method execut.. – user3959583 Aug 21 '14 at 10:40
  • what is progressbar I mean any user control in ur application or any default progressbar @user3959583? – Neel Aug 21 '14 at 10:42
  • I cant judge without looking at ur code – Neel Aug 21 '14 at 10:42
  • void bgw_DoWork(object sender, DoWorkEventArgs e) { for (int i = 1; i <= 100; i++) { System.Threading.Thread.Sleep(100); bgw.ReportProgress(i); } e.Result = ImportData(); } importdata is method execute for parallel progressbar..but it's not work.. – user3959583 Aug 21 '14 at 10:49
0
class YourForm : Form
{
    private SynchronizationContext synchronizationContext ;

    public YourForm()
    {
        this.synchronizationContext  = SynchronizationContext.Current;
        //the rest of your code
    }
}

and then, when you need to do some thread-unsafe form actualizations you should use something like this:

synchronizationContext.Send(new SendOrPostCallback( 
    delegate(object state) 
    {    
        textBoxOut.Text = "New text";
    } 
), null);

source codeproject

Nick
  • 4,192
  • 1
  • 19
  • 30
  • thnx it's work...but i use this code in when i click button then one method execute and then parallel progress bar open..when method execute finish then progressbar stop.so problem is that..button click event progressbar open but before execute method progressbar finish..any suggestion? – user3959583 Aug 21 '14 at 09:01