Wednesday, October 14, 2009

Single Instance Application in C#

Hello Friends,

Today I am discussing here a very HOT topic i.e. Single Instance in C#.

So friends, There are lots of ways to do so.
Some are very easy as 2-3 lines of code, some are big and complicated.
Just go through all of them.

Methods.
  1. Using Process Name, ID etc.
  2. By using Mutex class
  3. By using Microsoft.VisualBasic.dll
  4. By locking or binding a file.
  5. By locking a particular port.
First Method:- Using Process class

Step1. Give your window form a specific unique name.

this.Text = “My_Application_144”; // title name

Step2. Create a private method for checking all Process title Text.

private bool IsAnotherInstance()
 {
    foreach (Process process in Process.GetProcesses())
     {
        //give particular name same as you given above
        if (process.MainWindowTitle == this.Text) 
           return false;
     }
    return true;
 }

Step3. On form load method check another instance

if (!IsAnotherInstance ())
 {
     MessageBox.Show("Application is Already running.");
     this.Close();
 }

I will disscuss other method next time.
==================================================
2. Using Mutex Class

  
   [STAThread]
   static void Main()
   {
     bool isNew = true;
     using (Mutex mutex = new Mutex(true, "MyApplicationName", out isNew))
     {
       if (isNew)
       {
          Application.EnableVisualStyles();
          Application.SetCompatibleTextRenderingDefault(false);
          Application.Run(new Form1());
       }
       else
       {
          MessageBox.Show("Already running");
       }
     }
   }

Saturday, October 10, 2009

Question Related to .Net Technology.


Hello Friends,

Now i am going to post some of very good question and Article regarding .Net.

Wednesday, April 8, 2009

Convert Colored Images to Black And white in java


Hi,

Sometime you want to convert colored images to black and white.
Here i am giving you the simple method to do so.
Just see below.

//example for coverting colored image to black and white
import java.awt.image.*;
import javax.imageio.ImageIO;
import java.io.*;
import java.awt.*;

class BlackAndWhite
{
          public static void main(String asr[])
          {
                   try
                   {
                             //colored image path
                             BufferedImage image = ImageIO.read(new  
                                                File("D:\\MyProgram\\output.jpg"));

                             //getting width and height of image
                             double image_width = image.getWidth();
                             double image_height = image.getHeight();

                             BufferedImage bimg = null;
                             BufferedImage img = image;

                             //drawing a new image      
                             bimg = new BufferedImage((int)image_width, (int)image_height,
                                                                    BufferedImage.TYPE_BYTE_GRAY);
                             Graphics2D gg = bimg.createGraphics();
                             gg.drawImage(img, 0, 0, img.getWidth(null), img.getHeight(null), null);

                             //saving black and white image onto drive
                             String temp = "blackAndwhite.jpeg";
                             File fi = new File("D:\\My Program\\" + temp);
                             ImageIO.write(bimg, "jpg", fi);
                   }
                   catch (Exception e)
                   {
                             System.out.println(e);
                   }
          }
}

Tuesday, March 31, 2009

Dashed line drawing in java graphics.


Hello Friends,

 While working in my project i was searching how to draw a dashed line
in Graphics not in Graphics2D.

So i get this code and it is superb for drawing dash lines or rectangulars.
Just pass the line points and assign your dash length and space length.
See the below function........

////pass the line point x1,y1 and point 2 as x2 and y2
public void drawDashedLine(Graphics g, int x1, int y1, int x2,
                                                   int y2, double dash_length, double space_length)
{
          ///if both the points are same
          if ((x1 == x2) && (y1 == y2))
          {
                   g.drawLine(x1, y1, x2, y2);
                   return;
          }

          ///calculating line length
          double linelength = Math.sqrt((x2 - x1) * (x2 - x1) + (y2 - y1) * (y2 - y1));

          double yincrement = (y2 - y1) / (linelength / (dash_length + space_length));
          double xincdashspace = (x2 - x1) / (linelength / (dash_length + space_length));
          double yincdashspace = (y2 - y1) / (linelength / (dash_length + space_length));
          double xincdash = (x2 - x1) / (linelength / (dash_length));
          double yincdash = (y2 - y1) / (linelength / (dash_length));
          int counter = 0;
          for (double i = 0; i < linelength - dash_length; i += dash_length + space_length)
          {
                   g.drawLine((int)(x1 + xincdashspace * counter),
                               (int)(y1 + yincdashspace * counter),
                               (int)(x1 + xincdashspace * counter + xincdash),
                                (int)(y1 + yincdashspace * counter + yincdash));
                   counter++;
          }

          if ((dashlength + spacelength) * counter <= linelength)
                   g.drawLine((int)(x1 + xincdashspace * counter),
                               (int)(y1 + yincdashspace * counter), x2, y2);
}
=============================================

Wednesday, March 18, 2009

Closing command prompt from JAVA

Hi,

Many times developer ask question how to close command prompt after
launching the application?
or
How to exit from batch file after execution.

Here is all possible ways to do so.
1. Use javaw.exe for launching your application and exit.
2. Create an executable jar file so that just double clicks for launch.
3. Create exe from jar files.

If you want to exit from command prompt after calling your exe file.
Then apply the same concept which we are using for tracing output
from exe.(see previous post)

Technique:-
1. You have to insert a line on your code which print "Loaded" on output.
2. Crate a jar file then exe from jar file.
3. Again make a java file for launching your exe file.
4. Get the InputStreamReader from your process.
5. Check for string "Loaded" on output.
6. If you get "Loaded" then exit.

See the full source code.

/* Program for closing a command prompt from a executable
* Copyright 2009 @ yuvadeveloper
* Code By:- Prashant Chandrakar*/

import java.io.*;
import javax.swing.JOptionPane;
import java.net.BindException;
class Loader
{
    public static void main(String as[])
    {
        try
        {
            System.out.println("Loading application.........");

             //pass the name of your exe in place of my.exe
             Process p = Runtime.getRuntime().exec("my.exe");

             //getting inputstream reader from process
             BufferedReader reader = new BufferedReader
                                     (newInputStreamReader(p.getInputStream()));

             String line = reader.readLine();

              //exit from this class when get "Loaded" from your application
              while (line.equals("Loaded") == false)
               {
                       line = reader.readLine();
               }
                reader.close();
            }
            catch (Exception e)
            {
            }
    }
}
call exe from a batch file like...
java Loader

Tracing output from Exe in JAVA

Hello Everyone,

Suppose you are creating a exe files from you jar files.
So while running how to trace the output of application.

Here i am putting very small code to do so.
The idea behind this is make a object of process and execute a exe from runtime object.

get a inputstream reader from process.
Then trace the output.

Just see below.

/*
* Program for tracing a output from a executable
* Copyright 2009 @ yuvadeveloper
* Code By:- Prashant Chandrakar
*/
import java.io.*;
import javax.swing.JOptionPane;
import java.net.BindException;

class Loader
{
          public static void main(String as[])
          {
                   try
                   {
                             //pass the name of your exe in place of my.exe
                             Process p = Runtime.getRuntime().exec("DicomViewer.exe");

                             //getting inputstream reader from process
                             BufferedReader reader = new BufferedReader(new                   
                                        InputStreamReader(p.getInputStream()));
                             String line = reader.readLine();
                             while (line != null)
                             {
                                      line = reader.readLine();
                                      System.out.println(line);
                             }
                             reader.close();
                   }
                   catch (Exception e)
                   {
                   }
          }
}