I have a program which uses a dll from an SDK (onbon bx led panel). The basic idea for this code is I first set parameters with the dll and after send 'items' through Wi-fi. The code works fine, except that it freezes up while running the part with the Dlls, and so I'm trying to get it to use a BackgroundWorker component to keep it responsive.
So, I have dll library from SDK and known described functions:
namespace C_Sharp_Demo
{ public partial class Form1 : Form
{
//..
[DllImport("BX_IV.dll")]
public static extern int Initialize();
[DllImport("BX_IV.dll")]
public static extern int DeleteScreen(int nScreenNo);
//..
private void InitializeBackgroundWorker()
{
BackgroundWorker backgroundWorker1 = new BackgroundWorker();
backgroundWorker1.DoWork += backgroundWorker1_DoWork;
backgroundWorker1.ProgressChanged += backgroundWorker1_ProgressChanged;
backgroundWorker1.RunWorkerCompleted += backgroundWorker1_RunWorkerCompleted;
}
}
private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)
{
Initialize();
//GetErrorMessage("Initialize", nResult);
//-------------------------------- Add screen -------------------------------------------------------------
AddScreen(BX_5A1_WIFI, screen_number, 4, width_value, height_value,
nScreenType, 2, SCREEN_DATADA, SCREEN_DATAOE, SCREEN_ROWORDER, SCREEN_FREQPAR, "", SCREEN_BAUD, tbWiFiIp.Text, 5005,
nServerMode, Barcode, NetworkID, tbWiFiIp.Text, 5005, "admin", "888", tbWiFiIp.Text,
(int)numWiFiPort.Value, "", 0, "", "D:\\ScreenStatus.ini", callBack);
AddScreenProgram(screen_number, program_number, 0, 65535, 11, 26, 2011, 11, 26, 1, 1, 1, 1, 1, 1, 1, 0, 0, 23, 59);
AddScreenProgramBmpTextArea(screen_number, program_number, 64, 0, 96, 32);
AddScreenProgramAreaBmpTextText(screen_number, 0, 0, "fasdfasdfa", 1, "Tahoma", 22, 1, // bold
255, 4, // continuesly move left
6, // speed
0);
SendScreenInfo(screen_number, SEND_CMD_SENDALLPROGRAM, 0);
Uninitialize();
}
}
Above code works fine without BGworker. When I send it 5-10 times (one be one changing only text or image as parameter) my application freezes and won't respond until it finishes sending. My goal is to use the BackgroundWorker to keep the program responding.
Here is additional code that references the BackgroundWorker:
private void backgroundWorker1_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
{
if (e.Cancelled)
{
toolStripStatusLabel1.Text = "Task Cancelled.";
}
else if (e.Error != null)
{
toolStripStatusLabel1.Text = "Error while performing operation.";
}
else
{
toolStripStatusLabel1.Text = "Clip program is sent";
}
this.button12.Text = "Send Program";
}
private void button43_Click(object sender, EventArgs e)
{ backgroundWorker1.RunWorkerAsync(); }
....
The thing is that backgroundworker's DoWork
does nothing and as result I see "Clip program is sent" immediately after clicking the button.
UPDATE 02/11/16
I still cant find an answer. I tried using Tasks and Threads like so:
Thread nt = new Thread(() => Invoke(new Action(() => senddd())));
nt.Start();
nt.IsBackground = true;
but nothing - Applications continues to not respond.
I cant even show gif animation showing that a task is in process. My picturebox element is frozen, too.