visual c# 2010 communicating between two projects - c#

I am trying to create a windows form project, and use speech recognition for the Kinect with the Kinect to Windows SDK. I have
the form application project (p1) and
the Kinect speech project (p2) which is a command prompt.
I made it a command prompt because it was the easiest way to do things. Anyway, I have read and found two things about this.
1)I found out how to run two projects at the same time in the same solution.
2) I also found out how to add references to get classes from each project to the other.
So, how would I get variables from each project? Just by using project references, or something? P2 can recognize speech and save it to variables, if that counts for anything.

I made it a command prompt because it was the easiest way to do things.
That sounds like the problem. It sounds like really you should be looking at making your Kinect project a class library. Then you can just call into that class library from the Windows Forms application.
If you want a "test bed" console app, you can always write one which also references the class library.
Note that generally you shouldn't be sharing variables between projects - they're implementation details in most well-encapsulated systems - but you would create types which expose properties, appropriate methods etc.

Here's a couple of options if I'm understanding you right:
Add those variables to your classes as properties then
using Solution.MyNamespace;
in the class that uses the other project
If you have variables that need to be independent, consider adding a class library project called Abstract or something that both projects reference
I hope this might help,
Cheers

Another method is to use named pipes for interprocess communication.
MSDN has the references to use the name pipes API here.
Named pipes are part of the .NET framework and are a reliable method for communication without having to worry about access permissions on files.
To go down the static variable path, you would need to run a single process and turn one project into a dll and load the Program Main manually.
Of course you dont even need to use static variables but use synchronisation on reference variables passed in at load time. I would probably go this method if you didnt need to run two separate processes.
Just depends on what your goal is for having the projects separate processes.

Like #JonSkeet said, create a class library, then you can save the information like this:
public class SpeechRecognizer
{
public List<string> SpeechRecognized = new List<string>
{
};
public void SaveRecognizedSpeech(string foundSpeech)
{
SpeechRecognized.Add(foundSpeech);
}
}
In code:
SpeechRecognizer sr = new SpeechRecognizer();
sr.SaveRecognizedSpeech("blah blah");
sr.SaveRecognizedSpeech("BLAH BLAH");
Console.WriteLine("{0}, {1}", sr.SpeechRecognized[0], sr.SpeechRecognized[1]);
Console.Read();
Oh and to make your whole program know variables(I hope these are both in the same solution!) make them public. Hope this helps!

Related

Class object sharing across C# and Android

I create a lot of object classes when I do programming. There are many situations where same object definition will be reused across multiple projects. In windows, I simply build them into .dll file library and include them as the project reference. Therefore, when I need to add additional properties or methods, I just need to do it once and I don't need to worry about go through all projects and manually update the object class definition.
Now, I'm given a project to build an Android application which requires several object classes that's being used within other projects (and must be synced). Of course, I can manually create them within Android and update them every time whenever there's a change, but this is very dangerous because one day in the future, it is very likely to be out-of-synced.
Anyone have suggestions on how to share class library across C# and Android?
Thank you
The only way I know how to do this is to use Xamarin which would allow you to write your entire Android application in C#.
The problem is Android and .Net use completely different runtimes that are not compatible.
I don't have a clear enough view of what your application does, but if you are using the C# objects on a webAPI and looking to keep your objects synced with the client app, you can use Breeze.js - this keeps your client/server biz objects synced. The classes get pulled in dynamically via a meta service call.

Compiling forms in C#

