singleton implementation problem in C# - c#

--ConsoleApplication 1
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ConsoleApplication2
{
public class MsgService
{
private static CreateConnectionToA _instanceA;
private static CreateConnectionToB _instanceB;
protected MsgService()
{
}
public static MsgService GetInstanceA(string paramA, string paramB)
{
if (_instanceA != null)
{
return _instanceA;
}
return _instanceA = new CreateConnectionToA("p1","p2");
}
public static MsgService GetInstanceB(string paramA, string paramB)
{
if (_instanceB != null)
{
return _instanceB;
}
return _instanceB = new CreateConnectionToB("p1", "p2");
}
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ConsoleApplication2
{
class CreateConnectionToB : MsgService
{
public CreateConnectionToB(string param1, string Param2)
{
}
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ConsoleApplication2
{
class CreateConnectionToA : MsgService
{
public CreateConnectionToA(string param1, string Param2)
{
}
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ConsoleApplication2
{
class Program
{
static void Main(string[] args)
{
MsgService.GetInstanceA("p1", "p2");
Console.Read();
}
}
}
--ConsoleApplication 2
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("Press");
Console.Read();
ConsoleApplication2.MsgService.GetInstanceA("p1", "p2");
Console.Read();
}
}
}
I am trying to Make simgleton implementation but something is wrong with my approach. It always creates new instance of _instanceA and _instanceB from each console application.
Can someone please point me out what needs to be done here?

You would need named Mutexes for inter-process synchronization.

Sharing an object instance between two applications is kinda hard, since they run in separate appdomains, by default. To accomplish what I think you're trying to do, you'll need to either
marshal across appdomain boundaries with, or
run the two processes in a shared appdomain. Write a 3rd process — a shell — that's responsible for spawning/hosting the other two processes in a shared appdomain.
http://www.codeproject.com/KB/dotnet/AppDomainMemImprovement.aspx
Sharing data between AppDomains

Related

Why my Console.WriteLine(); does not work on referenced external project?

I'm getting an error on Console.WriteLine();
HelperLibrary Project I already reference it to my main project
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace MathClasses
{
public class MyMaths
{
public void Divisibility(int number, int divby)
{
int divisibility = number % divby;
if (divisibility == 0)
{
Console.WriteLine($"{number} is divisible by {divby}");
}
else
{
Console.WriteLine($"{number} is not divisible by {divby}");
}
}
}
}
and here is my main project
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using MathClasses;
using MiscClasses;
using PrinterClasses;
namespace ChallengeOne
{
class Program
{
static void Main(string[] args)
{
MyMaths myMaths = new MyMaths();
int number = Convert.ToInt32(Console.ReadLine());
int divby = Convert.ToInt32(Console.ReadLine());
myMaths.Divisibility(number, divby);
}
}
}
Note:
my instructor says that my 2nd project doesn't recognize my using System;
the error says
The name 'Console' does not exist in the current context

How to send a list from a form to another one

I have two forms in a Windows Forms project: Form1 and aCurso.
I'm trying to send a List with objects of a class called curso (I mean: List<curso>) from Form1 to aCurso.
But Visual Studio show this:
Accessibility inconsistency: The type of parameter List<curso> is less accessible than the method aCurso.aCurso(List<curso>).
So, here is the code from Form1:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace _18_05_18
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
List<curso> cursos = new List<curso>();
private void btnAC_Click(object sender, EventArgs e)
{
Form f = new aCurso(cursos);
f.Show();
}
}
}
Here's code from aCurso:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace _18_05_18
{
public partial class aCurso : Form
{
List<curso> cursos = new List<curso>();
public aCurso(List<curso> cursos)
{
InitializeComponent();
this.cursos = cursos;
}
}
}
Here's code from class curso:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace _18_05_18
{
class curso
{
private string nombre;
public curso(string nombre)
{
this.nombre = nombre;
}
}
}
You cannot expose a public method signature where some of the parameter types of the signature are not public. It wouldn't be possible to call the method from outside since the caller couldn't construct the parameters required.
All you have to do is make curso class public
public class curso
{
private string nombre;
public curso(string nombre)
{
this.nombre = nombre;
}
}

How do I create event between two projects?

I created two projects(Proj_1,Proj_2), Proj_1 contains Proj_1_Program.cs and ProjectOneClass.cs, Proj_2 contains Proj_2_Program.cs,and I need OnInformed trigger both Informed1 and Informed2 this is how I got so far:
//Proj_1_Program.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace CrossProjectEvent
{
class Proj_1_Program
{
static void Main(string[] args)
{
ProjectOneClass obj1 = new ProjectOneClass();
obj1.Inform += new EventHandler(Informed1);
obj1.InformNow();
Console.ReadLine();
}
private static void Informed1(object sender, EventArgs e)
{
Console.WriteLine("Informed1");
}
}
}
//ProjectOneClass.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace CrossProjectEvent
{
public class ProjectOneClass
{
public event EventHandler Inform;
public void InformNow()
{
OnInformed(new EventArgs());
}
private void OnInformed(EventArgs eventArgs) // I want this method both trigger Informed1 and Informed2
{
if (Inform != null)
{
Inform(this, eventArgs);
}
}
}
}
//Proj_2_Program.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using CrossProjectEvent;
namespace Proj_2
{
public class ProjectTwoClass
{
public ProjectOneClass obj_proj_1;
public ProjectTwoClass()
{
obj_proj_1 = new ProjectOneClass();
obj_proj_1.Inform += new EventHandler(Informed2);
}
private static void Informed2(object sender, EventArgs e)
{
Console.WriteLine("Informed2");
}
}
class Project2
{
static void Main(string[] args)
{
}
}
}
But it seems like only Informed1 being triggered, so how to fix this? thanks!
This is a typical problem requiring inter-process communication. There are a billion different techniques and approaches possible for it.
One solution would be to use Remoting using named pipes (Sample), but also TCP and NetSockets are possible. This might be one of the simplest solutions.
If you are building a larger application requiring a lot of inter-process communication, the actor model, especially the AKKA.NET libary, would be worth mentioning.
But these are just a few of the options you have.

is a field but is used like a type C#

I have 2 projects (libraries)
For Ass1.cs i have:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Ass1
{
public class Class1
{
public void print()
{
Console.WriteLine("Hello");
}
}
}
Ass2 is another library which contains a reference of Ass1. Here i am getting
the error message for cd is a field but is used like a type ?
Ass2.cs:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
/* Class1 is in project Ass1 , import project Ass1 */
using Ass1;
namespace Ass2
{
public class Class2
{
Class1 cd = new Class1(); /* gives error: is afield but is used like a type*/
cd.print();
}
}
Try to put your code inside a function or method
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
/* Class1 is in project Ass1 , import project Ass1 */
using Ass1;
namespace Ass2
{
public class Class2
{
public void foo()
{
Class1 cd = new Class1(); /* gives error: is afield but is used like a type*/
cd.print();
}
}
}
put your code in a method or constructor
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Ass1;
namespace Ass2
{
public class Class2
{
public Class2
{
Class1 cd = new Class1();
cd.print();
}
}
}

How to start a process/application on windows connection change event using C#

I can detect the network connection change event while running a C# code, how would I register an exe when Windows detects this event. What all details would I need. Below is how I am using this :
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Net.NetworkInformation;
namespace ConsoleApplication2
{
class Program
{
static void Main(string[] args)
{
testing t = new testing();
Console.Read();
}
}
public class testing{
public testing()
{
NetworkChange.NetworkAvailabilityChanged += new NetworkAvailabilityChangedEventHandler(NetworkChange_NetworkAvailabilityChanged);
}
void NetworkChange_NetworkAvailabilityChanged(object sender, NetworkAvailabilityEventArgs e)
{
if (e.IsAvailable)
{
Console.WriteLine("network is available");
}
}
}
}
What you could maybe do is in your method that is triggered to start a new process and execute your exe

Categories