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: