1. WcfServiceLibrary: 包含WCF服务接口的定义 (`IProgressService.cs`) 和实现 (`ProgressService.cs`)。
2. ProgressServiceUI: 包含进度条窗口的实现,该窗口作为WCF服务运行。
3. ProgressClient: Windows窗体应用程序,作为WCF服务的客户端。
以下是完整的项目结构:
```
Solution
│
├── WcfServiceLibrary
│ ├── IProgressService.cs
│ ├── ProgressService.cs
│ └── App.config
│
├── ProgressServiceUI
│ ├── ServicForm.Designer.cs
│ ├── ServicForm.cs
│ ├── Program.cs
│ └── App.config
│
└── ProgressClient
├── Form1.Designer.cs
├── Form1.cs
├── Program.cs
└── App.config
IProgressService.cs
using System.ServiceModel;namespace WcfServiceLibrary {[ServiceContract]public interface IProgressService{[OperationContract]void StartProgress();[OperationContract]void PauseProgress();[OperationContract]void ResumeProgress();[OperationContract]void ResetProgress();[OperationContract]int GetProgress();} }
ProgressService.cs
using System; using System.ServiceModel; using System.Threading; using WcfServiceLibrary;namespace WcfServiceLibrary {public class ProgressService : IProgressService{private int progress = 0;private bool isRunning = false;public void StartProgress(){isRunning = true;ThreadPool.QueueUserWorkItem(UpdateProgress);}public void PauseProgress(){isRunning = false;}public void ResumeProgress(){isRunning = true;}public void ResetProgress(){isRunning = false;progress = 0;}public int GetProgress(){return progress;}private void UpdateProgress(object state){while (isRunning){if (progress < 100){Thread.Sleep(100);progress++;}else{isRunning = false;}}}} }
App.config
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<system.serviceModel>
<services>
<service name="WcfServiceLibrary.ProgressService">
<host>
<baseAddresses>
<add baseAddress="http://localhost:8733/ProgressService/" />
</baseAddresses>
</host>
<endpoint address="" binding="basicHttpBinding" contract="WcfServiceLibrary.IProgressService" />
<endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange" />
</service>
</services>
<behaviors>
<serviceBehaviors>
<behavior>
<serviceMetadata httpGetEnabled="True" />
<serviceDebug includeExceptionDetailInFaults="False" />
</behavior>
</serviceBehaviors>
</behaviors>
</system.serviceModel>
</configuration>
ServicForm.cs
using System; using System.Threading; using System.Windows.Forms;namespace ProgressServiceUI {public partial class ServicForm : Form{private Thread progressThread;private bool isRunning = false;public ServicForm(){InitializeComponent();}public void StartProgress(){if (!isRunning){isRunning = true;progressThread = new Thread(UpdateProgress);progressThread.Start();}}public void PauseProgress(){isRunning = false;}public void ResumeProgress(){isRunning = true;}public void ResetProgress(){isRunning = false;progressBar1.Value = 0;}private void UpdateProgress(){while (isRunning){if (progressBar1.Value < progressBar1.Maximum){progressBar1.Value++;Thread.Sleep(100); // 控制进度更新速度 }else{isRunning = false;}}}} }
Program.cs
using System; using System.ServiceModel; using System.Windows.Forms; using WcfServiceLibrary;namespace ProgressServiceUI {class Program{static void Main(string[] args){using (ServiceHost host = new ServiceHost(typeof(ProgressService))){host.Open();Console.WriteLine("The service is ready.");Console.WriteLine("Press <Enter> to stop the service.");// 显示进度条窗口var servicForm = new ServicForm();servicForm.StartPosition = FormStartPosition.CenterScreen; // 设置窗口启动位置 servicForm.Show();Console.ReadLine();host.Close();}}} }
App.config
<configuration><system.serviceModel><services><service name="WcfServiceLibrary.ProgressService"><host><baseAddresses><add baseAddress="http://localhost:8733/Design_Time_Addresses/ProgressService/" /></baseAddresses></host><endpoint address="" binding="basicHttpBinding" contract="WcfServiceLibrary.IProgressService" /><endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange" /></service></services><behaviors><serviceBehaviors><behavior><serviceMetadata httpGetEnabled="True" /><serviceDebug includeExceptionDetailInFaults="False" /></behavior></serviceBehaviors></behaviors><serviceHostingEnvironment aspNetCompatibilityEnabled="true" multipleSiteBindingsEnabled="true" /></system.serviceModel> </configuration>