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

2019-07-22 20:32发布

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?

1条回答
我命由我不由天
2楼-- · 2019-07-22 20:42

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.

查看更多
登录 后发表回答