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

主宰稳场 提交于 2019-12-14 02:19:20

问题


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:


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.



来源:https://stackoverflow.com/questions/30285005/how-to-work-with-clipboard-in-asp-net-server-side

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!