Pause timer-based program at MessageBox.Show() - c#

I have a MessageBox.Show event that I want to also prevents timer-based methods from running while the MessageBox remains open.
Here is my code (Changes the value in a file location on a network every x minutes):
public void offlineSetTurn()
{
try
{
using (StreamWriter sWriter = new StreamWriter("FileLocation"))
{
sWriter.WriteLine(Variable);
}
}
catch (Exception ex)
{
DialogResult result = MessageBox.Show("Can't find file. Click Okay to try again and Cancel to kill program",MessageBoxButtons.OKCancel);
if (result == DialogResult.OK)
{
offlineSetTurn();
}
else if (result == DialogResult.Cancel)
{
Application.Exit();
}
}
}
I have methods in the form that are calling this every thirty seconds. Meaning every thirty seconds, another MessageBox pops up. Is there a way to pause the application with MessageBox and if not, what would be the best way to resolve this issue? If possible, I'd like to avoid using Timer.Stop() as it would reset the Timer count.

The simplest solution is to have a flag indicating whether or not the message box is currently open:
private bool isMessageBoxOpen = false;
public void offlineSetTurn()
{
if (isMessageBoxOpen)
return;
try
{
using (StreamWriter sWriter = new StreamWriter("FileLocation"))
{
sWriter.WriteLine(Variable);
}
}
catch (Exception ex)
{
isMessageBoxOpen = true;
DialogResult result = MessageBox.Show("Can't find file. Click Okay to try again and Cancel to kill program",MessageBoxButtons.OKCancel);
isMessageBoxOpen = false;
if (result == DialogResult.OK)
{
offlineSetTurn();
}
else if (result == DialogResult.Cancel)
{
Application.Exit();
}
}
}

Related

How to open form inside method, submit button and then come back to original method and continue?

Our application requires that a witness must authenticate before a logged in user can perform enrollment operations (Enroll and Delete).
This is not an issue for Enrolling as I can add a check (IsWitnessApproved) to an enrollment_OnStartEnroll method I.E. before the Capture method is called and fired.
However, this is not possible for Deletion as I don't have access to a point where the enrollment_OnDelete method hasn't fired.
I haven't been able to get a response to this issue from Digital Persona so I'm now looking at work-arounds.
I'm exploring if its possible to open up a new form (WitnessApproval) inside the enrollment_OnDelete method, approve the witness in the form (btnConfirmWitness_Click) and then come back into the method and continue on with the deletion?
enrollment_OnDelete method:
private void enrollment_OnDelete(DPCtlUruNet.EnrollmentControl enrollmentControl, Constants.ResultCode result, int fingerPosition)
{
if (!witnessApproved)
{
WitnessApproval witnessApproval = new WitnessApproval();
witnessApproval.Show();
}
else
{
int fingerMask = GetFingerMask(fingerPosition);
if (enrollmentControl.Reader != null)
{
try
{
// Delete from database
new EnrollmentDAL().DeleteEnrolledFingerprint(Settings.Default.Username, fingerMask, txt_WitnessName.Text);
MessageBox.Show("Fingerprint deleted.", "Message", MessageBoxButtons.OK, MessageBoxIcon.Information);
pbFingerprint.Image = null;
pbFingerprint.Visible = false;
btnCancel.Visible = false;
witnessApproved = false;
txt_WitnessName.Text = String.Empty;
txt_WitnessPassword.Text = String.Empty;
}
catch (Exception ex)
{
MessageBox.Show("There was a problem deleting the fingerprint.", "Message", MessageBoxButtons.OK, MessageBoxIcon.Error);
new Util().LogError(ex);
}
}
else
{
MessageBox.Show("No Reader Connected.", "Message", MessageBoxButtons.OK, MessageBoxIcon.Warning);
}
_sender.Fmds.Remove(fingerPosition);
}
}
Selected WitnessApproval methods:
private void btnConfirmWitness_Click(object sender, EventArgs e)
{
lbl_Validation.Visible = false;
if (txt_WitnessName.Text == String.Empty)
{
SetMessage("Please enter a Witness.");
return;
}
if (txt_WitnessPassword.Text == String.Empty)
{
SetMessage("Please enter a Password.");
return;
}
if (txt_WitnessName.Text == Settings.Default.Username)
{
SetMessage("User and witness cannot be the same.");
return;
}
bool IsValidate = Membership.ValidateUser(txt_WitnessName.Text, txt_WitnessPassword.Text);
Settings.Default.WitnessName = txt_WitnessName.Text;
Settings.Default.WitnessPassword = txt_WitnessPassword.Text;
if (IsValidate)
{
this.Close();
// Allow enrollment operations
}
else
{
SetMessage("Witness credentials invalid.");
}
}
private void btnCancelWitness_Click(object sender, EventArgs e)
{
this.Close();
// DO NOT Allow enrollment operations
witnessCancelled = true;
}
private void SetMessage(string message)
{
lbl_Validation.Visible = true;
lbl_Validation.Text = message;
}
How to open form inside method, submit button and then come back to original method and continue?
There is ShowDialog method for this purposes.
Here is usage example from MSDN:
public void ShowMyDialogBox()
{
Form2 testDialog = new Form2();
// Show testDialog as a modal dialog and determine if DialogResult = OK.
if (testDialog.ShowDialog(this) == DialogResult.OK)
{
// Read the contents of testDialog's TextBox.
this.txtResult.Text = testDialog.TextBox1.Text;
}
else
{
this.txtResult.Text = "Cancelled";
}
testDialog.Dispose();
}
In your case, Form2 is WitnessApproval.
In WitnessApproval Form button handlers you will also need to set DialogResult to true when the witness is approved or to false when user cancelled operation.