I want to compile each individual form on my application to be used sort of as a dll on its own... I looked into this and found very confusing representations of assemblies, which may or may not be what I wanted.
Is it possible to compile the form1.cs, form1.designer.cs and form1.resx to be 1 single file which then will be able to be used as a dll. I use "dll" as an example because that is the functionality I need with each of these forms when compiled to a single file, I need to be able to call it and use it from a shell application.
I know it is possible in VS to create a separate project which will compile into a dll but with something on the verge of 80 forms to compile... it will be a messy thing to maintain. So basically, is there an easier way?
this is the closest code I could get, but it is in console, so it will be impractical if there are easier ways... also I am not sure if it will actualy compile form1.cs, form1.designer.cs and form1.resx and still work as a dll
csc /target:library /out:MathLibrary.DLL Add.cs Mult.cs
Thanks for the help
Possible? Yes. Advisable? Umm, not sure.
You must study the CSC options to use it in such a massive way.
Partial classes are simply each listed among the sources. See here
The RESX file must be compiled by ResGen.exe to a resources file see here
You will use the /References parameter to include other DLLs.
The real challenge will probably come when you try to get cross references to work, depending on the layout of your application. Is there a main hub that will control all forms? Is it a plug-in architecture?
Good luck
Basically, you are working with solution. It can contain multiple projects. For each dll, you must have one project. So create 80 projects, add to each of them single form, edit it, add some logic.
Then there will be a main project, which produce exe. You can reference all dlls in that project, but better don't. If you do, updating any of dll will required recompiling that exe too. You can load them dynamically or use sort of plugin system (to enumerate dlls, understand their purpose, etc). Then you obtain Type from assembly (loaded dll), create instance (which will call constructor, which calls InitializeComponents, which loads form resources) and display form.
Regarding abstraction, you surely need something. To example, login window. You can create a generic form with some focus, user interface and user interaction logic. But it has to communicated with main project (which encapsulate encryption, password storage model, user rights, etc). One easy way to do this is to provide 2 interfaces:
interface ILoginImplementation
{
public void SetInitialUserName(string name);
}
interface ILoginLogic
{
public bool TryAuthenticate(string name, string password);
}
Implementation is what your form must implement and Logic is what main project implements and supply when instantiating login form.
I realize this is probably not ideal, but I still think your best bet is to use Visual Studio and create a separate project for each .dll to be created.
By right clicking the Solution node and selecting Add > New Solution Folder, you can at least organize your projects into a somewhat more orderly hierarchy. That alone might go a long way to make your project more manageable.
PS: If you haven't already, you should definitely try to create an interface, or a base class (or both!) that each of your Form-classes can derive from or implement. If you're able to abstract away and generalize some of the logic, it is quite likely to save you a lot of work down the road.

Cyclic dependency issue in referencing a window form application and class library

I have a 'window forms application' project and it has a reference to class library lets say LibA. Now LibA wants to access some data of this 'window forms application' project, how to do it now ? I cant add reference of this 'window forms application' project to LibA because of cylic dependency thing.
You can not do both things referring LibA in 'window forms application' and referring 'window forms application' in LibA. You can add reference in one way only.
If you are in such a situation that means your project architecture is not well designed. So to over come this situation you should design it in such a manner so that you don't face this situation.
More over you can refer below link in which Cyclic Dependency and how to over come it has been explained very well
What is Circular dependency and how do we resolve it
A Class library should never reference a Windows Forms (an application level proejct)
The issue you're facing is related to software design (or software architecture)
If the class library needs something from the Windows Forms it means that the windows forms project is doing something it is not responsible for.
You have two solutions and both imply redesign
Pass the code from the windows forms to your class library
or
Create a new class library project, put the 'common' code there and reference it in both projects
I strongly advise you to read about system architecture
SOLID Design
Multitier applications. an example with asp.net
EDIT
Nevertheless, if you are aware of its dangerous, you could reference the windows forms dll itself from the output bin directory instead of the Project. Attention that is very bad code and done only in extremely rare cases.
Second choice, also not recommended, is to use reflection to access the windows forms dll. You can use Assembly.Load() and then invoke or get property values in run-time.
Given your (perhaps) level of expertise i'd strongly maintain the recommendation of learning more about system architecture.
Here's quick and dirty a trick to bypass cyclic dependency issues (nevertheless, I agree with SpiderCode to say that this should not happen with a good architecture).
In your assembly LibA, define:
public interface IMyUi
{
// put whatever functions you need to access from LibA
string GetData();
}
public static class MyUiProvider
{
public static IMyUi MyUi;
}
In your assembly LibB, just make the class you want implement IMyUi, and assign MyUiProvider.MyUi on program load
Once this is done, you can use your UI via MyUiProvider.MyUi.GetData().
NB: I put that as static to have quick example of the pattern. This might not be a good practice: static keeps your UI in gc roots, and it forbids you to have multiple instances... but you can transpose the idea to instance injection of the interface.
NB 2: this pattern is known as "inversion of control"

Share an instance between multiple projects

