最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

C#線程間不能調(diào)用剪切板的解決方法

 更新時間:2014年07月16日 15:51:17   投稿:shichen2014  
這篇文章主要介紹了C#線程間不能調(diào)用剪切板的解決方法,需要的朋友可以參考下

最近做一個C#項目,需要用到線程,而且要用到剪切板,創(chuàng)建了一個子線程之后發(fā)現(xiàn)在子線程中剪切板上獲取不到數(shù)據(jù),經(jīng)過一番查找與測試最終該問題得以解決,現(xiàn)將解決方法歸納如下,供大家參考:

第一步:

public void btnAutoFocus_Click(object sender,EventArgs e)
{
Thread myThread = new Thread(msc.AutoFocusArithmetic);
//注意,一般啟動一個線程的時候沒有這句話,但是要操作剪切板的話這句話是必需要加上的,
//因為剪切板只能在單線程單元中訪問
//這里的STA就是指單線程單元
myThread .SetApartmentState(ApartmentState.STA); 
myThread .Start();
}

第二步:還需要將Program啟動類中

static class Program
{
///
/// 應(yīng)用程序的主入口點。
///

[STAThread] //這句話保留,如果要在主線程中訪問剪切板,這句式必須要的
//如果要在子線程中訪問剪切板,這個應(yīng)該可以不要,但是默認(rèn)是有的
static void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new MainForm());
//Application.Run(new TestRGBPixelThumbForm());
//Application.Run(new TestImageForm());
//Application.Run(new TestJudgeDefinitionForm());
//Application.Run(new TestVirusForm());
}
}

第三步:這個是讀取剪切板數(shù)據(jù)

private Image GetCaptureImage()
{
IDataObject iData = Clipboard.GetDataObject();
Image img = null;
if (iData != null)
{
if (iData.GetDataPresent(DataFormats.Bitmap))
{
img = (Image)iData.GetData(DataFormats.Bitmap);
}
else if (iData.GetDataPresent(DataFormats.Dib))
{
img = (Image)iData.GetData(DataFormats.Dib);
}
}
return img;
}

至此問題得以解決。

相關(guān)文章

最新評論

东港市| 镇雄县| 台江县| 大田县| 泾源县| 鹤峰县| 福清市| 新田县| 吉林省| 嘉义县| 剑河县| 二连浩特市| 三门县| 马关县| 岚皋县| 禄丰县| 沅江市| 文成县| 昆山市| 龙井市| 定结县| 清新县| 平遥县| 瑞昌市| 丽江市| 塔城市| 巍山| 新沂市| 吕梁市| 蒙城县| 和静县| 开原市| 吉木乃县| 云和县| 容城县| 浑源县| 枞阳县| 滨海县| 澳门| 黔东| 广东省|