create transaction in c# for executing series of function calls (all or none)

i want to implement a transaction in my code which is separated by functions.
There is one function in which all other functions are called. I want to make this function execute the function calls in a transaction (All or None).
Some of the function calls are queries while some are routine code for C# and Crystal report.
Can Some one help with this?
This function is working perfectly.
But if 2 users are accessing same database and executing this function at same time may get Cuncurrency problem.
the function code is attached.
public bool Generate_PDF(decimal wo_id)
{
if (Fill_WO_Report_Table(wo_id) == false) // this function has queries
return false;
try
{
string exception;
cryRpt = new ReportDocument();
cryRpt.Load(Application.StartupPath + "\\CrRpt_WO_Report.rpt");
DB_Interface.CrystalReport_Login(ref cryRpt, out exception);
string Temp_file_path = #"c:\Reports\WO_Temp.pdf";
cryRpt.ExportToDisk(ExportFormatType.PortableDocFormat, Temp_file_path);
string File_name = str_WO_File_Name + ".pdf";
// option to print at which location
//DialogResult dlg = MessageBox.Show("Select Option For Print :\n\nYes - Print To Default Path\nNo - Print To Custom Path\nCancel - Do Not Print",
// "Print GRN", MessageBoxButtons.YesNoCancel, MessageBoxIcon.Information);
SaveToDialog frm = new SaveToDialog(ref File_name);
DialogResult dlg = frm.ShowDialog();
File_name = frm.GetString();
frm.Dispose();
frm = null;
if (dlg == DialogResult.Yes)
{
if (Convert.ToString(MMS_Vars.PathReport_WO) == "")
throw new Exception("Default Path Not Available.");
string Full_File_name = Convert.ToString(MMS_Vars.PathReport_WO) + "\\" + File_name;
cryRpt.ExportToDisk(ExportFormatType.PortableDocFormat, Full_File_name);
System.Diagnostics.Process.Start(Full_File_name);
}
else if (dlg == DialogResult.No)
{
SaveFileDialog obj_saveFileDialog = new SaveFileDialog();
obj_saveFileDialog.InitialDirectory = Convert.ToString(MMS_Vars.PathReport_WO);
//obj_saveFileDialog.RestoreDirectory = true;
obj_saveFileDialog.FileName = File_name; //set file name to
obj_saveFileDialog.Filter = "PDF Files|*.pdf";
DialogResult result = obj_saveFileDialog.ShowDialog();
if (result == DialogResult.OK && obj_saveFileDialog.FileName != "")
{
cryRpt.ExportToDisk(ExportFormatType.PortableDocFormat, obj_saveFileDialog.FileName);
System.Diagnostics.Process.Start(obj_saveFileDialog.FileName);
}
else if (result == DialogResult.Cancel)
{
obj_saveFileDialog.Dispose();
cryRpt.Close();
cryRpt.Dispose();
return false;
}
obj_saveFileDialog.Dispose();
}
else if (dlg == DialogResult.Cancel)
{
cryRpt.Close();
cryRpt.Dispose();
return false;
}
cryRpt.Close();
cryRpt.Dispose();
// Drop Report tables
if (Drop_WO_Report_Table() == false) // this function has queries
return false;
return true;
}
catch (Exception exe)
{
MessageBox.Show(exe.Message, "WO Report", MessageBoxButtons.OK, MessageBoxIcon.Error);
return false;
}
}
You haven't posted any of the functions you do on your db. Are there any writes, etc? From your posted code, adding a transaction will probably not solve your problem. A simple solution can be to add a lock around the function so only one pdf is created at a time:
private static object locker = new object();
public bool Generate_PDF(decimal wo_id)
{
lock(locker){
if (Fill_WO_Report_Table(wo_id) == false) // this function has queries
return false;
........
........
return true;
}
catch (Exception exe)
{
MessageBox.Show(exe.Message, "WO Report", MessageBoxButtons.OK, MessageBoxIcon.Error);
return false;
}
}//END LOCK
}

