Hi
I am trying to manage an external dll.
I would like to create an assembly (later exported in a dll) that encapsulates the management of such dll so that it is "easily" usable (I hope!)
I have a two different problems so I will split them into different post:
first problem: thread safe
my assembly exports a class that manages the external dll
one function of a dll need a callback function as a parameter (in particular one function of the class)
No problem, I use a delegate approach and the call back function is correctly called.
The problem is that such call is not considered at the same thread level of the form that instantiates the class so that if I want to signal the form the raising of such function I have problems touching controls of the forum.
I explain better
The funny thing is that I don't receive any run time error as I received some month ago writing a textbox in a timer or in a serial port event manager
In reality, I receive nothing and the text box is not written
Solution found
My question is: do you know any solution that allows me to use the first approach?
How can I transform such tread unsafe class into a thread safe
I have tried to use a delegate approach (the one shown) and also an event approach creating a class inherited form EventArgs and so on but I had the same problem
I have also tried to inherit my class form INotifyPropertyChanged with no results
Thank you very much
Davide
I am trying to manage an external dll.
I would like to create an assembly (later exported in a dll) that encapsulates the management of such dll so that it is "easily" usable (I hope!)
I have a two different problems so I will split them into different post:
first problem: thread safe
my assembly exports a class that manages the external dll
one function of a dll need a callback function as a parameter (in particular one function of the class)
No problem, I use a delegate approach and the call back function is correctly called.
The problem is that such call is not considered at the same thread level of the form that instantiates the class so that if I want to signal the form the raising of such function I have problems touching controls of the forum.
I explain better
Code:
public delegate void DelFuncCallBack(... some parameters ...);
public delegate void Signal();
/// dll manager
public class DllAux
{
[DllImport("some_dll")]
private static extern int SomeDllFunc(DelFuncCallBack callback);
public Signal ReturnCallBack;
// function call back
private void func(... some parameters ...)
{
...
if (ReturnCallBack != null)
ReturnCallBack();
}
public DllAux()
{
}
public TryDll()
{
SomeDllFunc(func);
// or SomeDllFunc(new DelFuncCallBack(func)); // I have not understood the difference
}
...
}
class myForm : Form
{
...
DllAux DllUse = new DllAux();
void UseDLL()
{
DllUse.ReturnCallBack = new Signal(SignalHasArrived);
DllUse.TryDll();
}
private void SignalHasArrived()
{
myTextBox.Text = "Signal arrived"; // <== problem here
}
...
}
The funny thing is that I don't receive any run time error as I received some month ago writing a textbox in a timer or in a serial port event manager
In reality, I receive nothing and the text box is not written
Solution found
Code:
public delegate void WriteIntoTextBox(string str);
class myForm : form
{
...
private void SignalHasArrived()
{
if (this.InvokeRequired())
this.BeginInvoke(new WriteIntoTextBox(WriteTextBox),
new object[] { "Signal arrived" }); // this line is always executed
else
WriteTextBox("Signal arrived"); // this line is never executed
}
private void WriteTextBox(string text)
{
myTextBox.Text = "Signal has arrived";
}
...
}
My question is: do you know any solution that allows me to use the first approach?
How can I transform such tread unsafe class into a thread safe
I have tried to use a delegate approach (the one shown) and also an event approach creating a class inherited form EventArgs and so on but I had the same problem
I have also tried to inherit my class form INotifyPropertyChanged with no results
Thank you very much
Davide