File: SpawnTests.cs

package info (click to toggle)
gtk-sharp3 2.99.3-4.1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 25,488 kB
  • sloc: xml: 308,885; cs: 38,796; sh: 11,336; perl: 1,295; makefile: 1,099; ansic: 903
file content (114 lines) | stat: -rw-r--r-- 3,126 bytes parent folder | download | duplicates (13)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
// SpawnTests.cs - Tests for GLib.Process.Spawn*
//
// Author: Mike Kestner <mkestner@novell.com>
//
// Copyright (c) 2007 Novell, Inc.

namespace GtkSamples {

	using Gtk;
	using Gdk;
	using GLib;
	using System;

	public class SpawnTests  {

		static MainLoop ml;

		public static void Main (string[] args)
		{
			CommandLineSyncTest ();
			CommandLineAsyncTest ();
			SyncTest ();
			AsyncTest ();
			AsyncWithPipesTest ();
			ml = new MainLoop ();
			ml.Run ();
		}

		static void CommandLineAsyncTest ()
		{
			Console.WriteLine ("CommandLineAsyncTest:");
			try {
				GLib.Process.SpawnCommandLineAsync ("echo \"[CommandLineAsync running: `pwd`]\"");
			} catch (Exception e) {
				Console.WriteLine ("Exception in SpawnCommandLineAsync: " + e);
			}
			Console.WriteLine ("returning");
		}

		static void CommandLineSyncTest ()
		{
			Console.WriteLine ("CommandLineSyncTest:");
			try {
				string stdout, stderr;
				int exit_status;
				GLib.Process.SpawnCommandLineSync ("pwd", out stdout, out stderr, out exit_status);
				Console.Write ("pwd exit_status=" + exit_status + " output: " + stdout);
			} catch (Exception e) {
				Console.WriteLine ("Exception in SpawnCommandLineSync: " + e);
			}
			Console.WriteLine ("returning");
		}

		static void SyncTest ()
		{
			Console.WriteLine ("SyncTest:");
			try {
				string stdout, stderr;
				int exit_status;
				GLib.Process.SpawnSync ("/usr", new string[] {"pwd"}, null, SpawnFlags.SearchPath, null, out stdout, out stderr, out exit_status);
				Console.Write ("pwd exit_status=" + exit_status + " output: " + stdout);
			} catch (Exception e) {
				Console.WriteLine ("Exception in SpawnSync: " + e);
			}
			Console.WriteLine ("returning");
		}

		static void AsyncTest ()
		{
			Console.WriteLine ("AsyncTest:");
			try {
				Process proc;
				GLib.Process.SpawnAsync (null, new string[] {"echo", "[AsyncTest running]"}, null, SpawnFlags.SearchPath, null, out proc);
			} catch (Exception e) {
				Console.WriteLine ("Exception in SpawnSync: " + e);
			}
			Console.WriteLine ("returning");
		}

		static IOChannel channel;

		static void AsyncWithPipesTest ()
		{
			Console.WriteLine ("AsyncWithPipesTest:");
			try {
				Process proc;
				int stdin = Process.IgnorePipe;
				int stdout = Process.RequestPipe;
				int stderr = Process.IgnorePipe;
				GLib.Process.SpawnAsyncWithPipes (null, new string[] {"pwd"}, null, SpawnFlags.SearchPath, null, out proc, ref stdin, ref stdout, ref stderr);
				channel = new IOChannel (stdout);
				channel.AddWatch (0, IOCondition.In | IOCondition.Hup, new IOFunc (ReadStdout));
			} catch (Exception e) {
				Console.WriteLine ("Exception in SpawnSync: " + e);
			}
			Console.WriteLine ("returning");
		}

		static bool ReadStdout (IOChannel source, IOCondition condition)
		{
			if ((condition & IOCondition.In) == IOCondition.In) {
				string txt;
				if (source.ReadToEnd (out txt) == IOStatus.Normal)
					Console.WriteLine ("[AsyncWithPipesTest output] " + txt);
			}
			if ((condition & IOCondition.Hup) == IOCondition.Hup) {
				source.Dispose ();
				ml.Quit ();
				return true;
			}
			return true;
		}
	}
}