How to implement "retry/abort" mechanism for writing files that may be used by another process?

This is really short question. I don't understand try-catch mechanism completely.
This is my current code:
public static void WriteText(string filename, string text)
{
try
{
System.IO.StreamWriter file = new System.IO.StreamWriter(filename);
file.Write(text);
file.Close();
}
catch(Exception exc)
{
MessageBox.Show("File is probably locked by another process.");
}
}
Background:
Im writing application that shares configuration files with another application.
I need some dialog messagebox with "retry" and "abort" buttons, when that file is used by other application. When that message will appear - I will close that other application and I will try to rewrite that file again by pressing "Retry" button.
Whatr we have is using a counter for re-tries and possibly a thread sleep.
So something like
int tries = 0;
bool completed = false;
while (!completed)
{
try
{
System.IO.StreamWriter file = new System.IO.StreamWriter(filename);
file.Write(text);
file.Close();
completed = true;
}
catch(Exception exc)
{
tries++;
//You could possibly put a thread sleep here
if (tries == 5)
throw;
}
}
Even though there's a good answer already I'll submit one that's more tuned towards the OP's question (let the user decide instead of using a counter).
public static void WriteText(string filename, string text)
{
bool retry = true;
while (retry)
{
try
{
System.IO.StreamWriter file = new System.IO.StreamWriter(filename);
file.Write(text);
file.Close();
retry=false;
}
catch(Exception exc)
{
MessageBox.Show("File is probably locked by another process.");
// change your message box to have a yes or no choice
// yes doesn't nothing, no sets retry to false
}
}
}
If you need more info on how to implement the messagebox check out the following links;
http://msdn.microsoft.com/en-us/library/0x49kd7z.aspx
MessageBox Buttons?
I would do it like that:
public static void WriteText(string filename, string text, int numberOfTry = 3, Exception ex = null)
{
if (numberOfTry <= 0)
throw new Exception("File Canot be copied", ex);
try
{
var file = new System.IO.StreamWriter(filename);
file.Write(text);
file.Close();
}
catch (Exception exc)
{
WriteText(filename,text,--numberOfTry,ex);
}
}
I like it more like this (example tries to save a RichTextBox on close and allows retrying save or aborting close):
protected override void OnClosing(CancelEventArgs e)
{
if (richTextBox_Query.Modified)
{
DialogResult result;
do
try
{
richTextBox_Query.SaveFile(
Path.ChangeExtension(Application.ExecutablePath, "sql"),
RichTextBoxStreamType.UnicodePlainText);
result = DialogResult.OK;
richTextBox_Query.Modified = false;
}
catch (Exception ex)
{
result = MessageBox.Show(ex.ToString(), "Exception while saving sql query",
MessageBoxButtons.AbortRetryIgnore);
e.Cancel = result == DialogResult.Abort;
}
while (result == DialogResult.Retry);
}
base.OnClosing(e);
}

pause threads for messagebox

