读书人

C#的Service项目中如何使用FolderBr

发布时间: 2013-07-16 22:38:04 作者: rapoo

C#的Service项目中,怎么使用FolderBrowserDialog?


using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.ServiceProcess;
using System.Text;
using System.IO;
using System.Windows.Forms;
using System.Threading;
using Name.APIClasses.Core;

namespace AndyRuntimeService
{
public partial class Service1 : ServiceBase
{
private Thread m_thread;
/**
*
*/
public Service1()
{
InitializeComponent();
m_thread = new Thread(new ThreadStart(S_FormShow));
m_thread.Priority = ThreadPriority.Normal;
}
//=--=--=--=--=--=--=--=--=--=--=--=--=--=--=--=--=--=--=--=--=--=--=--=--
// static
//=--=--=--=--=--=--=--=--=--=--=--=--=--=--=--=--=--=--=--=--=--=--=--=--
public static void S_FormShow()
{
FormAPI2.GetDesktopWindow();
/*
*/
IntPtr hSavedWinStation = FormAPI2.GetProcessWindowStation();
IntPtr dwThreadId = FormAPI2.GetCurrentThreadId();
IntPtr hSavedDeskThread = FormAPI2.GetThreadDesktop(dwThreadId);
/*
*/


IntPtr hUserWinStation = FormAPI2.OpenWindowStation("WinSta0", false, 33554432);
if (hUserWinStation == IntPtr.Zero)
{
FormAPI2.RpcRevertToSelf();
return;
}
FormAPI2.SetProcessWindowStation(hUserWinStation);
IntPtr hUserDesk = FormAPI2.OpenDesktop("Default", 0, false, 33554432);
FormAPI2.RpcRevertToSelf();
if (hUserDesk == IntPtr.Zero)
{
FormAPI2.SetProcessWindowStation(hSavedWinStation);
FormAPI2.CloseWindowStation(hUserWinStation);
return;
}
FormAPI2.SetThreadDesktop(hUserDesk);
/*
*/
IntPtr dwGuiThreadId = dwThreadId;
/*
生成窗体,然后运行
*/
//Form f = new Form();
//f.Text = "窗体";
//Application.Run(f);
/*
*/
MessageBox.Show("xx2");
FolderBrowserDialog dlg = new FolderBrowserDialog();


dlg.Description = "选择文件";
dlg.RootFolder = Environment.SpecialFolder.DesktopDirectory;
dlg.ShowDialog();
/*
*/
dwGuiThreadId = IntPtr.Zero;
FormAPI2.SetThreadDesktop(hSavedDeskThread);
FormAPI2.SetProcessWindowStation(hSavedWinStation);
FormAPI2.CloseDesktop(hUserDesk);
FormAPI2.CloseWindowStation(hUserWinStation);
}
//=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
// protected
//=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
protected override void OnStart(string[] args)
{
m_thread.Start();
}

protected override void OnStop()
{
if (m_thread != null)
{
m_thread.Abort();
m_thread = null;
}
}
//∥∥∥∥∥∥∥∥∥∥∥∥∥
}
}


[解决办法]
想法就有问题。

不要在Windows服务里面直接调用UI。
服务就是在后台运行的。

你看到IIS、SQL Server服务有界面么?

应该单独创建一个程序作为服务控制台,并且使用某种RPC和服务交互。
[解决办法]
设置服务可与桌面交互,就可以的了。
[解决办法]
会不会是服务登陆账号的权限导致的呢,或其他配置

我写过一个大的服务,几十个线程混乱的执行着。。。。。我一直认为服务中调用UI不好
我采用socket与服务交互。
[解决办法]
该回复于2013-07-12 00:06:28被版主删除

读书人网 >C#

热点推荐