Console C# program audio never plays - c#

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();
...

Related

What are top level statements in C#? What is their use?

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!");

Console application message does not show up

using System;
namespace ConsoleApp2
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("Hello World!");
}
}
}
Code is perfect and simple.. you know :D
but the editor is empty? Whats the matter?
I first suggest you go to "Tools->Options->Debugging" and then make sure the checkbox at "Close the console when debug ends" (4th element from the bottom) is not checked. That will leave the console window open when the application exits.
Try running with Ctrl-F5.
This starts without the debugger and keeps the window open after the program ends.
I always set up a button for it each VS install, to make my life easier.
and the results:

C# not outputting to console

I'm sure there's some simple answer, but none of the other Stack Overflow posts has helped me. My code will not log to the Console, and it's hard to do anything useful with that state of affairs.
using System;
using System.Diagnostics;
namespace Learning
{
class MainClass
{
public static void Main (string[] args)
{
Debug.Log ("this?");
Debug.Print ("How about this?");
Console.WriteLine ("WORK");
Console.ReadLine ();
}
}
}
I've been able to write to the console before, I don't know why it's being persnickety now.
Probably because your code doesn't actually compile. Log() is a static method of Debugger, not Debug, and it takes three arguments: level, category, and message.
public static void Main (string[] args)
{
System.Diagnostics.Debugger.Log(1, "category", "this?");
System.Diagnostics.Debug.Print ("How about this?");
Console.WriteLine ("WORK");
Console.ReadLine ();
}
It's worth noting that Debug/Debugger methods will not do you any good unless you are Debugging. To start a debugging session in mono, go to the Run -> Debug
You may want to check what kind of application you are using. For example, if you are making a Forms Application, you won't have access to the Console functions.
You can change this by going into the Solution Properties, and changing it from a Windows Forms Application to a Console Application. This won't have any effect on your program, other than it will run a Console alongside.

Linux process API using C# in Mono

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#.

Using managed code with Mono embedding

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...

Categories

Resources