I am working in VS 2008 C# and need to share an instance of an object created in one project with another project. I tried creating a static class in project1 and adding it as a link to project2, but the information wasn't saved. The static class was written in project1.
//object o = new object
//project1.staticObject = o
//project2.object = project1.staticObject
When I tried something like above, project2.object would be null. By adding a class as a link, is it creating a new instance of the static class in project2 or is it referencing the same class? If it is referencing the same class, shouldn't any information saved into the static class from project1 be accessible by project2? I know this isn't the most elegant manner of sharing data, but if anyone would help with this problem or provide a better manner of doing it, I would greatly appreciate it.
Thanks in advance.
Projects run in separate processes, so they can't share data in this manner. You'll need to persist the data in another type of store. I recommend using a database (hey, 20 gazillion websites, stock trading apps, airlines, etc can't be wrong).
If you don't want to use a database, you could open an IP connection between instances of the app and have a thread send packets of data to sync back and forth between the applications. Or, in your "server" app, add a web service that each process would call to update and retrieve information.
If you need really high-speed communication between the processes, sockets with a peer or star topology is a good way to go. If you're okay with some latency, having a web service (which works fine even if these aren't web apps) or a database could be a good solution. The right approach depends on your application.
WCF could also solve this problem. It effectively wraps the IP/socket layer and provides some nice object persistence/remote control capabilities, but I find it overly complex for most applications.
To share a single instance of an object among different processes (that's what I think you are intending to do) you need something that will maintain that object's state. You can look at the WCF and how to set up it's behaviour to act as a singleton so essentially every requester gets the same instance across the board.
http://msdn.microsoft.com/en-us/magazine/cc163590.aspx
Creating the link creates only applies to the source code. When you compile each project, it then has that single class definition available in both projects. The process you took does nothing for instances during runtime for sharing.
You can look at WCF or .NET Remoting, although .NET Remoting is now officially replaced by WCF.
If you are talking about sharing the same object between two processes, you can do that, the concept is called memory-mapped files. Here is some starter docs from msdn.
Though the docs and API use the term "FileMapping" quite a bit, you can use it just for sharing memory between two processes.
In .NET 4.0, you can use the System.IO.MemoryMappedFiles namespace. For your case, looks like .NET 3.5, you'll have to use some sort of interop to use the Win API.

How to Create a C# Library that is Loaded into Memory only Once?

I would like to create a Class Library DLL in C#, that will have a Static Class in it.
That static class has 1 private static member(int), with a public static property for it.
What I want is, that for Every C# Application that references this DLL, it will get the same static class.
Meaning If Application1 changes the static member's value to be 5,
and then Application2 tries to get the value of the propery, it will get: 5.
Despite the fact that those are two different applications(EXEs).
In simply words, I want this whole class library to be "static",
so only once will be loaded of it to memory, and then its single value will be shared accross different EXEs that reference it.
Thank you
An attractive solution to shared data amongst all processes on the same computer is shared memory. You will have to rewrite your properties to retrieve the shared value, and the class will be loaded multiple times into each process that uses your library, but it will behave as though there were only one copy, if you do it correctly.
Here is a StackOverflow question to help you get started:
How to implement shared memory in .NET ?
It links to a complete shared-memory library you can use.
What you're looking for is some form of IPC or RPC. One option is .NET Remoting.
I am Omer, I registered so it looks as if I am a different user.
Regarding what Damien_The_Unbeliever said, allow me all to describe what I generally want to do, so you can direct me towards the best solution for that case.
I have created a nice application.
This application has a GUI.
I want to add another way to operate this application, which will be via exposing an API for it - methods and properties that other applications can use, to operate my application.
And what I plan to do is:
Create a DLL with a static class,
put the core of my application in that class,
and then have the GUI, and potentially all other applications, use that class.
please note that this class will hold not only data that can be stored,
but also references to "live" objects(i.e. open coomunication ports, etc etc),
so storing to disk, and reading from it will not be a good solution here.
I need this 1 static class, to be accessible from all applications that want,
and that all of them will get the same static class.
So, I need this class to be static, and I need the library to be "static".
(no such thing in .NET as "static library", I know. please note that I am not talking about static library like in C++ - there it's something else.. I am talking about a DLL that is created once only, for all applications who use it).

Categories