I have some threads running and once an error is detected I wanna use an interaction like messagebox to continue execution or stop it. And I don't want multiple msgboxes on my screen so I added a semaphore to let only 1 thread do the job. But it doesn't work.
so I have the following situation:
private void DO_WORK()
{
//some code missing
lock (_threadLock)
{
++activeWorkers;
}
ThreadPool.QueueUserWorkItem(o =>
{
WorkRequests(result);
lock (_threadLock)
{
--activeWorkers;
Monitor.Pulse(_threadLock);
}
});
lock (_threadLock)
{
if (STOP)
break;
while (activeWorkers > THREADS)
Monitor.Wait(_threadLock);
}
}
private void WorkRequests(string mystr)
{
string source = null;
string result = null;
bool processed = false;
bool messageBoxShown = false;
///////////////////////////////////
while(true)//this is for rechecking the bad ones
{
source = GetSource(mystr);
if (source == "ERROR_LIMITED")
{
lock (_threadLock)
{
if (!messageBoxShown)//<--- check messageBoxShown
{
if (MessageBox.Show("Blocked IP detected!\nPlease change it!", "test program", MessageBoxButtons.OKCancel, MessageBoxIcon.Exclamation) == DialogResult.OK)
{
messageBoxShown = true; //<--- set to true
}
else
DoStop();
}
}
}
else
break;
}
result = mystr + " - " + source;
////////////////////////////////////////
}
How can I pause all threads except one which will show the messagebox and based on dialogbox to continue execution or stop it?
Your primary problem is that messageBoxShown is a local variable, so each thread is going to have its own copy of it. One thread setting it to true will not be seen by the other threads.
If you want all threads to be able to see it, you have to declare it at class scope:
private volatile bool messageBoxShown = false;
private void WorkRequests(string mystr)
{
// other stuff
lock (_threadLock)
{
if (messageBoxShown)
{
return;
}
}
// do dialog stuff, then
messageBoxShown = true;
}
Also, in your code you have:
if (!messageBoxShown)//<--- check messageBoxShown
{
if (MessageBox.Show("Blocked IP detected!\nPlease change it!", "test program",
MessageBoxButtons.OKCancel, MessageBoxIcon.Exclamation) == DialogResult.OK)
{
messageBoxShown = true; //<--- set to true
}
else
DoStop();
}
If the user presses Cancel then messageBoxShown is never set to true. So every thread will display the message box unless you have some other means to stop them from doing it.
Actually the problem in you code is you doesn't stop the other threads or ask it to skip showing MessageBox Your code actually limits number of threads can execute
MessageBox block to 1, So they start showing ony by one.
Instead try something like this
private volatile bool messageBoxShown = false;//Declare a Instance variable
var source = validate(mystr);
if (source == "ERROR")
{
lock (_threadLock)
{
semaf.WaitOne();
}
if(messageBoxShown)<--- check messageBoxShown
{
return;// or skip showing messagebox do whatever you want
}
if (MessageBox.Show("Blocked IP detected!\nPlease change it!", "test program", MessageBoxButtons.OKCancel, MessageBoxIcon.Exclamation) == DialogResult.OK)
{
lock (_threadLock)
{
messageBoxShown = true; <--- set to true
semaf.Release();
}
}
else
DoStop();
}
Hope this will solve your problem.

TargetInvocationException was unhandled at the end of DoWork Backgroundworker Method

