Say I am desingning an exam portal page where i want To show countdown for the exam date and time and if that specific date and time is reached I want to redirect user to a login page. How can i do this? I have tried using Hangfire but it doesnt redirect me to the redirected page after the time is reached.The scheduled jobs gets updated on the database (HangFire.Job) but it doesnt redirect me to the other page. I am a total newbie in programming and i dont know much so Guiding me on how to acheive something like that would be appreciated a lot and yes, is it even possible to do something like this using Hangfire ?
public void Configuration(IAppBuilder app)
{
// For more information on how to configure your application, visit https://go.microsoft.com/fwlink/?LinkID=316888
GlobalConfiguration.Configuration
.UseSqlServerStorage("calanders");
var option = new DashboardOptions { AppPath = VirtualPathUtility.ToAbsolute("/Default.aspx") };
app.UseHangfireDashboard("/hangfire",option);
app.UseHangfireServer();
}
This is the Startup.cs
protected void Button1_Click(object sender, EventArgs e)
{
GlobalConfiguration.Configuration
.SetDataCompatibilityLevel(CompatibilityLevel.Version_170)
.UseColouredConsoleLogProvider()
.UseSimpleAssemblyNameTypeSerializer()
.UseRecommendedSerializerSettings()
.UseSqlServerStorage("calanders", new SqlServerStorageOptions
{
CommandBatchMaxTimeout = TimeSpan.FromMinutes(5),
SlidingInvisibilityTimeout = TimeSpan.FromMinutes(5),
QueuePollInterval = TimeSpan.Zero,
UseRecommendedIsolationLevel = true
});
BackgroundJob.Schedule(() => startCountdown(),TimeSpan.FromSeconds(20));
//d.InsertDate(TextBox1.Text.ToDa);
}
public void startCountdown()
{
Response.Write("<script>alert('Time Reached')</script>");
Response.Redirect("WebForm1.aspx");
}
This is the Main Page. (Default.aspx)
CodePudding user response:
In webforms there is no ongoing connection to the server in between postbacks, so a job that is activated on the server side won't be able to interact with the user at an arbitrary future time; only during the immediate time between the button click being triggered and the response being returned.
If you want to show a timer and then do something when it is done, you should look into how to do it with Javascript on the client side, if that works for your scenario. You can use Javascript to make background requests to the server if you need to, but your job won't be directly driven by the server side.