I am doing a project including a custom OPC Client.
The Class Main represents the MainWindow in a WPF application.
The private field _opcServer will hold an object for further use.
Only one _opcServer object is allowed at any time.
I came up with this (it's all sample code and works fine)
// "Main" Class --> it's a WPF Window
public class Main
{
// the "global" server object
private OpcServer _opcServer = new OpcServer();
public Main() {}
private void connectOpcServer()
{
if(this._opcServer == null)
{
// the "global" server object
this._opcServer = this.opcClientFactory().connectOpcServer("someOpcServer");
if(this._opcServer != null)
{
// we made the connection
}
else
{
// connection failed
}
}
}
private void disconnectOpcServer()
{
if(this._opcServer != null)
{
if(this.opcClientFactory().disconnectOpcServer(this._opcServer))
{
// disconnected
this._opcServer = null;
}
else
{
// something went wrong
}
}
}
private OpcClient ocpClientFactory()
{
OpcClient opcClient = new opcClient();
return opcClient;
}
}
// Client Class
public class OpcClient
{
// the server object
private OpcServer _opcServer = new OpcServer();
public OpcClient() {}
public OpcServer connectOpcServer(string progID)
{
bool madeConnection = this._opcServer.Connect(progID);
if(madeConnection)
{
return this._opcServer;
}
else
{
return null;
}
}
public bool disconnectOpcServer(OpcServer opcServer)
{
this._opcServer = opcServer;
if(this._opcServer.disconnect())
{
this._opcServer = null;
return true;
}
return false;
}
}
Not much comments in the code but I think you get the point.
Every time connect or disconnect is triggered via user action, a new object of the OPC Client is created and the Server Object is passed in the one or the other direction.
There will be more methods (like read tags, etc ...) like this, but since the user should use them only once or twice per day, I see no problem with creating new objects and passing something between them.
But what if there is a real funny user who thinks he has to use these things (connect/disconnect/ etc...) all the time. Then I will end up creating many objects!
I gave it a thought and came up with this.
public class Main
{
// the client object
private OpcClient _opcClient = OpcClient.Instance;
public Main(){}
private void connectOpcServer()
{
if(this._opcClient.connectOpcServer("someOpcServer"))
{
// we made the connection and can now use
// this._opcClient.opcServer
}
else
{
// connection failed
}
}
private void disconnectOpcServer()
{
if(this._opcClient.disconnect())
{
// disconnected
}
else
{
// something went wrong
}
}
}
public class OpcClient
{
private static OpcClient _instance;
public static OpcClient Instance
{
get
{
if(instance == null)
{
_instance = new OpcClient();
}
return _instance;
}
}
private OpcClient()
{
this.opcServer = new OpcServer();
}
public OpcServer opcServer
{
get;
private set;
}
public bool connectOpcServer(string progID)
{
return this.opcServer.Connect(progID);
}
public bool disconnectOpcServer()
{
return this.opcServer.disconnect();
}
}
Now I create a singelton of the OPC Client and pass it to the main class. Now only one object will be created, the user can click connect/disconnect all day long.
What is the best way to proceed here?
Store the Server Object in the main class
Store the Class Object in the main class
Depends
Both are bad ideas (if so, why? What can I do instead?)
I am choosing the 2nd option.
By choosing the singleton approach I can make sure that there is only one Server Object.
This is very important.
Related
i feel stupid really, but i think i am being snow blind. i cannot access a singleton class method when calling from another classy. i get the dreaded
(NullReferenceException).
here are both my simple singleton and how i am calling the method.
public class PlayerNodePosition : MonoBehaviour
{
public static PlayerNodePosition instance;
string code;
void Awake()
{
if (instance == null)
{
Debug.LogWarning("More than one instance of Inventory found!");
return;
}
instance = this;
}
public void AddCode(string _code)
{
code = _code;
}
}
and here is the caller from another script.
void AddCode()
{
PlayerNodePosition.instance.AddCode("Added!");
}
being a "simpleton" i am obviously missing the obvious.
You don't instantiate instance anywhere. You would need something like
private static PlayerNodePosition playerNodePosition;
public static PlayerNodePosition instance
{
get
{
if (playerNodePosition == null) {
playerNodePosition = new PlayerNodePosition();
}
return playerNodePosition;
}
}
The method Awake should be static and the instance should be set. I have no chance to check whether this runs as I have no C# installed, but the Debug log warning you give is logically wrong. If there is no instance, you need to create one. If there is an instance, you return that one. This is the singleton pattern.
public class PlayerNodePosition : MonoBehaviour
{
public static PlayerNodePosition instance;
string code;
void static getInstance()
{
if (instance == null)
{
instance = new PlayerNodePosition();
}
return instance;
}
public void AddCode(string _code)
{
code = _code;
}
}
Let's assume we have the following two classes, How can we listen for Errors and if any error occurred, recreate the singleton? I have put together the following code, but would like to know if there is a pattern for safely raise error, dispose object and recreate it automatically?
`
static void Main(string[] args)
{
MyFirstClass.Instance.SayHello();
}
}
class MySecondClass
{
public int ID { get; set; }
public void SayHelloFromSecondClass()
{
Console.WriteLine("Say Hello From Second Class");
}
public MySecondClass(int id)
{
ID = id;
}
}
public sealed class MyFirstClass
{
private static readonly MyFirstClass instance = new MyFirstClass();
private static MySecondClass msc;
public event EventHandler ErrorOccuredEvent;
private MyFirstClass() { }
public static MyFirstClass Instance
{
get
{
msc = new MySecondClass(id: 1);
return instance;
}
}
public void SayHello()
{
Console.WriteLine("Hello World...");
}
static void ErrorOccured(object sender, EventArgs e)
{
Console.WriteLine("Oops");
msc = null;
Thread.Sleep(5000);
GC.Collect();
msc = new MySecondClass(id: 2);
}
}
`
If I understand well, MyFirstClass (which is a singleton) is a kind of wrapper around MySecondClass that turns MySecondClass into a singleton as well.
Let's call MyFirstClass: Wrapper
Let's call MySecondClass: Service
If the clients always consume the Service through the single instance of Wrapper, then re-creating a Wrapper will not help, because the clients might keep a reference to Wapper. Re-creating Service can help if the clients don't see it and cannot keep a reference to it. Therefore they must consume the service indirectly.
It's easiest to achieve this through an interface:
public interface IHelloService
{
void SayHello();
}
public class HelloService : IHelloService
{
public void SayHello()
{
Console.WriteLine("Hello");
}
}
public class HelloServiceWrapper : IHelloService
{
public static readonly IHelloService Instance = new HelloServiceWrapper();
private HelloServiceWrapper () {}
private IHelloService _service;
public void SayHello()
{
EnsureServiceAvailable();
_service.SayHello();
}
private void EnsureServiceAvailable()
{
if(_service == null) {
_service = new HelloService();
}
}
private void HandleError()
{
_service = null;
}
}
But if the error happens when the client is using the service ...
HelloServiceWrapper.Instace.SayHello();
... this call might fail.
You would have to re-create the service instantly in order to make succeed the client's call (assuming that re-creating the service will solve the problem and that the error will not occur again immediately):
public void SayHello()
{
try {
_service.SayHello();
} catch {
_service = new HelloService();
_service.SayHello();
}
}
Note: Disposing the service invalidates the object and makes any reference a client has to it invalid. But re-creating a new one does not give the client a new reference! You would need to have a reference to the clients reference in order to be able to give the client a new instance.
I trying to allow people to write to NFC tags using my app, so that my app gets launched with a custom parameter. I want to be able to reprogram NFC tags which already have data on them.
I am using the following code but the problem is, that WP always recognizes the action which is already on the NFC tag and interrupts because it wants to launch the NFC tag action which was written anytime before.
How can I tell the OS to stop triggering the action of the tag so that I can immediately rewrite it?
public enum NfcHelperState
{
Initializing,
Waiting,
Ready,
Writing,
Finished,
Error,
NoDeviceFound
}
public class NfcHelper
{
private NfcHelperState _state = NfcHelperState.Initializing;
public NfcHelperState State
{
get { return _state; }
}
private ProximityDevice _nfcDevice;
private long _subscriptionId;
public NfcHelper()
{
Init();
}
public void Init()
{
UpdateState();
_nfcDevice = ProximityDevice.GetDefault();
if (_nfcDevice == null)
{
UpdateState(NfcHelperState.NoDeviceFound);
return;
}
UpdateState(NfcHelperState.Waiting);
}
private void UpdateState(NfcHelperState? state = null)
{
if (state.HasValue)
{
_state = state.Value;
}
if (OnStatusMessageChanged != null)
{
OnStatusMessageChanged(this, _state);
}
}
public void WriteToTag()
{
UpdateState(NfcHelperState.Ready);
_subscriptionId = _nfcDevice.SubscribeForMessage("WriteableTag", WriteableTagDetected);
}
private void WriteableTagDetected(ProximityDevice sender, ProximityMessage message)
{
UpdateState(NfcHelperState.Writing);
try
{
var str = "action=my_custom_action";
str += "\tWindowsPhone\t";
str += CurrentApp.AppId;
_nfcDevice.PublishBinaryMessage("LaunchApp:WriteTag", GetBufferFromString(str),
WriteToTagComplete);
}
catch (Exception e)
{
UpdateState(NfcHelperState.Error);
StopWaitingForTag();
}
}
private void WriteToTagComplete(ProximityDevice sender, long messageId)
{
sender.StopPublishingMessage(messageId);
UpdateState(NfcHelperState.Finished);
StopWaitingForTag();
}
private void StopWaitingForTag()
{
_nfcDevice.StopSubscribingForMessage(_subscriptionId);
}
private static IBuffer GetBufferFromString(string str)
{
using (var dw = new DataWriter())
{
dw.UnicodeEncoding = Windows.Storage.Streams.UnicodeEncoding.Utf16LE;
dw.WriteString(str);
return dw.DetachBuffer();
}
}
public delegate void NfcStatusMessageChangedHandler(object myObject, NfcHelperState newState);
public event NfcStatusMessageChangedHandler OnStatusMessageChanged;
}
WriteToTag is called when a button in my app is tapped and the app waits for a writable tag. If a writable tag is recognized, WriteableTagDetected gets called and immediately starts the writing process. However, this is interrupted by the WP dialog which asks whether to perform the NFC action or not. After writing, WriteToTagComplete should be called, where StopWaitingForTag gets called and ends the write process.
I hope you guys can help me :)
Turns out I thought the wrong way. I didn't need to wait for a tag to arrive in order to rewrite it. In fact, there's no need to do _nfcDevice.SubscribeForMessage("WriteableTag", WriteableTagDetected); before writing. Just start using PublishBinaryMessage and it will write to the tag once it arrives at the device.
My final code looks like the following:
public enum NfcHelperState
{
Initializing,
Ready,
WaitingForWriting,
FinishedWriting,
ErrorWriting,
NoDeviceFound
}
public class NfcHelper
{
private NfcHelperState _state = NfcHelperState.Initializing;
public NfcHelperState State
{
get { return _state; }
}
private ProximityDevice _nfcDevice;
private long? _writingMessageId;
public NfcHelper()
{
Init();
}
public void Init()
{
UpdateState();
_nfcDevice = ProximityDevice.GetDefault();
if (_nfcDevice == null)
{
UpdateState(NfcHelperState.NoDeviceFound);
return;
}
UpdateState(NfcHelperState.Ready);
}
private void UpdateState(NfcHelperState? state = null)
{
if (state.HasValue)
{
_state = state.Value;
}
if (OnStatusMessageChanged != null)
{
OnStatusMessageChanged(this, _state);
}
}
public void WriteToTag()
{
StopWritingMessage();
UpdateState(NfcHelperState.WaitingForWriting);
try
{
var str = new StringBuilder();
str.Append("action=my_custom_action");
str.Append("\tWindowsPhone\t{");
str.Append(CurrentApp.AppId);
str.Append("}");
_writingMessageId = _nfcDevice.PublishBinaryMessage("LaunchApp:WriteTag", GetBufferFromString(str.ToString()),
WriteToTagComplete);
}
catch
{
UpdateState(NfcHelperState.ErrorWriting);
StopWritingMessage();
}
}
private void WriteToTagComplete(ProximityDevice sender, long messageId)
{
UpdateState(NfcHelperState.FinishedWriting);
StopWritingMessage();
}
private void StopWritingMessage()
{
if (_writingMessageId.HasValue)
{
_nfcDevice.StopPublishingMessage(_writingMessageId.Value);
_writingMessageId = null;
}
}
private static IBuffer GetBufferFromString(string str)
{
using (var dw = new DataWriter())
{
dw.UnicodeEncoding = Windows.Storage.Streams.UnicodeEncoding.Utf16LE;
dw.WriteString(str);
return dw.DetachBuffer();
}
}
public delegate void NfcStatusMessageChangedHandler(object myObject, NfcHelperState newState);
public event NfcStatusMessageChangedHandler OnStatusMessageChanged;
}
This question already has answers here:
Use Unity API from another Thread or call a function in the main Thread
(5 answers)
Closed 6 years ago.
I am working on a project where I am having two Unity Projects that need to communicate with each other. I am trying to solve this by using the .net Remoting Framework.
For That I created a dll which both Unity projects will use. The dll consists of:
MyRemotableObject.cs
public class MyRemotableObject : MarshalByRefObject
{
public MyRemotableObject()
{
}
public void NotifyStatusChange(int status)
{
Cache.GetInstance().Status = 0;
}
public int GetCreditCount()
{
return Cache.GetInstance().CreditCount;
}
}
Cache.cs
public class Cache
{
private static Cache myInstance;
public static IObserver Observer;
private Cache()
{
}
public static void Attach(IObserver observer)
{
Observer = observer;
}
public static Cache GetInstance()
{
if(myInstance==null)
{
myInstance = new Cache();
}
return myInstance;
}
public int Status
{
set
{
Observer.NotifyFinished(value);
}
}
public int CreditCount
{
get
{
return Observer.QueryCreditCount();
}
}
}
IObserver.cs
public interface IObserver
{
void NotifyFinished(int status);
int QueryCreditCount();
}
Now I have my Menu - Unity project, acting as the remoting server
MenuController.cs
public class MenuController : MonoBehaviour, IObserver
{
private object lockObject;
List<ControllerBase> controllers;
private MyRemotableObject remotableObject;
private System.ComponentModel.Container components = null;
void Awake()
{
lockObject = new object();
try
{
remotableObject = new MyRemotableObject();
//für fehler: //http://www.mycsharp.de/wbb2/thread.php?postid=199935
//************************************* TCP *************************************//
// using TCP protocol
TcpChannel channel = new TcpChannel(124);
ChannelServices.RegisterChannel(channel, false);
RemotingConfiguration.RegisterWellKnownServiceType(typeof(MyRemotableObject), "TargetShooterMenu", WellKnownObjectMode.SingleCall);
//************************************* TCP *************************************//
RemotableObjects.Cache.Attach(this);
}
catch (Exception e)
{
Debug.Log(e.ToString());
}
controllers = new List<ControllerBase>();
foreach (GameObject controllerObject in GameObject.FindGameObjectsWithTag(GlobalNames.Tags.CONTROLLEROBJECT))
{
if (controllerObject.GetComponent<ControllerBase>())
controllers.Add(controllerObject.GetComponent<ControllerBase>());
}
}
delegate void PresentNameInputControllerDelegate(int status);
private void PresentNameInputController(int status)
{
if (status == (int)LevelStatusCode.OK)
foreach (ControllerBase controller in controllers)
{
controller.Hide();
if (controller.GetType() == typeof(NameInputController))
controller.Show();
}
}
public void NotifyFinished(int status)
{
Debug.Log("Notify");
lock (lockObject)
{
PresentNameInputControllerDelegate d = PresentNameInputController;
d(status);
}
}
public int QueryCreditCount()
{
Debug.Log("Query");
return 100;
}
}
This Server implements the IObserver Functions NotifyFinished and QueryCreditCount (returns dummy value for the moment)
When calling the NotifyFinished function from the client, following error occurs:
get_animation can only be called from the main thread.
Constructors and field initializers will be executed from the loading thread when loading a scene.
Don't use this function in the constructor or field initializers, instead move initialization code to the Awake or Start function.
Can someone tell me, how to solve this problem?
Thanks in advance,
Hoffmanuel
After lots of searching, i came to the solution: Using the Loom Unity Package from:
Unity Gems entry about threading
and using it like mentioned in Unity answers entry about threading:
void Start()
{
var tmp = Loom.Current;
...
}
//Function called from other Thread
public void NotifyFinished(int status)
{
Debug.Log("Notify");
try
{
if (status == (int)LevelStatusCode.OK)
{
Loom.QueueOnMainThread(() =>
{
PresentNameInputController();
});
}
}
catch (Exception e)
{
Debug.LogError(e.ToString());
}
}
I have searched for creating a Singleton object for a window in WPF.
public static Test DefInstance
{
get
{
if (formDefInstance == null) // formDefInstance.IsDisposed
{
initializingDefInstance = true;
formDefInstance = new cas18();
initializingDefInstance = false;
}
return formDefInstance;
}
set { formDefInstance = value; }
}
But the forDefInstance.IsDisposed is not working and throwing an error.
Any Idea regarding this?
I think everyone should take a look at Jon Skeet's C# In Depth site. If only to read and permanently burn into their brains the singleton patter a-la C#.
http://csharpindepth.com/Articles/General/Singleton.aspx
In your scenario, try to implement this (thread safe, non-lazy):
public sealed class DefInstance
{
private static readonly DefInstance instance = new DefInstance();
static DefInstance()
{
}
private DefInstance()
{
}
public static DefInstance Instance
{
get
{
return instance;
}
}
}
There are also Lazy<T> implementions and various other implementations of the pattern in that site.
I don't know if it's what you want to do but it works for me :
private static MyWindow _defInstance;
public static MyWindow DefInstance
{
get
{
if (null == _defInstance)
{
_defInstance = new MyWindow();
}
return _defInstance;
}
}
In MyWindow code :
protected override void OnClosing(System.ComponentModel.CancelEventArgs e)
{
this.Visibility = Visibility.Hidden;
e.Cancel = true;
}
To use it :
DefInstance.Show();
Then, only one window is display and you use one instance of your window.
you can achieve this by implementing following method
private static volatile DefInstance instance;
private static object syncRoot = new Object();
private DefInstance() {}
public static DefInstance Instance
{
get
{
if (instance == null)
{
lock (syncRoot)
{
if (instance == null)
instance = new DefInstance();
}
}
return instance;
}
}