Mehdi Dehghani
Mehdi Dehghani

Reputation: 11601

How to work with Clipboard in ASP.NET, Server side

Is there any way to working with Clipboard in ASP.NET, in Server-side? I want to push something in Clipboard & fetch it.

EXTRA INFO: I had some search and found out, the solution is working with Thread. but I'm looking for another way, if is there another way.

UPDATE: Please answer to following questions:

  1. Can I work with thread when I'm working with clipboard?
  2. If so, can I run new thread when I'm working with clipboard more than one time withing single process (imaging user clicked on a button and I have to push 100 data in clipboard withing a for (loop))

For example:

Option 1:

void myMethod(object i){        
    // put something on clipboard and get that        
}

protected void btnSubmit_Click(object sender, EventArgs e)
{
    for(int i=0; i<100; i++){
         Thread t = new Thread(myMethod);
         t.Start(i);
    }
}

Option 2:

void myMethod(){        
    for(int i=0; i<100; i++){
        // put something on clipboard and get that        
    }
}

protected void btnSubmit_Click(object sender, EventArgs e)
{
     Thread t = new Thread(myMethod);
     t.Start();        
}

Which one is correct?

Upvotes: 2

Views: 1757

Answers (1)

Dusan
Dusan

Reputation: 5144

This is how you do it:

    public static void PdfToJpg()
    {
        var Thread = new Thread(PdfToJpgThread);
        Thread.SetApartmentState(ApartmentState.STA);
        Thread.Start(); // You can pass your custom data through Start if you need
    }
    private static readonly object PdfToJpgLock = new object();
    private static void PdfToJpgThread(object Data)
    {
        lock (PdfToJpgLock)
        {
            for (int i = 0; i < count; i++)
            {

                // Call to Acrobat CopyToClipboard
                // ...

                Clipboard.GetImage().Save(outputPath, ImageFormat.Jpeg);
                Clipboard.Clear();

                // ...
            }
        }
    }

For each button click, just call PdfToJpg() and you are done.

Upvotes: 1

Related Questions