Home > Net >  BackgroundWorker controls incredibly error "invalid operation between threads"
BackgroundWorker controls incredibly error "invalid operation between threads"

Time:09-18

Debugging today call DLL written in labview, found that the loading time is very long, decisions will be implemented first invoked in backgroundWorker controls, at the same time the last step in backgroundWorker dowork wrote a. For the Enabled=true, debugging code has reported across threads access, and running exe is not an error, put that code in backgroundWorker RunWorkerCompleted nor an error, will seek to solve
 
using System;
using System.Collections.Generic;
Using System.Com ponentModel;
Using System. The Data;
Using System. Diagnostics;
Using System. Drawing;
using System.Linq;
Using System. The Runtime. InteropServices;
using System.Text;
Using System. The Threading;
Using System. Windows. Forms;

The namespace WindowsFormsApplication1
{
Public partial class Form1: Form
{
Public _click ()
{

InitializeComponent ();
For the. Enabled=false;
BackgroundWorker1. RunWorkerAsync ();
//backgroundWorker1. RunWorkerCompleted +=BackgroundWorker1_RunWorkerCompleted;
}

//private void BackgroundWorker1_RunWorkerCompleted (object sender, RunWorkerCompletedEventArgs e)
//{
//for the Enabled=true;
//}

Private void button1_Click (object sender, EventArgs e)
{
Text=labview. The ADD (30, 20.5). The ToString ();
}

Private void backgroundWorker1_DoWork (object sender, DoWorkEventArgs e)
{
Try
{
For the. Enabled=false;
Labview. The ADD (10, 20);
For the Enabled=true;
}
The catch (Exception ex)
{
MessageBox. Show (ex. ToString ());
}
}
}
The class labview
{
[DllImport (" SharedLib. DLL, "EntryPoint=" ADD ", CharSet=CharSet. Auto, CallingConvention=CallingConvention. Cdecl)]//every function is derived with this paragraph
Public static extern double ADD (double a, double b);
}
}

CodePudding user response:

Cross thread calls, you need to use the Invoke (synchronous) or the BeginInvoke (asynchronous)

CodePudding user response:

Dowork the inside of the code is another background thread execution, don't go to call the main thread of the UI, finished to perform should RunWorkerCompleted interface changes, this is the main thread,

Execute exe error, either automatically optimize the across threads is not an error, maybe automatic CheckForIllegalCrossThreadCalls=true, unclear mechanism, or simply not compiled for asynchronous,

CodePudding user response:

refer to the second floor HZTLTGG response:
dowork the inside of the code is another background thread execution, don't go to call the main thread of the UI, finished to perform should RunWorkerCompleted interface changes, this is the main thread,

Execute exe error, either automatically optimize the across threads is not an error, maybe automatic CheckForIllegalCrossThreadCalls=true, unclear mechanism, or simply not compiled for asynchronous,

I remove the DoWork call DLL code inside, also won't complain,

CodePudding user response:

Who has examined the MSDN, DoWork below this

CodePudding user response:

 
Invoke (new EventHandler (delegate
{
For the Enabled=true;
}));

CodePudding user response:

reference 1st floor zijiang001 response:
cross thread calls, you need to use the Invoke (synchronous) or (asynchronous) the BeginInvoke

Always thought DoWork can be used across threads

CodePudding user response:

refer to 6th floor _stephen_chow response:
thought DoWork is across threads can be used by the


Can the RunWorkerCompleted events and update the results to the UI, without having to Invoke,
Under the ProgressChanged events, also need not Invoke,
  •  Tags:  
  • C#
  • Related