I know it is possible to code C# .NET apps in Linux using Mono. However, I am wondering about the process interfaces of Linux. Could I use services like getpid(), getppid() and fork() using C# and Mono and running on a Linux environment?
For getpid() and getppid() you could use Syscall in this way:
using System;
using Mono.Unix.Native;
namespace StackOverflow
{
class MainClass
{
public static void Main(string[] args)
{
int pid = Syscall.getpid();
int ppid = Syscall.getppid();
Console.WriteLine ("PID: {0}", pid);
Console.WriteLine ("PPID: {0}", ppid);
}
}
}
You need Mono.Posix.dll
For fork() you can use Process. See example here: creating child process with C#
Documentation about Process Class C#.
Related
I just created my first .NET 6 console app, and instead of the default,
using System;
using System.Collections.Generic;
using System.Linq;
namespace MyApp
{
public class Program
{
public static void Main(string[] args)
{
Console.WriteLine("Hello World!");
}
}
}
I got:
// See https://aka.ms/new-console-template for more information
Console.WriteLine("Hello, World!");
And even when there's no class, the program runs! I took a look at the link provided in the comments, but I don't know if this is a new feature or an old one. If this is a new feature, does that mean C# will be allowing C-style programming hereafter?
It's a new feature of C# 9 or 10. Microsoft documentation says following:
Top-level statements enable you to avoid the extra ceremony required by placing your program's entry point in a static method in a class. The typical starting point for a new console application looks like the following code:
using System;
namespace Application
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("Hello World!");
}
}
}
The preceding code is the result of running the dotnet new console command and creating a new console application. Those 11 lines contain only one line of executable code. You can simplify that program with the new top-level statements feature. That enables you to remove all but two of the lines in this program:
Console.WriteLine("Hello, World!");
In a UWP C# app, need background (ie. worker) thread to use UI thread to display an image. But can't figure out how to compile Dispatcher.RunAsync().
using Foundation;
using System;
using UIKit;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using System.Timers;
using System.Threading;
using System.Windows.Threading; <<<<<<<<<< gets error
using Windows.UI.Core; <<<<<<<<<< gets error
public async static void process_frame()
{
await Dispatcher.RunAsync(CoreDispatcherPriority.Normal, () =>
{
// "the name dispatcher does not exist in current context"
//UI code here:
display_frame();
});
}
public void display_frame()
{
var data = NSData.FromArray(System_Hub_Socket.packet_frame_state.buffer);
UIImageView_camera_frame.Image = UIImage.LoadFromData(data);
}
Latest method
public async static void process_frame( /* ax obsolete: byte[] camera_frame_buffer, int frame_size_bytes */ )
{
await Task.Run( () => { viewcontroller.display_frame(); } );
}
// [3]
// Copies latest frame from camera to UIImageView on iPad.
// UI THREAD
public Task display_frame()
{
var data = NSData.FromArray ( System_Hub_Socket.packet_frame_state.buffer);
<<<<<< ERROR
UIImageView_camera_frame.Image = UIImage.LoadFromData( data );
return null;
}
Error from the latest method
Looking at the using statements in you code:
using UIKit;
...
using Windows.UI.Core;
This just can't happen. UIKit is a Xamarin.iOS, platform-specific namespace and Windows.UI.Core is Windows platform-specific namespace and in no way can those two be mixed in one file (except for shared project with #if directives that is, but that is not the case here).
Xamarin helps writing cross-platform apps, but you still cannot use platform specific APIs on OS on which they are not available. Windows has Dispatcher as a means of running code on the UI thread, but this concept is not available on iOS, which uses InvokeOnMainThread method instead.
So if you are writing code that is in the platform-specific iOS project, you must use iOS APIs. If you are writing code that is in the platfrom-specific UWP project, you must use UWP APIs - things like Dispatcher will work without problem there.
Finally, if you are writing code in a .NET Standard library, you cannot write any platform specific code directly and must use dependency injection to define an interface behind which you hide the use of platform specific APIs.
I'm currently spending a bit of time making simple Console applications using Xamarin studio/monodevelop in C#. (On Mac OSX)
But for whatever reason, my audio code isn't working. A track never plays when the console opens, but I get no errors.
I'm sure the code is good:
using System;
using System.Media;
namespace Learnin
{
class MainClass
{
public static void Main(string[] args)
{
(new SoundPlayer(#"/Users/alasdairgorniak/Desktop/Take.wav")).PlaySync();
...
I am trying to embed managed c# code to c++ (with the help of tutorial in http://www.mono-project.com/Embedding_Mono and examples included in samples).
However I cannot get it to work. I think it is very possible that the problem is with a System.Net.Sockets.TcpClient object (for instance I can access values and methods of some other classes but if I add a TcpClient object to a class I encounter problems.
Here is some simple C# code I wrote to test just adding a TCPClient object
using System;
using System.Collections.Generic;
using System.Text;
using System.Net.Sockets;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
}
static void TCPTest()
{
TcpClient TCPClient;
//Console.WriteLine("in tcp test");
}
}
}
And the c++ code is here: http://codepad.org/f9D5bg8u (it is a stripped down version of the mono embedding sample).
When I build the C# code like this,
mono_runtime_invoke (method, obj, NULL, NULL);
in the c++ side exits with code 1.
When I comment that out and try the console.writeline, that works.
I appreciate any suggestions,
Thanks...
I am using VMware Workstation 6.5 on Windows Vista x64.
I am trying to write some C# code that uses VMware Vix v1.6.1 COM API to get a list of registered virtual machines.
The code I am using is as follows:
using System;
using VixCOM;
namespace ConsoleApplication48
{
internal class Program
{
private static void Main()
{
var lib = new VixLibClass();
object results = null;
var job = lib.Connect(Constants.VIX_API_VERSION, Constants.VIX_SERVICEPROVIDER_VMWARE_WORKSTATION, null, 0,
null, null, 0, null, null);
var err = job.Wait(new[] {Constants.VIX_PROPERTY_JOB_RESULT_HANDLE}, ref results);
if (lib.ErrorIndicatesFailure(err))
Console.WriteLine("Error: " + err);
var host = (IHost)((object[])results)[0];
job = host.FindItems(Constants.VIX_FIND_REGISTERED_VMS, null, -1, new DiscoveryCallback(lib));
job.WaitWithoutResults();
host.Disconnect();
}
}
internal class DiscoveryCallback : ICallback
{
protected VixLibClass lib;
public DiscoveryCallback(VixLibClass lib)
{
this.lib = lib;
}
#region ICallback Members
public void OnVixEvent(IJob job, int eventType, IVixHandle moreEventInfo)
{
// this method is never called
}
#endregion
}
}
I am aware that the COM dll is 32-bit, so I made sure that the test application is compiled as 32-bit.
Also I made sure that all VMware services are running.
There are no exceptions thrown, no errors returned (as far as I can see) and no events written into the event log.
Strangely enough the above code works when I try to get a list of running VMs using constant VIX_FIND_RUNNING_VMS.
Any ideas on what might be causing this?
Thanks,
Arnie
Quick update on the situation.
I've had a closer look at the official documentation for the FindItems() method. Constant VIX_FIND_REGISTERED_VMS is not listed as a supported parameter - only constant VIX_FIND_RUNNING_VMS is.
My guess is this means that currently VIX API offers no way to get a list of VMs registered on a VMware Workstation instance.
That also would explain why the vmrun.exe command-line utility offers no way of getting registered VMs.
I guess I'll just have to wait until the next version of the VIX API is released.