我有一个程序,它使用SDK中的dll(onbon bx led面板)。这段代码的基本思路是我首先用dll设置参数,然后在发送'项目之后设置参数。通过Wi-Fi。代码工作正常,除了它在使用Dll运行部件时冻结,因此我试图让它使用BackgroundWorker组件来保持响应。
所以,我有来自SDK和已知描述函数的dll库:
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();
}
}
以上代码可以正常没有 BGworker。当我发送它5-10次(一个是只更改文本或图像作为参数)我的应用程序冻结并且不会响应直到它完成发送。我的目标是使用BackgroundWorker来保持程序响应。
以下是引用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(); }
....
问题是背景工作者DoWork
什么也没做,结果我看到"剪辑程序被发送"点击按钮后立即。
我仍然无法找到答案。我尝试使用任务和线程,如下所示:
Thread nt = new Thread(() => Invoke(new Action(() => senddd())));
nt.Start();
nt.IsBackground = true;
但没有 - 应用程序仍未响应。
我甚至无法显示gif动画,表明任务正在进行中。我的picturebox元素也被冻结了。
答案 0 :(得分:0)
事实是,我希望我的程序在向控制器发送数据时显示进度(使用wifi)。实际上,我错误地误解了基本概念。
汉斯说,BG是错误的解决方案。事实上,我们不需要 concurency / BG / async / etc来解决这个问题。我刚刚在SDK中找到了委托函数,如果被调用则显示带有文本消息和整数的进度。
这是一种用来完成我的任务的方法:
public delegate void CallBack(string szMessagge,int nProgress);