2

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.

Community
  • 1
  • 1
Elmatsidis Paul
  • 385
  • 1
  • 7
  • 19
  • Did you call **RunWorkerAsync()**? – Lei Yang Oct 26 '16 at 05:38
  • 1
    The 3rd most common threading bug is a *firehose* bug. Happens with BackgroundWorker when you call ReportProgress() too often or force the ProgressChanged event handler to do too much work. Your UI thread will be burning 100% core, trying to keep up. It can't keep up, the side-effect is that the UI will stop painting and responding to input. Until the worker thread finishes and stops slamming the UI, you'll see it get responsive again. Fix a firehose bug by having the UI do less work or by intentionally slowing down the worker thread. – Hans Passant Oct 26 '16 at 07:09
  • Sure, I called RunWorkerAsync(), but still doesnt work... Edited just now for review. For this moment UI thread burns almost 0% after I click button to process my job – Elmatsidis Paul Oct 26 '16 at 09:49
  • First thing to do is empty out backgroundWorker1_DoWork and add a big old `Thread.Sleep(10000)`. Does your UI still lock up? That will be easier to investigate. If not, add stuff back into the method, one line at a time. When it locks up, you'll know where. As to how unmanaged code could lock the UI thread, I have no idea. –  Nov 02 '16 at 13:14
  • How is it possible you have two methods both named `button43_Click()`? – Joel Coehoorn Nov 02 '16 at 13:24
  • sorry, just misclick.updated – Elmatsidis Paul Nov 02 '16 at 13:42
  • And what is `callback`? – H H Nov 02 '16 at 13:47
  • From the looks of it, the Bgw is a solution for the wrong problem. I don't think you need it. – H H Nov 02 '16 at 13:48
  • Just callback function in dll from sdk. public delegate void CallBack(string szMessagge, int nProgress); private CallBack callBack; yeah, probably bgw its not the right...Now I am looking another solution.Appriciate your help and time! – Elmatsidis Paul Nov 02 '16 at 20:32
  • thnx Will, I should put thread.sleep(x) - probably its the main reason! my code has changed, but I will try very soon – Elmatsidis Paul Nov 04 '16 at 06:18
  • It is not uncommon that a large library is sensitive to the apartment state of the thread. Or requires it to have a dispatcher. That makes the thread that BGW uses a hostile place for such a library. That library certainly smells like it has such a restriction, given that it is UI related. [Try this instead](http://stackoverflow.com/a/21684059/17034). Do consider using a telephone, the author always knows. – Hans Passant Nov 14 '16 at 15:15

1 Answers1

0

The truth is that I wanted my program to show progress while sending data to controller(with wifi). Actually it is my mistake of misunderstanding of basic concepts.

As said Hans, BG is wrong solution for this. In fact we do not need concurency/BG/async/etc for this problem. I have just found delegate function in SDK that if called shows progress with text message and integer number.

This was a method was used to complete my task:

public delegate void CallBack(string szMessagge, int nProgress);

Elmatsidis Paul
  • 385
  • 1
  • 7
  • 19