Tags » C-2

Lock ing an object

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Threading;

namespace lockOrnek2
{
    class Program
    {
        //create locking object    
        static object _kontrol = new object();

        static void say1()
        {
            lock (_kontrol)
            {

                for (int i = 0; i < 20; i++)
                {
                    Console.WriteLine(i + " - x");
                }
            }
        }

        static void say2()
        {
            lock (_kontrol)
            {
                for (int i = 0; i < 20; i++)
                {
                    Console.WriteLine(i + " - y");
                }
            }
        }

        static void Main(string[] args)
        {
            Thread t1 = new Thread(new ThreadStart(say1));
            Thread t2 = new Thread(new ThreadStart(say2));
            t1.Start();
            t2.Start();
        }
    }
}

C#

Mutithreading - Lock a file

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.IO;
using System.Threading;

namespace LockOrnek
{
    class Program
    {
        string yol = @"C:\Users\dsadas\Desktop\veri.txt";

        void Yaz()
        {
            lock (yol)
            {
                //flush file before writing
                File.WriteAllText(yol, string.Empty);

                StreamWriter yaz = File.AppendText(yol);

                for (int i = 0; i < 10; i++)
                {
                    Thread.Sleep(100);
                    yaz.WriteLine(Environment.TickCount);
                    Console.WriteLine("Writing");
                }
                yaz.Close();
            }
        }

        void Oku()
        {
            lock (yol)
            {
                var yazi = String.Empty;
                StreamReader oku = File.OpenText(yol);

                while ((yazi = oku.ReadLine()) != null)
                {
                    Console.WriteLine("Reading");
                }

                oku.Close();
            }
        }

        static void Main(string[] args)
        {
            Program a = new Program();

            Thread t1 = new Thread(new ThreadStart(a.Yaz));
            t1.Start();
            Thread t2 = new Thread(new ThreadStart(a.Oku));
            t2.Start();

            Console.ReadLine();
        }
    }
}

C#

USACO 1.1.3 Friday the Thirteenth

/*
ID: ahmedye1
PROG: friday
LANG: C++
*/
#include <iostream>
#include <fstream>

using namespace std;

int getNumOfDays(int month, int year)
{
if (month == 4 || month == 6 || month == 9 || month == 11)

118 more words
C#

Vampire's Kiss

(USA 1989)

“Alva, there is no one else in this entire office that I could possibly ask to share such a horrible job. You’re the lowest on the totem pole here, Alva.

390 more words
American

Write commands to serial port from c# application

In one of my projects I needed to use a DEVICE which used serial port to communicate with any other application. Hence from my C# application I had to somehow send commands to that device in order to operate it. 325 more words

.net

Object Oriented Programming Interview Questions

What is object-oriented programming (OOP) ?

OOP is a technique to develop logical modules, such as classes that contain properties, methods, fields, and events. An object is created in the program to represent a class. 1,594 more words

C#

USACO 1.1.1 Your Ride Is Here

/*
ID: ahmedye1
PROG: ride
LANG: C++
*/
#include <iostream>
#include <fstream>
using namespace std;

int main()
{
ifstream inputFile (“ride.in”);
ofstream outputFile (“ride.out”);
string groupName;

60 more words
C#