kryptonDockingManager save file and load file not working after restart app - winforms

i am using https://github.com/ComponentFactory/Krypton repo
i am using vs 2015 community
win 10
i am saving config file then close app then load here all is blank
This is my code
//load dock
private void loadDockToolStripMenuItem_Click(object sender, EventArgs e)
{
try
{
kryptonDockingManager.LoadConfigFromFile(#"C:\Users\SWT\Desktop\setting.ss");
}
catch (Exception rr)
{
MessageBox.Show(rr.Message);
}
}
//save dock
private void saveDockToolStripMenuItem_Click(object sender, EventArgs e)
{
try
{
kryptonDockingManager.SaveConfigToFile (#"C:\Users\SWT\Desktop\setting.ss");
}
catch (Exception rr)
{
MessageBox.Show(rr.Message);
}
}

Related

Cross thread operation not valid in BackgroundWorker

I want to display some data on form load in a data gridview , the data which i want to display is in large number of rows , when i use background worker processor it show me the following error.
My code:
private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)
{
try
{
FTPUtility obj = new FTPUtility();
dataGridViewRequest.DataSource = obj.ListRequestFiles();
dataGridViewRequest.Columns[0].AutoSizeMode = DataGridViewAutoSizeColumnMode.Fill;
dataGridViewRequest.Columns[1].AutoSizeMode = DataGridViewAutoSizeColumnMode.Fill;
dataGridViewRequest.Columns[2].AutoSizeMode = DataGridViewAutoSizeColumnMode.Fill;
dataGridViewResponses.DataSource = obj.ListResponseFiles();
dataGridViewResponses.Columns[0].AutoSizeMode = DataGridViewAutoSizeColumnMode.Fill;
dataGridViewResponses.Columns[1].AutoSizeMode = DataGridViewAutoSizeColumnMode.Fill;
dataGridViewResponses.Columns[2].AutoSizeMode = DataGridViewAutoSizeColumnMode.Fill;
}
catch (Exception ex)
{
MessageBox.Show(ex.Message, "Error Message", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
Form load:
private void FormFTP_Load(object sender, EventArgs e)
{
try
{
//this.comboBoxRequests.SelectedIndex = 0;
backgroundWorker1.RunWorkerAsync();
}
catch (Exception ex)
{
MessageBox.Show(ex.Message, "Error Message", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
There are many different ways to prevent the form from being freezed.
For example you can load your data like this:
private async void Form_Load(object sender, EventArgs e)
{
//do some initializations
await LoadData();
//do some other initializations that you need to perform.
}
private async Task LoadData()
{
//Load your data here
//For example
FTPUtility obj = new FTPUtility();
dataGridViewRequest.DataSource = obj.ListRequestFiles();
}
This way when running the form, the commands run in the sequence you wrote while the UI is responsive and you will not face with common difficulties of using BackgroundWorker or threads like Cross thread operation exceptions.
The key point is in using async/await. For more information read Asynchronous Programming with Async and Await
Remember that this way, every where you want to call LoadData, you should call it this way:
await LoadData();
And the method that you write this code in, should be async:
private async void RefreshButton_Click(object sender, EventArgs e)
{
await LoadData();
}
EDIT For .Net 4.0
For .Net 4.0 you can use both Task.Run or BackgroundWorker. I recommend Task.Run because it is more simple and more readable.
Please note Cross Thread Operation Exception will throw when you access the UI elements from another thread than UI. In this situations you should use this.Invoke(new Action(()=>{/*Access UI Here*/})); instead. And never put a time-consuming task in your invoke part.
BackgroundWorker Approach:
private void Form1_Load(object sender, EventArgs e)
{
backgroundWorker1.RunWorkerAsync();
//If you put some code here for example MessageBox.Show("");
//The code will immadiately run and don't wait for worker to complete the task.
}
private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)
{
this.LoadData();
}
public void LoadData()
{
var ftp = new FtpUtility();
var data = ftp.ListRequestFiles();
this.Invoke(new Action(() =>
{
//Setup columns and other UI stuff
//Set datasource of grid
this.dataGridView1.DataSource = data;
}));
}
Remember everywhere you formerly used LoadData, now you should use backgroundWorker1.RunWorkerAsync(); instead.
If you want to do a job after the worker completes the task, put your job in backgroundWorker1_RunWorkerCompleted or in Invoke part of LoadData.
Task.Run Approach
private void Form1_Load(object sender, EventArgs e)
{
Task.Run(() =>
{
LoadData();
})
.ContinueWith(x =>
{
//You can put codes you want run after LoadData completed here
//If you access the UI here, you should use Invoke
});
//If you put some code here for example MessageBox.Show("");
//The code will immadiately run and don't wait for worker to complete the task.
}
public void LoadData()
{
var ftp = new FtpUtility();
var data = ftp.ListRequestFiles();
this.Invoke(new Action(() =>
{
//Setup columns and other UI stuff
//Set datasource of grid
this.dataGridView1.DataSource = data;
}));
}
Remember everywhere you formerly used LoadData, now you should use Task.Run(()=>{LoadData();}); instead.
If you want to do a job after the LoadData completes, put your job in ContinueWith or in Invoke part of LoadData.
Try this.
private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)
{
try
{
dataGridViewRequest.Invoke(new Action(() => {
FTPUtility obj = new FTPUtility();
dataGridViewRequest.DataSource = obj.ListRequestFiles();
dataGridViewRequest.Columns[0].AutoSizeMode = DataGridViewAutoSizeColumnMode.Fill;
dataGridViewRequest.Columns[1].AutoSizeMode = DataGridViewAutoSizeColumnMode.Fill;
dataGridViewRequest.Columns[2].AutoSizeMode = DataGridViewAutoSizeColumnMode.Fill;
dataGridViewResponses.DataSource = obj.ListResponseFiles();
dataGridViewResponses.Columns[0].AutoSizeMode = DataGridViewAutoSizeColumnMode.Fill;
dataGridViewResponses.Columns[1].AutoSizeMode = DataGridViewAutoSizeColumnMode.Fill;
dataGridViewResponses.Columns[2].AutoSizeMode = DataGridViewAutoSizeColumnMode.Fill;
}));
}
catch (Exception ex)
{
MessageBox.Show(ex.Message, "Error Message", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
private void FormFTP_Load(object sender, EventArgs e)
{
try
{
//this.comboBoxRequests.SelectedIndex = 0;
backgroundWorker1.RunWorkerAsync();
}
catch (Exception ex)
{
MessageBox.Show(ex.Message, "Error Message", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}

Caching Cookies in Awesomium

The Setup:
I am building an MDI Application. One of the Child Forms I call is a basic web browser using the Awesomium API.
Reference: BrowserControl.cs
public partial class BrowserControl : UserControl
{
public BrowserControl()
{
InitializeComponent();
}
private void OnControlLoad(object sender, EventArgs e)
{
String w3DataPath = Path.GetDirectoryName(Assembly.GetEntryAssembly().Location) + "\\HttpAgent";
if (!Directory.Exists(w3DataPath))
{
Directory.CreateDirectory(w3DataPath);
}
var webSession = WebCore.CreateWebSession(w3DataPath, WebPreferences.Default);
webControl.WebSession = webSession;
}
private void OnBackButtonClick(object sender, EventArgs e)
{
this.webControl.GoBack();
}
private void OnFwdButtonClick(object sender, EventArgs e)
{
this.webControl.GoForward();
}
private void OnReloadButtonClick(object sender, EventArgs e)
{
this.webControl.Reload(true);
}
private void OnStopButtonClick(object sender, EventArgs e)
{
this.webControl.Stop();
}
private void OnHomeButtonClick(object sender, EventArgs e)
{
this.webControl.GoToHome();
}
private void OnSearchButtonClick(object sender, EventArgs e)
{
String urlString = String.Format("https://www.google.com/#q={0}", HttpUtility.UrlEncode(this.SearchBox.Text));
this.webControl.Source = new System.Uri(urlString);
}
private void OnLoadingFrame(object sender, Awesomium.Core.LoadingFrameEventArgs e)
{
this.BrowserStatus.Text = "Loading " + e.Url;
}
private void OnLoadingComplete(object sender, Awesomium.Core.FrameEventArgs e)
{
this.BrowserStatus.Text = "Document Ready";
}
private void OnLoadingFailure(object sender, Awesomium.Core.LoadingFrameFailedEventArgs e)
{
this.BrowserStatus.Text = e.ErrorDescription;
}
}
Problem: Functionally the browser works just fine; however, it does not cache any persistent data locally. Note, I have established a valid DataPath using the Awesomium WebSession object as indicated in the above method OnControlLoad()
In terms of the validity of the path I define - I have confirmed it does exist on disk and it even appears the Awesomium WebSession adds a sub-directory called "cache". Unfortunately, none of the cookies I am getting in my web session are being saved locally. In fact - nothing is being cached. Both the HttpAgent directory I define and the cache directory the WebSession object defines are empty after deliberately logging into sites I know set cookies.
Any insight or help would be greatly appreciated. Thanks
I am not sure if it helps but try closing Awesomium "correctly": dispose WebControls, WebSession, call WebCore.Shutdown().

How to start WPF application using Win Froms Application

I am trying to start a WPF application using Win Forms application.
Here is my code
private void button2_Click(object sender, EventArgs e)
{
try
{
var app = new App();
app.InitializeComponent();
app.Run(new MainWindow());
}
catch (Exception ex)
{
MessageBox.Show(ex.ToString());
}
}
When i click the button it will load WPF app. Then if i close the WPF and click the button again then i get the following error
Cannot create more than one System.Windows.Application instance in the same AppDomain
You cannot initiate more than one instance in App class. so you can solve this out using one App instance.Answer is following
public partial class Form1 : Form
{
App app;
public Form1()
{
InitializeComponent();
app = new App();
}
private void button1_Click(object sender, EventArgs e)
{
try
{
app.InitializeComponent();
app.MainWindow = new MainWindow();
app.ShutdownMode = System.Windows.ShutdownMode.OnExplicitShutdown;
app.MainWindow.Show();
}
catch (Exception ex)
{
MessageBox.Show(ex.ToString());
}
}
}

How to Add a progress bar when user trying to start/stop windows service

`private void btnStartServerIns_Click(object sender, RoutedEventArgs e)
{
try
{
ServiceController sc = new ServiceController("RMExSoftphoneService");
if ((sc.Status.Equals(ServiceControllerStatus.Stopped)))
{
sc.Start();
LogDebug("Starting the RMExSoftphoneServer");
}
LogDebug("Started the RMExSoftphoneServer");
}
catch (Exception Ex)
{
LogDebug(Ex.Message);
}
}`
I have a project which starts and stops a service. Most of the times it is fast but sometimes it is not. so i want to way to show the user that is function is being executed

Switching between multiple Cameras using Emgu CV

I have a quick question: I need to switch between the two camera on a tablet. Front and Back. By default, the Front camera is always used by Emgu CV.
Thanks.
Ok. There is a different constructor. I was building upon the 7 line demo for Emgu CV.
Using the correct overloaded constructor, this is what did the trick for me:
private Capture _capture;
private void InitCapture(Int32 _camIndex) {
try {
if (_capture != null) {
Application.Idle -= ProcessFrame;
}
_capture = new Capture(_camIndex);
Application.Idle += ProcessFrame;
}
catch (NullReferenceException excpt) {
XtraMessageBox.Show(excpt.Message);
}
}
private void ProcessFrame(object sender, EventArgs arg) {
Image<Bgr, Byte> frame = _capture.QueryFrame();
ImageBoxCapture.Image = frame;
}
private void CharmsBarBase_ButtonTop01Click(object sender, EventArgs e) {
InitCapture(0);
}
private void CharmsBarBase_ButtonTop02Click(object sender, EventArgs e) {
InitCapture(1);
}
Regards.

Resources