Here's DoWork:
private void uploadWorker_DoWork(object sender, DoWorkEventArgs e)
{
uploadWorker.ReportProgress(20);
int tiffError = 0;
finalFiles = Directory.GetFiles(AppVars.FinalPolicyImagesFolder);
foreach (string file in finalFiles)
{
if (!file.EndsWith(".tiff"))
{
tiffError = 1;
break;
}
}
uploadWorker.ReportProgress(50);
if (tiffError == 1)
{
MessageBox.Show("There are files in this folder that are not .tiff. Please ensure only .tiff files are in this folder.", "Error!", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
else
{
if (finalFiles.Length == 0)
{
MessageBox.Show("There are no TIFF files to be uploaded. Please generate files first.", "Error!", MessageBoxButtons.OK, MessageBoxIcon.Error);
pbUpload.Value = 0;
EnableAllButtons();
}
else
{
double count = finalFiles.Length;
int current = 0;
int pbValue = 0;
uploadWorker.ReportProgress(70);
foreach (string file in finalFiles)
{
current = current + 2;
if (file.Contains(".tiff") == true)
{
PolicyNumber = Path.GetFileName(file).Split('_')[0];
basePolicyNumber = PolicyNumber.Remove(PolicyNumber.Length - 2);
basePolicyNumber = basePolicyNumber + "00";
finalPolicyName = Path.GetFileName(file);
PolicyUUID = Transporter.GetPolicyUUID(AppVars.pxCentralRootURL, basePolicyNumber);
if (PolicyUUID == "")
{
MessageBox.Show("The InsightPolicyID for the policy you are trying to upload does not exist in ixLibrary. Please ensure the policy number is correct. If you are sure it should be in ixLibray, please contact IT.", "Error!", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
else
{
ixLibrarySourceFileURL = AppVars.ixLibraryPolicyAttachmentsURL + finalPolicyName;
UploadToixLibraryErrorCode = Transporter.UploadFileToixLibrary(AppVars.ixLibraryPolicyAttachmentsURL, file);
if (UploadToixLibraryErrorCode != 0)
{
MessageBox.Show("There was an error uploading the file to ixLibrary. Please contact IT about this problem.", "Error!", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
else
{
GeneratePayLoadErrorCode = Transformer.GeneratePayLoad(ixLibrarySourceFileURL, finalPolicyName);
if (GeneratePayLoadErrorCode != 0)
{
MessageBox.Show("There was an error generating the XML for pxCentral. Please contact IT about this problem.", "Error!", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
else
{
pxCentralPOSTErrorCode = Transporter.pxCentralPOST(AppVars.pxCentralRootURL + PolicyUUID, AppVars.pxCentralXMLPayloadFilePath);
pbValue = Convert.ToInt32(((current / count) * 30) + 70);
uploadWorker.ReportProgress(pbValue);
}
}
}
}
}
}
}
}
As soon as it hits the last }, I get the TargetInvocationException was unhandled error here (see comment in code):
static class Program
{
[STAThread]
static void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
bool createdNew = false;
Mutex mutex = new Mutex(true, "MyApplicationMutex", out createdNew);
if (createdNew == true)
{
//error happens here
Application.Run(new frmMain());
}
else
{
MessageBox.Show("The application is already running.", Application.ProductName, MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
}
}
}
I'm not sure why this started happening all of the sudden. Does anyone know why?
Finally, here's RunWorkerCompleted:
private void uploadWorker_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
{
if (e.Error == null)
{
DeleteFinalFiles(finalFiles);
MessageBox.Show("Upload process complete.", "Complete!", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
EnableAllButtons();
}
You are calling EnableAllButtons in your DoWork handler. this, presumably, changes the Enabled state of buttons on the form. that is not legal from any other thread than the UI thread. You should make the call to EnableAllButtons in your ProgressChanged event handler or in your RunWorkerCompleted event handler. You're also calling ProgressBar.Value in the DoWork with the code pbUpload.Value = 0.
Also, you should call MessageBox.Show from your UI thread (i.e. in ProgressChanged or RunworkerCompleted handler) so that the MessageBox can be associated with your forms message pump propertly. You should pass a form object to MessageBox.Show to associate the message box with the form so you can't bring the form to the foreground while the message box is shown. e.g.:
MessageBox.Show(this,
"There are files in this folder that are not .tiff. Please ensure only .tiff files are in this folder.",
"Error!", MessageBoxButtons.OK, MessageBoxIcon.Error);
In WinForms, you must only access a control on the thread that created the control. Your DoWork event handler is not running on the thread that created the form (which, of course, is the point). Therefore, you must not access any of the controls on the form in the DoWork handler. Doing so can create unpredictable results.
I had exactly the same problem with TargetInvocation Exception raised after the completion of the background process. Inside the backgroundWorker ProgressChanges Event I have references to controls as shown below`private void m_oWorker_ProgressChanged(object sender, ProgressChangedEventArgs e)
{
// This function fires on the UI thread so it's safe to edit
// the UI control directly, no funny business with Control.Invoke :)
CurrentState state =
(CurrentState)e.UserState;
txtProgress.Text = state.CrawlStatus;
lblStatus2.Text = state.sStatus;
txtItemsStored.Text = state.TotItems.ToString() + " items";
txtLastRunTime.Text = state.MostRecentGatherDate.ToString();
AppNameKey.SetValue("LastCrawlTime", txtLastRunTime.Text);
}`
The DoWork event reads from a control
private void m_oWorker_DoWork(object sender, DoWorkEventArgs e)
{
DateTime LastCrawlTime;
try
{
LastCrawlTime = Convert.ToDateTime(txtLastRunTime.Text);
if (lblStatus2.Text != "Status: Running" || (!cmdRunNow.Enabled && cmdStopRun.Enabled)) // run is not currently running or cmdRunNow clicked
{
//lblStatus2.Text = "Status: Running";
GetUpdated(LastCrawlTime,e);
}
}
catch (Exception Ex)
{
MessageBox.Show(Ex.Message);
}
}
The RunWorkedrCompleted Event writes to a control:
void m_oWorker_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
{
if (e.Cancelled)
{
lblStatus2.Text = "Status: Stopped";
cmdStopRun.Enabled = false;
}
// Check to see if an error occurred in the background process.
else if (e.Error != null)
{
lblStatus2.Text = "Fatal Error while processing.";
}
else
{
// Everything completed normally.
//CurrentState state = (CurrentState)e.UserState;
lblStatus2.Text = "Status: Finished";
}
}
None of these caused problems. What did cause the problem was the attempt to reference e.UserState in the RunWorker_Completed event (commented out above)
I figured out the issue.
The progress bar was exceeding its maximum allowed (of 100).
The problem was that in the code, I was incrementing the progress bar as such:
current = current + 2;
I replaced it with:
current++;
The reason why I was incrementing by 2 was simply for testing purposes.

Categories

Resources